栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言:找出单链表的最大值MAX(随机输入)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言:找出单链表的最大值MAX(随机输入)

#include
#include
#include
 
#define MAXSIZE 10
#define OK 1
#define ERROR 0
#define T 1
#define F -1
 
typedef int Status;
struct Student {
	int id;
};
 
typedef struct Dlist {
	Student data;
	struct Dlist* next;
}Dlist,*Zdlist;
 
//创建
Status Cj_List(Zdlist& L) {
	L = (Zdlist)malloc(sizeof(Student));
	if (L) {
		L->next = NULL;
		printf("创建成功!n");
		return OK;
	}
	printf("创建失败!an");
	return ERROR;
}
 
//插入
Status Cr_List(Zdlist &L ,int i,Student e) {
	Zdlist p ,s;
	p = L;
	int j = 0;                          //判断是否有头结点,无头节点则创建失败。
	while (p && (j < i - 1)) {
		p = p->next;
		++j;
	}
	if (!p || j > i - 1) {
		printf("插入失败!an");
		return ERROR;
	}
	s = (Zdlist)malloc(sizeof(Student));//插入时先新建头结点
	s->next = NULL;                 //定义p=L用p代替L进行指针遍历,防止丢失头结点
	s->data = e;
	s->next = p->next;           
	p->next = s;
	printf("插入成功!n");
	return OK;
	
}

//输出
Status Sc_List(Zdlist L) {
	Zdlist p = L;
	int sum=0;
	if (!p->next) {
		printf("链表为空!an");
		return ERROR;
	}
	printf("全部学生数据:n");
	while (p->next!=NULL) {
		p = p->next;
		sum++;      //输出总人数
		printf("%2dn", p->data.id);
	}
	return OK;
}

//找出最大值MAX
Status fine_max(Zdlist L){
	Zdlist p = L;
	int sum=0,max,temp;
	if (!p->next) {
		printf("链表为空!an");
		return ERROR;
	}
	while (p->next!=NULL) {
		p = p->next;
		max=p->data.id>max?p->data.id:max;
	}printf("max:%dn",max);
	return OK;
}
 
int main() {
	Zdlist L;
	Student e;
	int choose;
	int i,n=5;
	Cj_List(L);
	while(n>0){
	e.id=rand()%100;			
	scanf("%d", &i);
	Cr_List(L ,i, e);
	n--;
	}
	
	Sc_List(L);
	fine_max(L);
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385008.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号