版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、#include #include 头文件struct student( long num;char name12;char age;char sex;定义结构体char addr3O;struct student *next;#defineNULLO#define LEN sizeof(struct student)定义 NULL=0确定结构体的长度struct student *pHead;指针定义(为内存中建立链表做准备)void load();void save();void insert();void modify();void delet();void disp();各函数的声明i
2、nt main(int argc, char* argv)char c;while(l)死循环printf(tlnput selection:ntH); printf(nl: Load Data ;ntH); printf(H2: Insert Record ;nt); printf(3: Modify Record ;ntH); printf(H4: Delete record ;ntH); printf(H5: Display All record ;ntH); printf(6: Save data ;ntH); printffO: Exit.nt);显示提示信息flushall();c
3、 = getchar();c = c0 x30;switch(c)清空缓存提取指令数字字符转化为数字执行相应的指令case 0:exit(O);break;case 1:load();break;case 2:insert();break;case 3:modify();break;case 4:delet();break;case 5:disp();break;case 6:save();(return 0;/*死循环条件不同) void load()FILE *fp;struct student *pl=NULL/*p2=NULL;/*p2=0,p2 #定指向 0000if(fp=fope
4、n(,studentData,;lrb,)=NULL) 打开文件若失败就退出printf(Hcan not open filenH); exit(O);fgetc(fp);while(!feof(fp)从fp所指定的文件中(开头)取得下一个字符到缓存判断从文件中取的字符是否为结束符fseek(fp,l,l);/(向前一个字符位)将fp所指定的文件的位置指针以(0:开头,1:当前位置2结尾)为起点的移动多少个单位 if(pl=(struct student *)malloc(LEN)=NULL)申请一个结构体的空间不成功就退出 return;退回到主函数if(p2=NULL) pHead=pl;
5、(条件只在链表头建立是成立)建立动态链表开头申请下一个(从第二个空间开始)结else p2-next = pl;构体空间与上一个的链接p2 = pl;的指针指向自己本身fread(pl, sizeof(struct student),l,fp);位)的数据存到pl所指的内存区fgetc(fp);fclose(fp);void save()(FILE *fp;struct student *pl=pHead;if(fp=fopen(,studentData,/,wb,)=NULL)(printf(ncan not open filenH);exit(O);while( pl!=NULL)fwri
6、te(pl, sizeof(struct student),fp);所指向的文件pl = pl-next;fclose(fp);)指针后移*最后的结构体中从指定文件中读取指定的长度(1各单从文件中取一下一个字符关闭文件定义pl指向Pheadf地址)打开出错,退出程序不到链表结尾不停止循环把pl所指向的一个结构体的数据输出到fppl在链表中后移关闭文件void insert()(struct student *pO/pl/p2;定义指针if(pO=(struct student *)malloc(LEN)=NULL)/申请内存空间,不成功就退出return;printf(tlnput num:t
7、”);提示输入相应信息(数据),数组不用取地址scanf(,%d,&pO-num);flushallf);printf(Htlnput name:tH);gets(pO-name);flushallf);printf(tlnput age:tM);scanf(,%d,&pO-age);flushallf);printf(tlnput sex:tH);scanf(c,&pO-sex);flushall();printf(,tlnputaddr:tH);gets(pO-addr);printfCXn);pl=pHead;if(pHead=NULL)pHead=pO;flushall();/pl和P
8、Head 一样没初始化,都为0的地址(不可写) /PHead没初始化时指OopO-next=NULL;开始组建链表else(画图(链表示意图)好理解while(pO-numpl-num) & (pl-next!=NULL) 找到 pO 在头之后的位置(p2后,pl前)*排序:学号从小到大(先于头比).(当输入了两个结构体以后有效) void modify()p2=pl;pl=pl-next;)if(pO-numnum)号小if(pHead=pl) pHead=pO;有一个现成的结构体)中学号还小,使po为头 else p2-next=pO;pO-next=pl;(else按从小到大排pl-ne
9、xt=pO;pO-next=NULL;)return;)新建的结构体中学号比pl里的学新建的结构体中学号比.头.(为仅新建的结构体接在p2、pl之间新建的结构体中学号比pl里的学号大,返回到主函数中的死循环中int i;char c;struct student *pl;pl=pHead;printf(HtPlease input the number of the stu. you want to modify:n); scanf(d,&i);while(l)if(pl-num=i)提示信息printf(Mtlnput selection to modify the information
10、of the student.:nt”);printf(nl: modify number;ntM);printf(n2: modify name;ntH);printf(3: modify age;nt);printf(H4: modify sex;ntH);printf(H5: modify address;ntH);printf(H6: modify all information.ntH);flushallf);c = getchar();提取指令c = c-0 x30;数字字符转化为数字switch(c)执行相应的指令case 1:printf(tlnput num:tH);flus
11、hall();scanf(%d,&pl-num);return;case 2:printf(Htlnput name:tM);flushall();gets(pl-name);flushall();return;case 3:printf(Htlnput age:tu);flushall();scanf(%d,&pl-age);return;case 4:printf(Htlnput sex:tn);flushall();scanf(%c”,&plsex);return;case 5:printf(Htlnput addr:tH);flushall();gets(pl-addr);return
12、;case 6:flushall();printf(Htlnput num:tM);scanf(,%d,&pl-num);flushall();printf(Htlnput name:tH);gets(pl-name);flushall();printf(Htlnput age:tM);scanf(,%d,&pl-age);flushall();printf(Htlnput sex:tH);scanf(%c,&plsex);flushall();printf(Htlnput addr:tH);gets(pl-addr);flushall();return;if (pl-next=NULL)pr
13、intf(Htthere isnt that student.nnn);return;if(pl-num!=i)pl = pl-next;void delet()(int i;struct student *pl,*p2;pl=pHead;printf(HtPlease input the number of the stu. you want to delete:ntH);scanf(%d“,&i);while(l)循环寻找指定的结构体if(pl-num=i)if(pl=pHead)删除链表开头pHead = pHead-next;elsep2-next = pl-next;删除非链表开头free(pl);释放空间break;跳出循环if (pl-next=NULL)printf(Htthere isnt that student.nnH);return;if(pl-num!=i)p2 = pl;pl = pl-next;)void disp()(struct student *pl=pHead;定义结构体指针printf(tnum t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- T/CI 633-2024DNA甲基化位点定点检测指南
- T/HNSPXH 026-2024宁陵梨膏
- DB42/T 2305-2024高品质住宅技术标准
- 贵州黔东南苗族侗族自治州2026-2027学年高三上学期模拟考试地理试卷( 含答案)
- 福建省厦门市2026-2027学年八年级上学期期中练习物理试卷(含解析)
- DB32/T 4761-2024大跨径钢桁梁制造规范
- 2025-2026年海洋生态系统与气候变化关系模拟试题
- 2026年四川省部编版七年级英语下册第12单元同步练习题
- 2026年生化标本溶血脂黄干扰练习试卷及答案
- 2026年真菌性肝脓肿病理摸底试卷及答案
- 生物系统建模与仿真课件
- 2025-2026学年江苏省苏州市吴中区木渎高级中学高二上学期10月月考数学试卷(含答案)
- 建设工程施工图设计方案
- 2025年9月27日安徽省市遴选笔试真题及解析(省直卷)
- T/CECCEDA 1-2025企业管理创新体系要求及实施指南
- 老旧小区改造施工安全文明管理方案解读
- 《基于WEB漏洞检测系统的设计与实现》10000字(论文)
- 初二物理第一、二单元测试试卷
- 实验室安全事故案例
- GA/T 1991-2022法庭科学疑似毒品中卡西酮等5种卡西酮类毒品检验气相色谱和气相色谱-质谱法
- 沈阳地铁6号线一期工程环评报告
评论
0/150
提交评论