数据结构实验报告合并链表

数据结构实验报告合并链表

ID:47892231

大小:143.84 KB

页数:7页

时间:2019-10-19

数据结构实验报告合并链表_第1页
数据结构实验报告合并链表_第2页
数据结构实验报告合并链表_第3页
数据结构实验报告合并链表_第4页
数据结构实验报告合并链表_第5页
资源描述:

《数据结构实验报告合并链表》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、《数据结构》实验报告班级:10020801姓名:吴亮学号:2008302651电话:15191416617日期:2009.11.7(一)需求分析1、输入的形式和输入值的范围:根据题目要求与提示输入两链表,且数与数之间用空格隔开,输入一行数后用0作为结束符。2、输出的形式:输出合并后的链表。3、程序所能达到的功能:程序能合并两个有序链表为一个新的有序链表,并以从小到大的顺序输出。4、测试数据:输入一个链表,并用空格将数隔开,每个链表以0作为结束符,如:1390246100输岀的链表为:12346910.(二)概要设计为了实现上述操作,应以链表为存储结构。1.基

2、本操作:(1).structnode*scanff()初始条件:structnode*scanff形式的指针存在;操作结果:建立链表函数,返回建立好的链表的头结点heado(2).voidprint(structnode*head)初始条件:structnode*head为一已知链表的头结点;操作结果:输出该链表。(3)・structnode*insert(structnode*ah,structnode*bh)初始条件:ah,bh两链表己知,且有序;操作结果:合并3h,bh为一新的有序链表。2.本程序包含二个模块:(1)主程序模块;(2)输入,输出,合并链

3、表函数模块(3)模块调用图:主程序模块输入链表模块输出链表模块合并链表模块(三)详细设计1.存储类型,元素类型,结点类型:structnode{//构造链表指针结构intdata;structnode*next;}node,*Inode;元素类型为整形。1.每个模块的分析:(1)主程序模块:voidmain(){structnode*ahead,*bhead,*abh;合并两链表ahead=scanff();//调用scanff函数,输入表Abhead=scanff();//调用scanff函数,输入表Babh=insert(ahead,bhcad);//调

4、用insert函数,print(abh);〃输出合并后的函数}(2)输入函数模块structnode*scanff(){〃建立链表函数,返回建立好的链表的头结点structnode*pl;structnode*head;n二0;structnode*p2;pl=p2=(structnode*)malloc(len);scanf("%d〃,&pl->data);head二NULL;while(pl->data!=0){n++;if(n==l)head二pl;elsep2->next=pl;p2二pl;pl=(structnode*)malloc(len);sc

5、emf("%d",&pl->data);}p2->next=NULL;return(head);}输出函数模块voidprint(structnode*head){//建立输出函数structnode*p;p=head;if(p!二NULL)printf(z/%d/z,p->data);p=p->next;}while(p!二NULL);}合并链表函数模块structnode*insert(structnode*ah,structnode*bh){〃定义insert函数,用來合并两个链表structnode*pal,*pbl,*pa2,*pb2;pa2=pa

6、l=ah;pb2=pbl=bh;do{whi1e((pbl->data>pal~>data)&&(pal->next!=NULL)){pa2=pal;pal二pal->next;}if(pbl->data<=pal->data){if(ah二二pa1)ah=pb1;else{pa2->next二pbl;pbl=pbl~>next;pb2~>next二pal;pa2=pb2;pb2=pbl;}}}while((pal->next!=NULL)

7、

8、(pal==NULL&&pbl!=NULL));if((pbl!=NULL)&&(pbl->data>pal->da

9、ta)&&(pal~>next==NULL))pal->next二pbl;return(ah);3)函数调用关系图main()structnodestructnode*insert(structnode*ah,structnodevoidprint(struet*scanff()*bh)node*head)1.完整的程序:#inelude"stdio.h"#include"malloc.h"#definelensizeof(stmetnode)structnode{〃构造链表指针结构intdata;structnode*next;}node,binode;i

10、ntn;structnode*scanff(){//

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。