算法与数据结构实验报告

上传人:ji****72 文档编号:27087606 上传时间:2018-01-07 格式:DOC 页数:44 大小:184KB
返回 下载 相关 举报
算法与数据结构实验报告_第1页
第1页 / 共44页
算法与数据结构实验报告_第2页
第2页 / 共44页
算法与数据结构实验报告_第3页
第3页 / 共44页
算法与数据结构实验报告_第4页
第4页 / 共44页
算法与数据结构实验报告_第5页
第5页 / 共44页
点击查看更多>>
资源描述

《算法与数据结构实验报告》由会员分享,可在线阅读,更多相关《算法与数据结构实验报告(44页珍藏版)》请在金锄头文库上搜索。

1、金陵科技学院实验报告学 生 实 验 报 告 册课程名称:算法与数据结构金陵科技学院实验报告实验项目名称: 顺序表 实验学时: 2 同组学生姓名: 实验地点: 工科楼 A205 实验日期: 2013 年 10 月 16 日 实验成绩: 批改教师: 批改时间: 金陵科技学院实验报告实验 1 顺序表一、实验目的和要求掌握顺序表的定位、插入、删除等操作。二、实验仪器和设备Turbo C 2.0三、实验内容与过程(含程序清单及流程图)1、必做题(1) 编写程序建立一个顺序表,并逐个输出顺序表中所有数据元素的值。编写主函数测试结果。(2) 编写顺序表定位操作子函数,在顺序表中查找是否存在数据元素 x。如果

2、存在,返回顺序表中和 x 值相等的第 1 个数据元素的序号(序号从 0 开始编号) ;如果不存在,返回 1。编写主函数测试结果。(3) 在递增有序的顺序表中插入一个新结点 x,保持顺序表的有序性。解题思路:首先查找插入的位置,再移位,最后进行插入操作;从第一个元素开始找到第一个大于该新结点值 x 的元素位置 i 即为插入位置;然后将从表尾开始依次将元素后移一个位置直至元素 i;最后将新结点 x 插入到 i 位置。(4) 删除顺序表中所有等于 X 的数据元素。2、选做题(5) 已知两个顺序表 A 和 B 按元素值递增有序排列,要求写一算法实现将 A 和 B 归并成一个按元素值递减有序排列的顺序表

3、(允许表中含有值相同的元素) 。程序清单:1、#define maxsize 100typedef structint datamaxsize;int last;sequenlist;main()int i;sequenlist l=2,5,6,8,2,8,4,3,7;printf(nThe list is:);金陵科技学院实验报告for(i=0;ix) break;for(j=l.last;j=i-1;j-)l.dataj+1=l.dataj;l.datai-1=x;l.last+;printf(the list after insertion is:n);for(j=0;jtypedef

4、int datattype;typedef struct nodechar data;struct node *next;linklist;main()char ch;linklist *head,*s,*r,*p;head=malloc(sizeof(linklist);金陵科技学院实验报告r=head;scanf(%c,while(ch!=$)s=malloc(sizeof(linklist);s-data=ch;r-next=s;r=s;scanf(%c,r-next=NULL;r=head-next;while(r!=NULL)printf(%c,r-data);r=r-next;2、

5、#include stdio.h#include stdlib.htypedef struct nodeint data;struct node *next;linklist;main()int x,y;linklist *head,*s,*r,*p,*q,*m,*n;clrscr();head=malloc(sizeof(linklist);r=head;printf(input the order numbers :);scanf(%d,while(x!=0)s=malloc(sizeof(linklist);s-data=x;r-next=s;r=s;scanf(%d,金陵科技学院实验报

6、告r-next=NULL;printf(Please input the insert value:);scanf(%d,p=head-next;while(p!=NULL)if (p-datanext;else break;q=malloc(sizeof(linklist);q-data=y;m=head;while(m-next!=p) m=m-next;q-next=p;m-next=q;n=head-next;printf(the list are:);while(n!=NULL)printf(%3d,n-data);n=n-next;3、#include stdio.h#includ

7、e stdlib.htypedef struct nodeint data;struct node *next;linklist;main()int a;linklist *head,*s,*r,*p,*q,*t;clrscr();head=malloc(sizeof(linklist);r=head;printf(Input some numbers:);scanf(%d,金陵科技学院实验报告while(a!=0)s=malloc(sizeof(linklist);s-data=a;r-next=s;r=s;scanf(%d,r-next=NULL;printf(n The linklist

8、 before changed is:n );p=head-next;while(p)printf(%d,p-data);p=p-next;p=head-next;q=p-next;while(q!=NULL)t=q-next;q-next=p;p=q;q=t;head-next-next=NULL;head-next=p;printf(nAfter changed:n);p=head-next;while(p!=NULL)printf(%d,p-data);p=p-next;四、实验结果与分析(程序运行结果及其分析)1、输入:1 2 3 a b c $输出结果:1 2 3 a b c 2、输

9、入:input the order numbers : 1 3 5 7 8 9 0金陵科技学院实验报告Please input the insert value::4输出结果:the list are: 1 3 4 5 7 8 93、输入:Input some numbers:1 3 4 5 8 0 输出结果:The linklist before changed is:13458After changed:85431五、实验体会(遇到问题及解决办法,编程后的心得体会)遇到问题:编写成功后运行时,没有加入$导致程序运行不成功,不能够退出。后注意到这个问题才继续运行下去。实验体会:在编写程序时,

10、设置了结束字符一定要牢牢记住,并且在输入时观察仔细类型是什么,以及是否是输入一串有顺序的数字,编写成功不难,但是要规范格式,不能仅仅以完成程序为目的。而完成这一章的实验也让我了解了,顺序表便于查找不便于插入删除,而链表恰恰相反,链表的插入删除只需要移动指针,而顺序表要从后往前依次移动,二者各有优劣。金陵科技学院实验报告实验项目名称: 堆栈和队列 实验学时: 2 同组学生姓名: 实验地点: 工科楼 A205 实验日期: 2013 年 10 月 30 日 实验成绩: 批改教师: 批改时间: 金陵科技学院实验报告实验 3 堆栈和队列一、实验目的和要求(1)掌握应用栈解决问题的方法。(2)掌握利用栈进

11、行表达式求和的算法。(3)掌握队列的存储结构及基本操作实现,并能在相应的应用问题中正确选用它们。二、实验仪器和设备Turbo C 2.0三、实验内容与过程(含程序清单及流程图)1、必做题(1) 判断一个算术表达式中开括号和闭括号是否配对。(2) 测试“汉诺塔”问题。(3) 假设称正读和反读都相同的字符序列为”回文”,试写一个算法判别读入的一个以为结束符的字符序列是否是“回文” 。2、选做题在顺序存储结构上实现输出受限的双端循环队列的入列和出列算法。设每个元素表示一个待处理的作业,元素值表示作业的预计时间。入队列采取简化的短作业优先原则,若一个新提交的作业的预计执行时间小于队头和队尾作业的平均时

12、间,则插入在队头,否则插入在队尾。程序清单:1、typedef int datatype;#define M 100typedef structchar dataM;int top; seqstack;main()char strM;int result=0,i=0;seqstack s;s.top=0;gets(str);while(stri!=0)金陵科技学院实验报告if(stri=()s.top+;s.datas.top=(;if(stri=)if(s.top=0)result=1;break;else s.top-;i+;if(result=0 else if(result=1) pr

13、intf(Missing left!n);else if(s.top0) printf(Missing right!n);2、#includevoid hanoi(int n,char a,char b,char c)if(n=1)printf(n Move disk %d from pile %c to pile %c,n,a,c);elsehanoi(n-1,a,c,b);printf(n Move disk %d from pile %c to pile %c,n,a,c);hanoi(n-1,b,a,c);void main()int n;clrscr();printf(n Pleas

14、e enter the number of disks to be moved:);scanf(%d,hanoi(n,A,B,C);3、#include金陵科技学院实验报告#define M 100typedef structchar dataM;int top;seqstack;main()char strM;int i=0,n;seqstack s;s.top=0;gets(str);while(stri!=) i+;if(i=1)printf(Yesn);return;n=i;for(i=0;i=s.curlen)printf(Not find!n);2、#define maxsize 100typedef structchar chmaxsize;int curlen;seqstring;main()int i,flag=0;char ch;seqstring s=abadeag,6;for(i=0;i#includetypedef struct linknodechar data;struct linknode *next;linkstring;金陵科技学院实验报告main()linkstring *head,*s,*r,*p,*q;int i,b,l,k=0;char ch;head=NULL;r=NULL;printf(n Ne

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

当前位置:首页 > 行业资料 > 其它行业文档

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