数据结构实验报告(一).docVIP

  • 6
  • 0
  • 约4.26千字
  • 约 6页
  • 2018-12-13 发布于天津
  • 举报
数据结构实验报告(一)

计算机027 孟宏铭PAGE PAGE 4 计算机027 孟宏铭数据结构实验报告(一) 一、 实验名称:链表的使用 二、 实验目的: (1)学习对链表的操作; (2)近一步熟悉C/C++编程及调试。 三、 实验内容: (1)创建两个整数链表; (2)对两个链表进行排序; (3)将两个链表合并为一个有序表; (4)打印三个链表。 四、实验原理: *创建两个带头节点的链表,头节点储存链表中元素的个数; *用插入法将两个链表排序,使链表中的数据按由小到大的顺序排列; *有序合并两个链表,把合并的链表放在新的内存空间中; *遍历链表,输出三个有序表。 此实验的程序部分分为三个部分,list.cpp(内部包括所有对链表的操作,创建链表、链表的排序、链表的合并以及链表的输出);main.cpp(主函数);list.h(头函数的声明、结构体的定义)。(见附录) 五、调试程序: 按运行程序提示输入数据,得到结果如下: 程序结束,得到预想结果! 如果输入的节点数小于或等于0结果如下: 六、结果分析: 此程序运行可读性比较好,程序对错误的输入,以及重复的数据都有相应的处理,对各种大小不同的数据排列都可以成功的得出结果,说明它很健壮。 但还可以把在程序的重复使用上有所提高。 七、附录: // list.h #includemalloc.h struct student { long num; //float score; struct student *next; }; //create struct student *create(int m); //print void print(struct student *head); //sort() void sort(struct student *head); //merge struct student *merge(struct student *la,struct student *lb); // main.cpp #includelist.h #includeprocess.h #includeiostream.h // main() void main() { cout* 创建两个链表 *endlendlendl; cout* 链表La *:endl; int n=0; struct student *La=create(n); if(La!=NULL) print(La); cout* 链表Lb *:endl; struct student *Lb=create(n); if(Lb!=NULL) print(Lb); //全空 if((La==NULL)(Lb==NULL)) { cout!两链表都为空!endl; cout* 程序结束 谢谢使用 *endl; exit(1); } cout* 对两个链表排序 *endl; sort(La);if(La==NULL)cout!La为空链表!endl; sort(Lb);if(La==NULL)cout!La为空链表!endl; cout* 输出排序后的两个链表 *:endl; cout* La: ;print(La); cout* Lb: ;print(Lb); struct student *Lc=merge(La,Lb); cout* 输出合并后的链表 *:endl; cout* Lc: ;print(Lc); cout* 程序结束 谢谢使用 *endl; } // list.cpp #includeiostream.h #includemalloc.h #includeprocess.h #includeList.h #define NULL 0 #define LEN sizeof(struct student) //create a list struct student *create(int m) { cout输入节点数:; cinm; struct student *head; struct student *p1,*p2; if(m0) { cout输入错误,重新输入节点数: ; cinm; }

文档评论(0)

1亿VIP精品文档

相关文档