链表——找出最大值与最小值

找出单项链表中的最大值与最小值,同时返回
将结果存入数组返回
#include
#include
typedef struct node{
	int data;
	struct node* next;
}ElemSN;
ElemSN* CreatLink(int a[], int n)
{
	ElemSN *h = NULL, *p;
	for (int i = n - 1; i > -1; i-- ) {
		p = (ElemSN*)malloc(sizeof(ElemSN));
		p->data = a[i];
		p->next = h;
		h = p;
	}
	return h;
}
int* FindMax_Min(ElemSN* h)
{
	int b[2];
	int* p = b;
	int max = h->data, min = h->data;
	ElemSN* q = h->next;

	while (q){
		if (q->data > max) {
			max = q->data;
		}
		else if (q->data < min) {
			min = q->data;
		}
		q = q->next;
	} 
	b[0] = max;
	b[1] = min;
	return p;
}
int main(void)
{
	int a[] = { 3,2,5,8,4,7,6,9 };
	int* b;
	ElemSN* head = CreatLink(a, sizeof(a)/sizeof(a[0]));
	b = FindMax_Min(head);
	printf("max=%d,min=%d", b[0], b[1]);

	return 0;
}
将结果存入结构体返回
#include
#include
typedef struct node {
	int data;
	struct node* next;
}ElemSN;
typedef struct MAxmin {
	ElemSN* max;
	ElemSN* min;
}ElemSNMaxMin;

ElemSN* CreatLink(int a[], int n)
{
	ElemSN* h=NULL, * p;
	for (int i = n - 1; i > -1; i--) {
		p = (ElemSN*)malloc(sizeof(ElemSN));
		p->data = a[i];
		p->next = h;
		h = p;
	}
	return h;
}
ElemSNMaxMin* FindMaxMinNode(ElemSN* h)
{
	ElemSN* pmax, * pmin, * p;
	pmax = pmin = h;
	for (p = h->next; p; p = p->next) {
		if (p->data > pmax->data) {
			pmax = p;
		}
		else if (p->data < pmin->data) {
			pmin = p;
		}
	}
	ElemSNMaxMin* pm = (ElemSNMaxMin*)malloc(sizeof(ElemSNMaxMin));
	pm->max = pmax;
	pm->min = pmin;
	return pm;
}

int main(void)
{
	int a[] = { 3,2,5,8,4,7,6,9 };
	int n = sizeof(a) / sizeof(a[0]);
	ElemSN* head = CreatLink(a, n);
	ElemSNMaxMin* pm = FindMaxMinNode(head);
	printf("max=%d,min=%d", pm->max->data, pm->min->data);

	return 0;
}
二级指针
#include
#include
typedef struct node {
	int data;
	struct node* next;
}ElemSN;
ElemSN* CreatLink(int a[], int n)
{
	ElemSN* h=NULL, * p;
	for (int i = n - 1; i > -1; i--) {
		p = (ElemSN*)malloc(sizeof(ElemSN));
		p->data = a[i];
		p->next = h;
		h = p;
	}
	return h;
}
ElemSN** FindMaxMinNode(ElemSN* h)
{
	ElemSN* pmax, * pmin, * p;
	pmax = pmin = h;
	for (p = h->next; p; p = p->next) {
		if (p->data > pmax->data) {
			pmax = p;
		}
		else if (p->data < pmin->data) {
			pmin = p;
		}
	}
	ElemSN** pm = (ElemSN**)malloc(sizeof(ElemSN**));
	pm[0] = pmax;
	pm[1] = pmin;
	
	return pm;
}

int main(void)
{
	int a[] = { 3,2,5,8,4,7,6,9 };
	int n = sizeof(a) / sizeof(a[0]);
	ElemSN* head = CreatLink(a, n);
	ElemSN** pm = FindMaxMinNode(head);
	printf("max=%d,min=%d", pm[0]->data,pm[1]->data);

	return 0;
}
#include
#include
typedef struct node {
	int data;
	struct node* next;
}ElemSN;
ElemSN* CreatLink(int a[], int n)
{
	ElemSN* h=NULL, * p;
	for (int i = n - 1; i > -1; i--) {
		p = (ElemSN*)malloc(sizeof(ElemSN));
		p->data = a[i];
		p->next = h;
		h = p;
	}
	return h;
}
void FindMaxMinNode(ElemSN* h, ElemSN**ppmax, ElemSN**ppmin)
{
	ElemSN* p;
	*ppmax=*ppmin=h;
	for (p = h->next; p; p = p->next) {
		if (p->data > (*ppmax)->data) {
			*ppmax=p;
		}
		else if (p->data < (*ppmin)->data) {
			*ppmin=p;
		}
	}

}

int main(void)
{
	ElemSN *pmax,*pmin;
	int a[] = { 3,2,5,8,4,7,6,9 };
	int n = sizeof(a) / sizeof(a[0]);
	ElemSN* head = CreatLink(a, n);
	FindMaxMinNode(head,&pmax,&pmin);
	printf("max=%d,min=%d", pmax->data,pmin->data);

	return 0;
}

你可能感兴趣的:(数据结构,算法,数据结构,单链表)