1、数据结构实验指导.数据结构实验指导书宁夏师范学院计算机教研室实验1线性表的抽象数据类型的实现实验目的1)掌握线性表的顺序存储结构和链式存储结构;2)熟练掌握顺序表和链表基本算法的实现;3)掌握利用线性表数据结构解决实际问题的方法和基本技巧;4)按照实验题目要求独立正确地完成实验内容(编写、调试算法程序,提交程序清单及及相关实验数据与运行结果);5)按时提交实验报告。实验环境计算机、C语言程序设计环境实验学时2学时,必做实验。实验内容一、顺序表的基本操作实现实验要求:数据元素类型ElemType取整型int。按照顺序存储结构实现如下算法(各算法边界条件和返回结果适当给出): 1)创建任意整数线性
2、表(即线性表的元素值随机在键盘上输入),长度限定在25之内; 2)打印(遍历)该线性表(依次打印出表中元素值); 3)在线性表中查找第i个元素,并返回其值; 4)在线性表中第i个元素之前插入一已知元素; 5)在线性表中删除第i个元素; 6)求线性表中所有元素值(整数)之和;二、链表(带头结点)基本操作实验要求:数据元素类型ElemType取字符型char。按照动态单循环链表结构实现如下算法(各算法边界条件适当给出): 1)创建任意字符型有序(递增排序)单循环链表(即链表的字符元素随机在键盘上输入),长度限定在15之内; 2)打印(遍历)该链表(依次打印出表中元素值); 3)在链表中查找第i个元
3、素,i合法返回元素值,否则,返回FALSE; 4)在链表中查找与一已知字符相同的第一个结点,有则返回TRUE,否则,返回FALSE; 5)在链表中按照有序方式插入一已知字符元素; 6)在线性表中删除第i个结点; 7)计算链表的长度。实验步骤一、顺序表的源程序#include#include#includeint list25;int i,n,a,sum=0,k,l;int eleminsert;/*-创建函数-*/void initlist()printf(Please input the total of the elems:);scanf(%d,&n);if(n25|n1) printf(
4、ERROE!);return;printf(Please input the elems:.n);for(i=0;in;i+) scanf(%d,&listi); return;/*-打印函数-*/void Print(int list,int n)int j;for(j=0;jn;j+)printf(%dt,listj);printf(n);return;/*-查找函数-*/int Search(int list,int n,int m)if(mn)printf(ERROR!n); return ;else printf(The elem is %d at %d placen,listm-1
5、,m);return;/*-插入函数-*/void Insert(int list,int n,int m,int elem)int j;if(mn)printf(ERROR!n); return ;for(j=n-1;j=m-1;i-)listj+1=listj;listm-1=elem;n=n+1;printf(The new list are: );Print(list,n);return;/*-删除函数-*/void Delete(int list,int n,int m)int q;int j;if(mn) printf(ERROR!n); return ;j=listm-1;for
6、(q=m-1;q=n;q+)listq=listq+1;printf(The new list are:);Print(list,n-1);free(j);return;/*-求和函数-*/void Sum(int list,int n,int sum)int j;for(j=0;jn;j+)sum=sum+listj;printf(The sum is :%d,sum);return;void menu()int j;/*-菜单函数-*/menulab:printf(* MENU *nn);printf(Create a new int list :.press 1nn);printf(Pr
7、int the whole list :.press 2nn);printf(Search by order :.press 3nn);printf(Insert the elem in the place i:.press 4nn);printf(Delete the elem by order :.press 6nn);printf(Sum all elem in the list :.press 7nn);printf(exit the programe :.press 0nn);printf(* END *nn);printf(Please choose the number from
8、 (07).);checklabel: scanf(%1d,&j);getchar();if(j7) printf(Error! Please choose again.); goto checklabel; printf(ntYou choose the number %dn ,j);printf(ntPress any key to continue.);getchar();clrscr(); /*clear screen*/switch(j) case 1:/*创建任意整数线性表*/ initlist(); clrscr(); /*clear screen*/ goto menulab;
9、case 2: /*打印(遍历)该线性表*/ printf(The original list is:); Print(list,n); printf(Press any key to continue.); getchar(); clrscr(); /*clear screen*/ goto menulab;case 3:/*在线性表中查找第i个元素,并返回其值*/ printf(Input which LNode you want to Search(Input number):); scanf(%d,&a); getchar(); Search(l,n,a); printf(Press
10、any key to continue.); getchar(); clrscr(); /*clear screen*/ goto menulab;case 4:/*在线性表中第i个元素之前插入一已知元素*/ printf(Please input the elems place where you want to insert); scanf(%d,&k); printf(Input the elem which you want to insert:); scanf(%d,&eleminsert); Insert(list,n,k,eleminsert); printf(Press any
11、 key to continue.); getchar(); clrscr(); /*clear screen*/ goto menulab;case 5:/*在线性表中删除第i个元素*/ printf(Please input the elem you want to delete:); scanf(%d,&l); n=n+1; Delete(list,n,l); n=n-1; printf(Press any key to continue.); getchar(); clrscr(); /*clear screen*/ goto menulab;case 6:/*求线性表中所有元素值(整
12、数)之和*/ Sum(list,n,sum); printf(Press any key to continue.); getchar(); clrscr(); /*clear screen*/ goto menulab;case0:/*退出程序*/ printf(Press any key to continue.); getchar(); exit(0); void main() void menu(); menu();二、链表(带头结点)的源程序#include#includestruct LNodechar elem;struct LNode* next;*l,*p,*new;int i,a,k,n;char c,s;/*-创建函数-*/void intilist(void) l=(struct LNode *)malloc(sizeof(struct LNode); l-next=NULL; clrscr(); printf(Input the total of the elems:.); scanf(%d,&n); getchar(); if(n15) printf(Error!); for(i=n;i0;i-) new=(struct
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1