线性表(链式存储)及其应用

上传人:woxinch****an2018 文档编号:39302660 上传时间:2018-05-14 格式:DOC 页数:7 大小:826KB
返回 下载 相关 举报
线性表(链式存储)及其应用_第1页
第1页 / 共7页
线性表(链式存储)及其应用_第2页
第2页 / 共7页
线性表(链式存储)及其应用_第3页
第3页 / 共7页
线性表(链式存储)及其应用_第4页
第4页 / 共7页
线性表(链式存储)及其应用_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《线性表(链式存储)及其应用》由会员分享,可在线阅读,更多相关《线性表(链式存储)及其应用(7页珍藏版)》请在金锄头文库上搜索。

1、线性表(链式存储)及其应用线性表(链式存储)及其应用一、一、 实验目的实验目的1. 掌握链表的结构原理、如何创建链表、如何输出结点数据、如何插入 结点、如何删除结点等。 2. 复习 C 语言,加深对 C 语言的理解和应用。二、二、 实验内容实验内容建立一个采用链式存储的线性表,表中元素为学生,每个学生信息包含姓 名和学号两部分,对该表实现:输出、查找、插入、删除功能。 程序源代码:#include #include #define NULL 0 #define LEN sizeof(struct student) #include #include #define A struct stude

2、nt struct student long num; char name8; struct student *next;int count; 云南大学物理实验教学中心云南大学物理实验教学中心 实验报告实验报告1/*-输入函数-*/ struct student *creat() A *head,*p,*pt;int length,i; printf(“你要输入的个数:“);scanf(“%d“, count=length;for(i=1;inext=p;pt=p;printf(“第%d 个数的学号“,i);scanf(“%ld“,while(p-num99999|p-numnum); pri

3、ntf(“第%d 个数的姓名“,i);scanf(“%s“, p-next=NULL;printf(“ 你输入的数据:n“);return(head);/*-输出函数-*/void print(A *head) A *p;p=head; printf(“ _n“); printf(“ 有%d 个学生n“,count); printf(“ 学号 姓名n“);if(head!=NULL) do printf(“%23ld%24sn“,p-num,p-name); p=p-next;while(p!=NULL); else printf(“sorry!空表“);printf(“ _n“); /*-内

4、部数据-*/struct student *date() A *head,*p1,*p2,*p3,*p4;head=(A *)malloc(LEN); if(head=NULL) printf(“申请空间失败“);head-next=NULL; p1=(A *)malloc(LEN); head=p1; p1-num=888;strcpy(p1-name,“Jack“);p1-next=NULL; p2=(A *)malloc(LEN); p1-next=p2; p2-num=150;strcpy(p2-name,“Rose“);p2-next=NULL; p3=(A *)malloc(LEN

5、); p2-next=p3; p3-num=133;strcpy(p3-name,“Bush“);p3-next=NULL; p4=(A *)malloc(LEN); p3-next=p4; p4-num=999;strcpy(p4-name,“Bush“);p4-next=NULL; count=4;printf(“ 内部数据:n“);return(head);云南大学物理实验教学中心云南大学物理实验教学中心 实验报告实验报告2/*-位置插入函数-*/ struct student *p_insert(A *head) A *p1,*p2,*inp;int place,i; printf(“

6、要插入的位置:“);scanf(“%d“,inp=(A *)malloc(LEN); printf(“学号“);scanf(“%ld“, while(inp-num99999|inp-numnum); printf(“姓名“);scanf(“%s“,p1=head; for(i=1;inext; if(i=1)inp=p1-next;head=inp; else p2-next=inp;inp-next=p1; count+;printf(“ 按位置插入后结果:n“);return(head); /*-排序函数-*/ struct student *shen_sort(A *head) A *

7、p,*min;int i=1;long m_num;char m_name8; p=head;min=head; for(i=1;inext;while(p!=NULL)if(p-numnum)m_num=p-num;p-num=min-num;min-num=m_num;strcpy(m_name,p-name);strcpy(p-name,min-name);strcpy(min-name,m_name); p=p-next; min=min-next; printf(“ 排序后结果:n“);return(head); /*-按顺序插入函数-*/ struct student *s_ins

8、ert(A *head) A *p1,*p2,*pi; head=shen_sort(head); pi=(A *)malloc(LEN); printf(“插入的学号“);scanf(“%ld“, while(pi-num99999|pi-numnum); printf(“姓名“);scanf(“%s“,p1=head;while(pi-nump1-nump1=p1-next;if(pi-numnum)if(p1=head) head=pi; else p2-next=pi; pi- next=p1;else p1-next=pi;pi-next=NULL;云南大学物理实验教学中心云南大学物

9、理实验教学中心 实验报告实验报告3count+;printf(“ 按顺序插入后结果:n“);return(head); /*-删除函数-*/ struct student *del(A *head) A *p1,*p2;long del_num; printf(“要删除的学号“);scanf(“%ld“,p1=head;while(p1-num!=del_nump1=p1-next;if(p1-num=del_num)if(p1=head)head=p1-next;else p2-next=p1-next;count-; printf(“ 删除后结果:n“); else printf(“学号不

10、存在,删除失败“);return(head); /*-修改函数-*/ struct student *amend(A *head) A *p;long num;printf(“要修改的学号“);scanf(“%ld“,p=head;while(p-num!=numif(p-num=num)printf(“修改后的姓名“);scanf(“%s“, printf(“ 修改后结果:n“);else printf(“学号不存在,修改失败“); return(head); /*-释放函数-*/ void free_list(A *head) A *p=head;printf(“释放链表“);while(

11、p!=NULL)head=head-next;free(p);p=head; /*-菜单函数-*/ void menu() printf(“*链表操作菜单*n“); printf(“ 1、输入 2、输出 3、内部数据 4、按位置插入n“); printf(“ 5、排序 6、顺序插入 7、删除n“); printf(“ 8、修改 0、释放链表 退出n“);printf(“* *n“); /*-主函数-*/ void main() A *head; int m;int flag=1;menu(); while(flag)云南大学物理实验教学中心云南大学物理实验教学中心 实验报告实验报告4 prin

12、tf(“输入代码:“);scanf(“%d“,switch(m) case 1:head=creat();print(head);break; case 2:print(head);break; case 3:system(“cls“);menu();head=date();print(head);break; case 4:system(“cls“);menu();print(head);head=p_insert(head);print(head);break; case 5:system(“cls“);menu();print(head);head=shen_sort(head);print(head);break; case 6:system(“cls“);menu();print(head);head=s_insert(head);print(head);break; case 7:system(“cls“);menu();print(head);head=del(head);print(head);break; case 8:system(“cls“);menu();p

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号