算法与数据结构实验报告_第1页
算法与数据结构实验报告_第2页
算法与数据结构实验报告_第3页
算法与数据结构实验报告_第4页
算法与数据结构实验报告_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

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

3、int 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;i<=l.last;i+) printf("%2d",l.datai);2、#define maxsize 100typedef struct int datamaxsize; int last;sequenlist;main() int x,i,s=-1; sequenlist l=2,5,6,7,9,8,4,3,7;

4、 printf("nThe list is:"); for(i=0;i<=l.last;i+) printf("%2d",l.datai); printf("nPlease input the number :"); scanf("%d",&x); for(i=0;i<=l.last;i+) if(l.datai=x) s=i;break; printf("%d",s);3、#define maxsize 100typedef struct int datamaxsize;

5、int last;sequenlist;main() int i,x,j; sequenlist l=1,3,5,6,7,9,5; printf("nThe list is:"); for(i=0;i<=l.last;i+) printf("%2d",l.datai); printf("nInput the insert number:"); scanf("%d",&x); for(i=1;i<=l.last;i+) if(l.datai-1>x) break; for(j=l.last;

6、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;j<=l.last;j+) printf("%3d",l.dataj);4、#define maxsize 100typedef struct int datamaxsize; int last;sequenlist;main() int i,j,x=0,k=0; sequenlist L=1,3,5,7,2,4,6,8,2,9,9; prin

7、tf("n The list is:"); for(i=0;i<=L.last;i+) printf("%3d",L.datai); printf("nPlease input a number x:"); scanf("%d",&x); for(i=1;i<=L.last+1;i+) if(L.datai-1=x) for(j=i;j<=L.last+1;j+) L.dataj-1=L.dataj; L.last-; i-; k=1; if(k=1) printf("The l

8、ist after deletion is:n"); for(j=0;j<=L.last;j+) printf("%3d",L.dataj); else printf("Not found!n");四、实验结果与分析(程序运行结果及其分析)1、输出结果:The list is: 2 5 6 8 2 8 4 32、输出结果:The list is: 2 5 6 7 9 8 4 3 Please input the number:8 5The list is: 2 5 6 7 9 8 4 3 Please input the number:1

9、 -13、输出结果:The list is: 1 3 5 6 7 9 Input the insert number:8 The list after insertion is: 1 3 5 6 7 8 94、输出结果:The list is: 1 3 5 7 2 4 6 8 2 9 Please input a number x:5 The list after deletion is: 1 3 7 2 4 6 8 2 9 The list is: 1 3 5 7 2 4 6 8 2 9 Please input a number x:11 Not found!五、实验体会(遇到问题及解决办

10、法,编程后的心得体会)遇到问题:读取数据元素时,误将=写成=,导致错误。实验过程中,顺序表的赋值没有弄懂,以致输出多个0或者少输出。格式运算符也要正确控制,否则系统会停止工作。实验体会:通过实验掌握了顺序表的基本操作,如初始化、插入、读取元素、删除等等。并了解到线性表顺序存储结构的特点,即逻辑关系上相邻的两个元素在物理位置上也相邻,然而从另一方面来看,在做插入和删除时需要移动大量元素。本次实验基本完成了实验要求的目的,顺序表的初始化,定义,插入,查找等,更好的了解了顺序表基本操作的算法,以及更直观的了解了数据结构在C语言环境下的体现。实验项目名称: 单链表 实验学时: 2 同组学生姓名: 实验

11、地点: 工科楼A205 实验日期: 2013年10月23日 实验成绩: 批改教师: 批改时间: 实验2 单链表一、实验目的和要求1、实验目的掌握单链表的定位、插入、删除等操作。2、实验要求(1)注意链表的空间是动态分配的,某结点不用之后要及时进行物理删除,以便释放其内存空间。(2)链表不能实现直接定位,一定注意指针的保存,防止丢失。二、实验仪器和设备Turbo C 2.0三、实验内容与过程(含程序清单及流程图)1、必做题(1) 编写程序建立一个单链表,并逐个输出单链表中所有数据元素。(2) 在递增有序的单链表中插入一个新结点x,保持单链表的有序性。解题思路:首先查找插入的位置然后进行插入操作;

12、从第一个结点开始找到第一个大于该新结点值的结点即为插入位置;然后在找到的此结点之前插入新结点;注意保留插入位置之前结点的指针才能完成插入操作。(3) 编写实现带头结点单链表就地逆置的子函数,并编写主函数测试结果。2、选做题已知指针LA和LB分别指向两个无头结点单链表的首元结点。要求编一算法实现,从表LA中删除自第i个元素起共len个元素后,将它们插入到表LB中第j个元素之前。程序清单:1、#include<stdlib.h>typedef int datattype;typedef struct node char data; struct node *next;linklist;

13、main() char ch;linklist *head,*s,*r,*p; head=malloc(sizeof(linklist); r=head; scanf("%c",&ch); while(ch!='$') s=malloc(sizeof(linklist); s->data=ch; r->next=s; r=s; scanf("%c",&ch); r->next=NULL; r=head->next; while(r!=NULL) printf("%c",r->

14、;data); r=r->next; 2、#include "stdio.h"#include "stdlib.h"typedef struct node int 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&qu

15、ot;,&x); while(x!=0) s=malloc(sizeof(linklist); s->data=x; r->next=s; r=s; scanf("%d",&x); r->next=NULL; printf("Please input the insert value:"); scanf("%d",&y); p=head->next; while(p!=NULL) if (p->data<y) p=p->next; else break; q=mallo

16、c(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"#include "stdlib.h"typedef struct node

17、 int 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",&a); while(a!=0) s=malloc(sizeof(linklist); s->data=a; r->next=s; r=s; scanf("%d",&

18、a); r->next=NULL; printf("n The linklist 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("nAft

19、er 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、输入:input the order numbers : 1 3 5 7 8 9 0Please input the insert value::4 输出结果:the list are: 1 3 4 5 7 8 93、输入:Input some numbers:1 3 4 5 8

20、 0 输出结果:The linklist before changed is: 13458After changed: 85431五、实验体会(遇到问题及解决办法,编程后的心得体会)遇到问题:编写成功后运行时,没有加入$导致程序运行不成功,不能够退出。后注意到这个问题才继续运行下去。实验体会:在编写程序时,设置了结束字符一定要牢牢记住,并且在输入时观察仔细类型是什么,以及是否是输入一串有顺序的数字,编写成功不难,但是要规范格式,不能仅仅以完成程序为目的。而完成这一章的实验也让我了解了,顺序表便于查找不便于插入删除,而链表恰恰相反,链表的插入删除只需要移动指针,而顺序表要从后往前依次移动,二者各

21、有优劣。实验项目名称: 堆栈和队列 实验学时: 2 同组学生姓名: 实验地点: 工科楼A205 实验日期: 2013年10月30日 实验成绩: 批改教师: 批改时间: 实验3 堆栈和队列一、实验目的和要求(1)掌握应用栈解决问题的方法。(2)掌握利用栈进行表达式求和的算法。(3)掌握队列的存储结构及基本操作实现,并能在相应的应用问题中正确选用它们。二、实验仪器和设备Turbo C 2.0三、实验内容与过程(含程序清单及流程图)1、必做题(1) 判断一个算术表达式中开括号和闭括号是否配对。(2) 测试“汉诺塔”问题。(3) 假设称正读和反读都相同的字符序列为”回文”,试写一个算法判别读入的一个以

22、为结束符的字符序列是否是“回文”。2、选做题在顺序存储结构上实现输出受限的双端循环队列的入列和出列算法。设每个元素表示一个待处理的作业,元素值表示作业的预计时间。入队列采取简化的短作业优先原则,若一个新提交的作业的预计执行时间小于队头和队尾作业的平均时间,则插入在队头,否则插入在队尾。程序清单:1、typedef int datatype;#define M 100typedef struct char dataM; int top; seqstack;main() char strM; int result=0,i=0; seqstack s; s.top=0; gets(str); whi

23、le(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 && s.top=0) printf("Match!n"); else if(result=1) printf("Missing left!n"); else if(s.top>0) printf("Missing righ

24、t!n");2、#include<stdio.h>void 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); else hanoi(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

25、Please enter the number of disks to be moved:"); scanf("%d",&n); hanoi(n,'A','B','C');3、#include<stdio.h>#define M 100typedef struct char dataM; int top;seqstack;main() char strM; int i=0,n; seqstack s; s.top=0; gets(str); while(stri!='') i+;

26、if(i=1) printf("Yesn"); return; n=i; for(i=0;i<n/2;i+) s.top+; s.datas.top=stri; i=i-1; if(n%2=0) i+; else i=i+2; while(i<n && s.datas.top=stri) i+; s.top-; if(i=n) printf("Yesn"); else printf("Non");四、实验结果与分析(程序运行结果及其分析)1、输入:(a)输出结果:Match!输入:(a输出结果:Missin

27、g right!输入:a)输出结果:Missing left!2、输入:3 输出结果:Move disk 1 from pile A to pile CMove disk 2 from pile A to pile BMove disk 1 from pile C to pile BMove disk 3 from pile A to pile CMove disk 1 from pile B to pile AMove disk 2 from pile B to pile CMove disk 1 from pile A to pile C3、输入:qwewq 输出结果:Yes 输入:qwe

28、rewr 输出结果No五、实验体会(遇到问题及解决办法,编程后的心得体会)遇到问题:在本章栈和队列中编程,有许多的if语句,编写时一不小心就会少加一个花括号,以致编写不成功。在本章汉诺塔问题中,使用了栈以及函数的递归,这其中的过程一直不是很了解,在经过老师的讲解后,理解了大致过程。实验体会:递归函数是编程中经常会用到的一种函数,它可以实现许多我们在平时言语和解释上解决不了的问题,我们需要理解并且可以熟练的使用它,这对我们之后的编程会有很大的帮助。而汉诺塔利用栈是一种很经典的递归的算法,这让我们理解栈和递归。实验项目名称: 串 实验学时: 2 同组学生姓名: 实验地点: 工科楼A205实验日期:

29、 2013年11月6日 实验成绩: 批改教师: 批改时间: 实验4 串一、实验目的和要求掌握串的存储及应用。二、实验仪器和设备Turbo C 2.0三、实验内容与过程(含程序清单及流程图)1、必做题(1) 编写输出字符串s中值等于字符ch的第一个字符的函数,并用主函数测试结果。(2) 编写输出字符串s中值等于字符ch的所有字符的函数,并用主函数测试结果。解题思路:可以将第一题程序改进成一个子函数,在本题中循环调用。(3) 设字符串采用单字符的链式存储结构,编程删除串s从位置i开始长度为k的子串。2、选做题假设以链结构表示串,编写算法实现将串S插入到串T中某个字符之后,若串T中不存在这个字符,则

30、将串S联接在串T的末尾。提示:为提高程序的通用性,插入位置字符应设计为从键盘输入。程序清单:1、#define maxsize 100typedef struct char chmaxsize; int curlen;seqstring;main() int i; char ch; seqstring s="asdfghg",6; for(i=0;i<s.curlen;i+) printf("%c",s.chi); printf("nPlease input aa character ch:"); scanf("%c&

31、quot;,&ch); for(i=0;i<s.curlen;i+) if(s.chi=ch) printf("ch=s.ch%d=%cn",i,s.chi); break; if(i>=s.curlen) printf("Not find!n");2、#define maxsize 100typedef struct char chmaxsize; int curlen;seqstring;main() int i,flag=0; char ch; seqstring s="abadeag",6; for(i=0

32、;i<s.curlen;i+) printf("%c",s.chi); printf("nPlease input aa character ch:"); scanf("%c",&ch); for(i=0;i<s.curlen;i+) if(s.chi=ch) printf("ch=s.ch%d=%cn",i,s.chi); flag+; if(flag=0) printf("Not find!n");3、#include<stdio.h>#include<

33、stdlib.h>typedef struct linknode char 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 Next to creat LinkString,$ as end markn"); ch=getchar(); while(ch!='$') s=malloc(sizeof(linkstring); s->data=c

34、h; if(head=NULL) head=s; else r->next=s; r=s; ch=getchar(); if(r!=NULL) r->next=NULL; q=head; while(q) printf("%c",q->data); q=q->next; k+; printf("n Now input two int for stratpostion and length for deleted:"); scanf("%d %d",&b,&l); if(b>k-1|b+l&

35、gt;k) printf("Error!n"); return; p=head; for(i=0;i<b-1;i+) p=p->next; printf("%c %d %d %d n",p->data,b,l,k); for(i=b-1;i<b+l-1;i+) q=p->next;p->next=q->next;free(q); q=head; while(q) printf("%c",q->data); q=q->next; printf("n");四、实验结

36、果与分析(程序运行结果及其分析)1、输入:s 输出结果:ch=s.ch1=s2、输入:a 输出结果:ch=s.ch0=a ch=s.ch2=a ch=s.ch5=a3、输入:asdfghjkl$ 2 5 输出结果:s 2 5 9 askl五、实验体会(遇到问题及解决办法,编程后的心得体会)实验体会:本章第一题可以作为第二题的子函数,使用调用;也可以从开头查找对应的字符到结尾,最后全部输出。前两题使用顺序串,后面一题是链串。串的存储结构包含有顺序存储结构和链式存储结构。在串的顺序存储结构中,表示串的长度通常有两种方法:一种方法是设置一个串的长度参数,其优点在于便于在算法中用长度参数控制循环过程;

37、另一种方法是在串值得末尾添加结束标记,此种方法的优点在于便于系统自动实现。在串的存储过程中,串值用双引号引起来,系统将自动在串值得末尾添加结束标记0字符。实验项目名称: 二叉树 实验学时: 2 同组学生姓名: 实验地点: 工科楼A205 实验日期: 2013年11月13日 实验成绩: 批改教师: 批改时间: 实验5 二叉树一、实验目的和要求(1)掌握二叉树的生成,以及前、中、后序遍历算法。(2)掌握应用二叉树递归遍历思想解决问题的方法。二、实验仪器和设备Turbo C 2.0三、实验内容与过程(含程序清单及流程图)1、必做题(1) 建立一棵二叉树。对此树进行前序遍历、中序遍历及后序遍历,输出遍

38、历序列。(2) 在第一题基础上,求二叉树中叶结点的个数。(3) 在第一题基础上,求二叉树中结点总数。(4) 在第一题基础上,求二叉树的深度。2、选做题已知一棵完全二叉树存于顺序表sa中,sa.elem1sa.last存储结点的值。试编写算法由此顺序存储结构建立该二叉树的二叉链表。解题思路:根据完全二叉树顺序存储的性质来确定二叉树的父子关系即“还原”了二叉树,之后再按照二叉树二叉链表的构造方法进行建立。完全二叉树顺序存储的一个重要性质为,第i个结点的左孩子是编号为2i的结点,第i个结点的右孩子是编号为2i+1的结点。程序清单:1(1)#include<stdio.h>#include

39、<stdlib.h>#define maxsize 100typedef struct node char data; struct node *lchild,*rchild;bitree;bitree *Qmaxsize;bitree *Creatree() char ch; int front,rear; bitree *root,*s; root=NULL;front=1;rear=0; printf("Now Creat the bitree,input baseing the order top to bottom,left to right:n");

40、 ch=getchar(); while(ch!='#') s=NULL; if(ch!='') s=malloc(sizeof(bitree); s->data=ch; s->lchild=NULL; s->rchild=NULL; rear+; Qrear=s; if(rear=1) root=s; else if(s && Qfront)if(rear%2=0) Qfront->lchild=s;else Qfront->rchild=s; if(rear%2=1) front+; ch=getchar();

41、return root;void preorder(t)bitree *t; if(t) printf("%c",t->data); preorder(t->lchild); preorder(t->rchild); void inorder(t)bitree *t; if(t) inorder(t->lchild); printf("%c",t->data); inorder(t->rchild); void postorder(t)bitree *t; if(t) postorder(t->lchild);

42、postorder(t->rchild); printf("%c",t->data); main() bitree *root; clrscr(); root=Creatree(); printf("preorder is:");preorder(root); printf("n"); printf("inorder is:");inorder(root); printf("n"); printf("postorder is:");postorder(root);

43、 printf("n");(2)#include<stdio.h>#include<stdlib.h>#define maxsize 100typedef struct node char data; struct node *lchild,*rchild;bitree;bitree *Qmaxsize;bitree *Creatree() char ch; int front,rear; bitree *root,*s; root=NULL;front=1;rear=0; printf("Now Creat the bitree,inpu

44、t baseing the order top to bottom,left to right:n"); ch=getchar(); while(ch!='#') s=NULL; if(ch!='') s=malloc(sizeof(bitree); s->data=ch; s->lchild=NULL; s->rchild=NULL; rear+; Qrear=s; if(rear=1) root=s; else if(s && Qfront)if(rear%2=0) Qfront->lchild=s;else

45、 Qfront->rchild=s; if(rear%2=1) front+; ch=getchar(); return root;int left(bitree *t) int num1,num2; if(t=NULL) return 0; else if(t->lchild=NULL && t->rchild=NULL) return 1; else num1=left(t->lchild); num2=left(t->rchild); return(num1+num2); main() bitree *root; clrscr(); root

46、=Creatree(); printf("lefts is %dn",left(root);(3)#include<stdio.h>#include<stdlib.h>#define maxsize 100typedef struct node char data; struct node *lchild,*rchild;bitree;bitree *Qmaxsize;bitree *Creatree() char ch; int front,rear; bitree *root,*s; root=NULL;front=1;rear=0; print

47、f("Now Creat the bitree,input baseing the order top to bottom,left to right:n"); ch=getchar(); while(ch!='#') s=NULL; if(ch!='') s=malloc(sizeof(bitree); s->data=ch; s->lchild=NULL; s->rchild=NULL; rear+; Qrear=s; if(rear=1) root=s; else if(s && Qfront)if(r

48、ear%2=0) Qfront->lchild=s;else Qfront->rchild=s; if(rear%2=1) front+; ch=getchar(); return root;int nodes(bitree *t) int num1,num2; if(t=NULL) return 0; else if(t->lchild=NULL &&t->rchild=NULL) return 1; else num1=nodes(t->lchild); num2=nodes(t->rchild); return (num1+num2+1

49、); main() bitree *root; clrscr(); root=Creatree(); printf("nodes is %dn",nodes(root);(4)#include<stdio.h>#include<stdlib.h>#define maxsize 100typedef struct node char data; struct node *lchild,*rchild;bitree;bitree *Qmaxsize;bitree *Creatree() char ch; int front,rear; bitree *r

50、oot,*s; root=NULL;front=1;rear=0; printf("Now Creat the bitree,input baseing the order top to bottom,left to right:n"); ch=getchar(); while(ch!='#') s=NULL; if(ch!='') s=malloc(sizeof(bitree); s->data=ch; s->lchild=NULL; s->rchild=NULL; rear+; Qrear=s; if(rear=1) r

51、oot=s; else if(s && Qfront)if(rear%2=0) Qfront->lchild=s;else Qfront->rchild=s; if(rear%2=1) front+; ch=getchar(); return root;int depth(bitree *t) int dep1,dep2; if(t=NULL) return 0; else dep1=depth(t->lchild); dep2=depth(t->rchild); if(dep1>dep2) return (dep1+1); else return

52、(dep2+1); main() bitree *root; clrscr(); root=Creatree(); printf("depth is %dn",depth(root);四、实验结果与分析(程序运行结果及其分析)(1)Now Creat the bitree,input baseing the order top to bottom,left to right:abc#preorder is:abcinorder is:abcpostorder is:cba(2)Now Creat the bitree,input baseing the order top to bottom,left to right:abc#lefts is 1(3)Now Creat the bitre

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论