实验二链表的实现和应用

上传人:shaoy****1971 文档编号:108139407 上传时间:2019-10-22 格式:DOC 页数:7 大小:25.50KB
返回 下载 相关 举报
实验二链表的实现和应用_第1页
第1页 / 共7页
实验二链表的实现和应用_第2页
第2页 / 共7页
实验二链表的实现和应用_第3页
第3页 / 共7页
实验二链表的实现和应用_第4页
第4页 / 共7页
实验二链表的实现和应用_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《实验二链表的实现和应用》由会员分享,可在线阅读,更多相关《实验二链表的实现和应用(7页珍藏版)》请在金锄头文库上搜索。

1、实验二 链表的实现和应用一、 实验目的掌握线性表的链式存储结构设计与基本操作的实现二、 实验内容与要求 定义线性表的链式存储表示; 基于所设计的存储结构实现线性表的基本操作; 编写一个主程序对所实现的线性表进行测试; 线性表的应用:设线性表L1和L2分别代表集合A和B,试设计算法求A和B的并集C,并用线性表L3代表集合C;设线性表L1和L2中的数据元素为整数,且均已按值非递减有序排列,试设计算法对L1和L2进行合并,用线性表L3保存合并结果,要求L3中的数据元素也按值非递减有序排列。三、 数据结构设计 在主函数中实现函数的调用,从而实现线性表的插入、删除、创建、显示等。四、 测试结果 通过输入

2、不同的数字(16)实现不同的操作,在两个线性表结合时,线性表二在源程序中已给出的,通过操作可以得到非单调递减的有序数列五、 心得体会 在写程序的过程中要注意分析,参照其他标准程序,多上机运行一点点的改正错误,这样才会有所提高。MAIN.C 方丽平 信计1203班 1130112321 最后修改时间2014/3/31#include#include#define maxsize 1024typedef int datatype;typedef struct nodedatatype data;struct node * next;linkList;int main()linkList * CRE

3、ATE();int INSERT(linkList *head, int value, int position);int DELETE(linkList *head, int position);int DISPLAY(linkList *head);linkList * COMBINE(linkList *head1, linkList *head2);linkList * head1;linkList * head2;linkList * head3;linkList * head;linkList * p1,* p2,* s1,* s2,* r1,* r2;int position,

4、value, i;head1 = malloc(sizeof(linkList);r1 = head1;head2 = malloc(sizeof(linkList);r2 = head2;for(i = 0; idata = i;r1 -next = s1;r1 = s1;s2 = malloc(sizeof(linkList);s2 -data = i + 2;r2 -next = s2;r2 = s2;r2 - next = NULL;r1 - next = NULL;while(1) printf(the program n); printf(can realize to create

5、,insert,delete,display,etcn); printf(1:create the ranked listn); printf(2:insert a datan); printf(3:delete a datan); printf(4:display all the datan); printf(5:Combine two link lists of operationn); printf(6:return,end of the programn); printf(selection of operationn); scanf(%d,&i); while ( i 6 ) pri

6、ntf(please input againn); scanf(%d,&i); switch(i) case 1: head = CREATE(); break; case 2: /*INSERT(p);*/ printf(Please input insert placen); scanf(%d,&position);printf(Please input insert valuen); scanf(%d,&value);INSERT(head, value, position); break; case 3: printf(Please input delete positionn); s

7、canf(%d,&value);DELETE(head, position); break; case 4: DISPLAY(head); break;case 5:printf(The list 1:n);DISPLAY(head1);printf(The list 2:n);DISPLAY(head2);printf(The combine list:n);head3 = COMBINE(head1, head2);DISPLAY(head3);break; case 6: exit(0); break; linkList * CREATE() int value; linkList *

8、head,* s,* r; head = malloc(sizeof(linkList); r = head; printf(input the data of the number,input 55 over!); scanf(%d,&value); do s = malloc(sizeof(linkList); s -data = value; r -next = s; r = s; printf(input the data of the number); scanf(%d,&value); while(value != 55); r - next = NULL; return head

9、;int INSERT(linkList *head,int value, int position)int j = 1;linkList *p,*s,*n;s = malloc(sizeof(linkList);s -data = value;p = head;printf(Note:if the position is greater than the length of the table will be put in the final);while(j next) != NULL )p = p - next;j +;n = p - next;s - next =n;p - next

10、= s;return 0; int DELETE(linkList *head, int position)int j;linkList *p;linkList *s;p = head;printf(Note:if the position is greater than the length of the table will be delete nothing!);for(j = 1;j next) != NULL )p = p - next;elsereturn 0;s = p - next;if(s - next) != NULL)(*p).next = (*s).next;s = N

11、ULL;return 0;int DISPLAY(linkList *head)linkList *p;int i;i = 0;p = head - next; while(p != NULL) printf(%5d,(*p).data); p = p - next; i +; if(i % 5 = 0) printf(n); return 0;linkList * COMBINE(linkList *head1, linkList *head2)linkList *head,*r;linkList *p1, *p2,*s;head = malloc(sizeof(linkList);r = head;p1 = malloc(sizeof(linkList);p2 = malloc(sizeof(linkList);p1 = head1 - next;p2 = head2 - next;dos = malloc(sizeof(linkList);if( (p1 != NULL) & (p2 != NULL) )if(p1 - data data )s - data = p1 - data;p1 = p1 -next;elses - data = p2 - data;

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

当前位置:首页 > 办公文档 > 其它办公文档

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