




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include<stdio.h>#include<malloc.h>typedefstructStudent/*定义学生类*/{ intnum; charname[20]; charsex[2]; intage; floatgrade;}stu;typedefstructLNode{ studata; structLNode*next;}LNode,*Linklist;LinklistInitList_L(LinklistL)/*构造一个空的单向链表*/{ L=(Linklist)malloc(sizeof(stu)); if(!L) printf("ERROR\n"); else { L=NULL; printf("OK\n"); returnL; }}voidDestroyList_L(LinklistL)//销毁单向链表*/{ Linklistp; if(!L) printf("ERROR\n");else { while(L) { p=L; L=L->next; free(p); } printf("OK\n"); } }voidClearList_L(LinklistL)/*将L重置为空表*/{ Linklistp; if(!L) printf("ERROR\n"); else { while(L->next) { p=L->next; L->next=p->next; free(p); } printf("OK\n"); }}voidListEmpty_L(LinklistL)/*L为空表返回TRUE,否则返回FALSE*/{ if(!L) printf("ERROR\n"); else { if(!L->next) printf("TRUE\n"); else printf("FLASE\n"); }}intListLength_L(LinklistL)/*返回L中数据元素个数*/{ inti=0; Linklistp=L; if(!L) return0; else { while(p) { i++; p=p->next; } returni; }}voidGetElem_L(LinklistL,inti)//返回第i个元素的值*/{ stue; Linklistp=L; intj=1; while(p!=NULL&&j<i) { p=p->next; ++j; } if(!p||j>i) printf("ERROR\n"); else { printf("学生%d的数据为:\n",i); printf("学号:%d\n姓名:%s\n性别:%s\n年龄:%d\n成绩:%f\n",p->data.num,p->,p->data.sex,p->data.age,p->data.grade); printf("OK\n"); }}voidPriorElem_L(LinklistL,stucur_e)/*用pre_e返回cur_e的前驱*/{ Linklistp=L; while(p->next&&(p->next->data).num!=cur_e.num) p=p->next; if(!p->next) printf("ERROR\n"); else { printf("cur_e的前驱为:\n"); printf("num:%d\nname:%s\nsex:%s\nage:%d\ngrade:%f\n",p->data.num,p->,p->data.sex,p->data.age,p->data.grade); printf("OK\n"); }}voidNextElem_L(LinklistL,stucur_e)/*用next_e返回cur_e的后驱*/{ Linklistp=L; while(p&&(p->data).num!=cur_e.num) p=p->next; if(!p||!p->next) printf("ERROR\n"); else { printf("cur_e的后驱为:\n"); printf("num:%d\nname:%s\nsex:%s\nage:%d\ngrade:%f\n",p->next->data.num,p->next->,p->next->data.sex,p->next->data.age,p->next->data.grade); printf("OK\n"); }}voidListInsert_L(LinklistL,inti)/*在i位置之前插入新元素*/{ intj=0; Linklistp=L,s; while(p&&j<i-2) { p=p->next; ++j; } if(!p||j>i-2) printf("ERROR\n"); else { s=(Linklist)malloc(sizeof(LNode)); printf("请输入要插入的元素的数据:\n"); printf("学号:\n");scanf("%d",&s->data.num); printf("姓名:\n");scanf("%s",&s->);printf("年龄\n");scanf("%d",&s->data.age); printf("性别:\n"); scanf("%s",&s->data.sex); printf("成绩:\n"); scanf("%f",&s->data.grade); s->next=p->next; p->next=s; printf("OK\n"); }}voidListDelete_L(LinklistL,inti)/*删除第i个元素*/{ Linklistp=L,q; intj=0; while(p&&j<i-2) { p=p->next; ++j;} if(!p->next||j>i-2) printf("ERROR\n"); else { q=p->next; p->next=q->next; free(q); printf("OK\n"); }}voidListTraverse_L(LinklistL,intvisit(stu))/*对L中每个元素调用函数visit()*/{ Linklistp=L->next; while(p) { if(!visit(p->data)) { printf("ERROR\n"); break; break; } } if(!p) printf("OK\n");}LinklistListCreat_L()/*建立不带头结点的单链表*/{ LinklistL,p1,p2; intn=0; p1=p2=(Linklist)malloc(sizeof(LNode)); printf("请输入学生的数据,学号为零停止\n学号:\n"); scanf("%d",&p1->data.num); printf("姓名:\n"); scanf("%s",&p1->); printf("年龄\n"); scanf("%d",&p1->data.age); printf("性别:\n"); scanf("%s",&p1->data.sex); printf("成绩:\n"); scanf("%f",&p1->data.grade); L=NULL;while(p1->data.num!=0){ n=n+1; if(n==1) L=p1; else p2->next=p1; p2=p1; p1=(Linklist)malloc(sizeof(LNode)); printf("请输入学生的数据,学号为零停止:\n学号:\n"); scanf("%d",&p1->data.num); printf("姓名:\n"); scanf("%s",&p1->); printf("年龄\n"); scanf("%d",&p1->data.age); printf("性别:\n"); scanf("%s",&p1->data.sex); printf("成绩:\n"); scanf("%f",&p1->data.grade); } p2->next=NULL; returnL;}voiddisplay(Linklistp){ printf("***************学生信息*****************\n"); printf("学号姓名性别年龄成绩\n"); while(p) { printf("%d\t%s\t%s\t%d\t%f\n",p->data.num,p->,p->data.sex,p->data.age,p->data.grade); p=p->next; } printf("****************************************\n");}voidmain(){ LinklistL,Q,p; printf("首先请先建立一个学生数据单链表再进行其他操作\n"); Q=ListCreat_L(); p=Q; L=Q; printf("现在输出建立的表\n"); display(p); intn; printf("\n请输入任意键进入菜单........\n"); getchar();for(;;) { printf("***********************菜单******************\n"); printf("1.建立空的单链表\n"); printf("2.建立单链表\n"); printf("3.销毁单链表\n"); printf("4.将单链表置为空表\n"); printf("5.L若为空表返回TRUE,否则返回FLASE\n"); printf("6.返回L中数据元素个数\n"); printf("7.返回L中第i个元素的值\n"); printf("8.返回cur_e的前驱\n"); printf("9.返回cur_e的后驱\n"); printf("10.在i之前插入新元素\n"); printf("11.删除第i个元素\n"); printf("0.结束\n"); printf("***********************************************\n"); printf("请输入你的选择(0~11):\n");scanf("%d",&n);switch(n){case1:L=InitList_L(L);break;case2:{ printf("现在请重新建立单链表\n"); L=ListCreat_L(); printf("现在输出建立的单链表\n"); display(L); break; }case3:DestroyList_L(L);break;case4:ClearList_L(L);break;case5:ListEmpty_L(L);break;case6:printf("线性表的长度为%d.\n",ListLength_L(Q));break;case7:{ inti; printf("请输入查找的数据位序:\n"); scanf("%d",&i); GetElem_L(L,i); break; } case8:{ stucur_e; printf("请输入cue_e的学生的学号:\n"); scanf("%d",&cur_e.num); PriorElem_L(L,cur_e); break;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO/IEC 17067:2013 AR Conformity assessment - Fundamentals of product certification and guidelines for product certification schemes
- 【正版授权】 ISO 15118-10:2025 EN Road vehicles - Vehicle to grid communication interface - Part 10: Physical layer and data link layer requirements for single-pair Ethernet
- 【正版授权】 IEC 62304:2006 EN-D Medical device software - Software life cycle processes
- 【正版授权】 IEC 61834-2:1998 FR-D Recording - Helical-scan digital video cassette recording system using 6,35 mm magnetic tape for consumer use 525-60,625-50,1125-60 and 1250-50 syst
- 【正版授权】 IEC 60335-2-73:2002+AMD1:2006 CSV FR-D Household and similar electrical appliances - Safety - Part 2-73: Particular requirements for fixed immersion heaters
- 皮瓣移植患者护理
- 学前教育一日生活
- 大班整合课程:我和我的外婆
- 汽车行业的营销案
- 2025年中学校青年老师培育实施方案
- 博物馆布展项目施工组织设计
- 大学藻类课件教学课件
- 报关实务-教学课件 第一章 海关概念
- OECD -二十国集团 经合组织公司治理原则2023
- 体育中国学习通超星期末考试答案章节答案2024年
- 化妆品生产质量管理规范与流程
- 矩形的判定公开课公开课获奖课件百校联赛一等奖课件
- GB/T 39673.6-2024住宅和楼宇电子系统(HBES)及楼宇自动化和控制系统(BACS)第6部分:规划和安装要求
- 医疗机构消防安全突出火灾风险和检查要点
- 焊接工程劳务分包
- 中国矿业大学《自然辩证法》2022-2023学年期末试卷
评论
0/150
提交评论