




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实现功能: 1. 创建一个新链表。 2. 插入节点。 3. 删除节点。 4. 插入法排序链表(从小到大)。 5. 选择法排序链表(从小到大)。 6. 显示当前链表。 0. 退出程序。 . . . /*单向链表的相关操作*/ /* 2010.03.27 */ . . #include “stdio.h“ #include “malloc.h“ #include “stdlib.h“ #define null 0 int length; struct List int data; struct List *next; List; struct List * InitList() struct List *head,*p,*q; int d; printf(“n 创建一个链表:nn“); head=(struct List *)malloc(sizeof(struct List); head-next=null; q=head; scanf(“%d“, while(d0) p=(struct List *)malloc(sizeof(struct List); p-next=null; p-data=d; q-next=p; q=p; scanf(“%d“, length+; return head; void ListInsert(struct List *head,int i,int e) struct List *p,*q; int j=1; if(i0 while(jnext; j+; q=(struct List *)malloc(sizeof(struct List); q-data=e; q-next=p-next; p-next=q; length+=1; else printf(“n 对不起您所输入的节点位置或数据出错,数据插入失败!n“); int ListDelete(struct List *head,int i,int e) struct List *p,*q; int j=0; if(i0 j+; e=p-next-data; q=p-next; p-next=q-next; free(q); length-=1; return e; else printf(“n 对不起您所输入的节点位置出错,数据删除失败!n“); return null; void ListInsertSort (struct List *head) struct List *p,*q,*s; q=head-next-next; head-next-next=null; while(q) s=q-next; p=head; while(p-next q-next=p-next; p-next=q; q=s; void ListChooseSort (struct List *head) struct List *p,*q; int t; for(p=head-next;p-next;p=p-next) for(q=p-next;q;q=q-next) if(p-dataq-data) t=p-data; p-data=q-data; q-data=t; void print(struct List *head) struct List *p; if(head) p=head-next; while(p) printf(“%5d “,p-data); p=p-next; printf(“n“); void main() struct List *head; int i,e,n; head=null; do system(“cls“); printf(“nn 单向链表的相关操作nnn“); printf(“ 1. 创建一个新链表。nn“); printf(“ 2. 插入节点。nn“); printf(“ 3. 删除节点。nn“); printf(“ 4. 插入法排序链表(从小到大)。 nn“); printf(“ 5. 选择法排序链表(从小到大)。 nn“); printf(“ 6. 显示当前链表。nn“); printf(“ 0. 退出程序。nnnn“); printf(“ 请选择所要操作的项目:“); scanf(“%d“, switch(n) case 1: system(“cls“); head=InitList(); printf(“n 您所创建的链表为:“); print(head); printf(“n“); system(“pause“); break; case 2: system(“cls“); if(length) printf(“n 当前链表为:“); print(head); printf(“n 请输入要插入的节点位置及要插入的数据:“); scanf(“%d%d“, ListInsert(head,i,e); printf(“n 插入节点后的链表为:“); print(head); else printf(“n 请先创建链表,再执行此操作!nn“); printf(“n“); system(“pause“); break; case 3: system(“cls“); if(length) printf(“n 当前链表为:“); print(head); printf(“n 请输入要删除的节点位置:“); scanf(“%d“, e=ListDelete(head,i,e); printf(“n 您所删除的节点处的数据为%dnn“,e); if(length) printf(“删除节点后的链表为:“); else printf(“删除节点后的链表为空表。“); print(head); else printf(“n 请先创建链表,再执行此操作!nn“); printf(“n“); system(“pause“); break; case 4: system(“cls“); if(length) printf(“n 当前链表为:“); print(head); ListInsertSort(head); printf(“n 排序后的链表为:“); print(head); else printf(“n 请先创建链表,再执行此操作!nn“); printf(“n“); system(“pause“); break; case 5: system(“cls“); if(length) printf(“n 当前链表为:“); print(head); ListChooseSort(head); printf(“n 排序后的链表为:“); print(head); else printf(“n 请先创建链表,再执行此操作!nn“); printf(“n“); system(“pause“); break; case 6: system(“cls“); if(length) printf(“n 当前链表为:“); print(head); else printf(“n 请先创建链表,再执行此操
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年天成教育命题研究院高三物理第一学期期末检测试题
- 安徽省蚌埠市田家炳中学、五中2025年物理高三第一学期期末达标检测模拟试题
- 企业电力施工安全培训课件
- 澳洲超时出境管理办法
- 电子业务印章管理办法
- 煤矸石管理办法江西省
- 企业安全用电常识培训
- 出租车公司安全培训会议课件
- 2025服务器租用合同
- 出国务工安全教育培训课件
- UI视觉设计(适用培训、教学)-第6章-游戏界面设计
- 吴《园林植物配置技术》课件
- 小学生卡通自我介绍PPT
- 技术文档编制管理规定
- 集成电路芯片测试技术PPT全套完整教学课件
- 合理低价法投标报价得分自动计算表
- 土地资源管理专业考试知识事业单位考试
- 《琵琶行》导学案-教师版
- 安全技术交底(蜘蛛人)
- 砼搅拌机、灰浆机验收记录表
- 1999年版干部履历表
评论
0/150
提交评论