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

找出单项链表中的最大值与最小值,同时返回
将结果存入数组返回
#include<stdio.h>
#include<malloc.h>
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<stdio.h>
#include<malloc.h>
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<stdio.h>
#include<malloc.h>
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<stdio.h>
#include<malloc.h>
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;
}
以下是用 C 语言实现的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 struct Node { int val; struct Node* next; }; // 创建链表 struct Node* createList(int n) { struct Node* head = NULL; struct Node* tail = NULL; for (int i = 0; i < n; i++) { int val; scanf("%d", &val); struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->val = val; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; } // 输出链表最大值 void printMax(struct Node* head) { int max = head->val; while (head != NULL) { if (head->val > max) { max = head->val; } head = head->next; } printf("%d ", max); } // 判断链表是否为升序 int isSorted(struct Node* head) { while (head != NULL && head->next != NULL) { if (head->val > head->next->val) { return 0; } head = head->next; } return 1; } int main() { int n; scanf("%d", &n); struct Node* head = createList(n); printMax(head); if (isSorted(head)) { printf("1"); } else { printf("0"); } return 0; } ``` 代码思路: 1. 定义链表结构体,包括节点值和指向下一个节点的指针。 2. 创建链表,读取输入的每个元素,并将其存储在链表节点中。 3. 输出链表最大值,遍历链表,记录最大值输出。 4. 判断链表是否为升序,遍历链表,比较相邻节点的值,如果前一个节点的值大于后一个节点的值,则链表不是升序的。 5. 在 main 函数中调用上面的函数,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值