1、数据结构课程实验报告CS1305U14927韩晓阳资料 课 程 实 验 报 告课程名称: 数 据 结 构 专业班级: CS1305 学 号: U201314927 姓 名: 韩晓阳 指导教师: 许贵平 报告日期: 2015年6月2日 计算机科学与技术学院1课程实验概述计算机科学是一门研究数据表示和数据处理的科学。数据是计算机化的信息,它是计算机可以直接处理的最基本和最重要的对象。无论是进行科学计算或数据处理、过程控制以及对文件的存储和检索及数据库技术应用等,都是对数据进行加工处理的过程。因此,要设计出一个结构好效率高的程序,必须研究数据的特性及数据间的相互关系及其对应的存储表示,并利用这些特性
2、和关系设计出相应的算法和程序。数据结构课程作为一门计算机专业的重要的专业基础课,内容丰富,涉及面广泛,它主要研究非线性数据的数据结构及其算法,它不仅是汇编语言、操作系统原理、编译原理、数据库原理等课程的前驱和基础课程,而且直接关系到软件设计水平的提高和专业素质的培养,在整个计算机专业体系中处于举足轻重的地位。通过课程实验,能够使我们加深对基本结构与操作算法以及并行数据结构和并行算法的理解,提高进行数据结构设计与算法设计能力,以及提高我们运用数据结构知识分析和解决实际问题的综合能力。本次课程实验着重于基本的数据类型以及基本数据类型的基本运算的熟练掌握,主要包括线性表的顺序存储结构、线性表的链式存
3、储结构、二叉树二叉链表存储结构以及遍历等基本操作。另外,通过整理实验资料,撰写规范的实验报告,培养良好习惯与技能。2 实验一 基于顺序结构的线性表实现2.1 实验内容与要求基于顺序存储结构,实现线性表ADT,具有10种基本运算。要求:提供一个实现功能的演示系统。 具体物理结构和数据元素类型自行选定。 线性表数据可以使用磁盘数据永久保存。2.2 程序概要设计 将实现各个具体功能的函数分别编写,主函数中利用菜单函数menu()提供简易菜单界面,利用switch语句实现函数status InitList、status DestroyList、status ClearList、status ListE
4、mpty、int ListLength、status GetElem、int LocatElem、status PriorElem、status NextElem、status ListInsert、status ListDelete、status ListTrabverse,在程序开始加入读取磁盘选项,程序结束加入保存入磁盘函数。2.3 数据结构与算法设计2.3.1 结构类型定义元素类型选择整形,结构类型定义如下:typedef struct int item1;Elemtype;线性表结构类型定义如下:typedef struct Elemtype * elem; int length;
5、int listsize;SqList;2.3.2 主函数算法设计主函数首先由用户端进行选择是否从磁盘文件端读入数据,之后进行人机交互菜单函数界面,利用switch语句,根据用户输入的op值,链接到各个执行功能函数中,当输入op为0时退出。另外在用户输入选择时,附加输入合法性判断,若非法要求用户重新输入。主函数设计参考图2.1。2.3.3 子函数算法设计初始化操作:用malloc函数分配LIST_INIT_SIZE个大小为Elemtype的空间,若分配成功,将表长置零,大小置为LIST_INIT_SIZE;若失败返回OVERFLOW。流程图如图2.2所示。销毁操作:参数合法性判断,若合法,释放
6、L-elem的空间,返回OK;否则返回ERROR。清空操作:参数合法性判断,若合法,依次调用销毁操作和初始化操作,返回OK;否则返回ERROR。判断表是否为空操作:判断表长L.length,如果为0返回TRUE,否则返回FALSE。求表长操作:直接返回表长L.length。取元素操作:参数合法性判断,若合法,即取的位置i不小于并且不大于表长,表不空时,用for语句到相应位置,赋值回主函数,返回OK;若参数非法,返回ERROR。流程图如图2.3所示。寻找操作:遍历线性表,如果找到了,返回下标位置,否则返回0。寻找先驱操作:先调用寻找操作,若i为0(表示无该元素)或者i为1(表示该元素在第一个位置
7、,无先驱),返回OVERFLOW,否则返回i-1号位置的元素值。流程图如图2.4所示。寻找后继操作:先调用寻找操作,若i为0(表示无该元素)或者i为表长(表示该元素在最后一个位置,无后继),返回OVERFLOW,否则返回i+1号位置的元素值。流程图如图2.5所示。插入操作:参数合法性判断,如果插入位置小于1且大于表长加一,则返回ERROR;如果表长此时以及大于或者等于L-listsize,需要使用realloc动态分配扩大空间,用for语句将插入位置元素后移,插入元素,表长加一,返回OK。流程图如图2.6所示。删除操作:参数合法性判断,如果插入位置小于1且大于表长加一,则返回ERROR;否则将
8、i位置元素赋值给e用于传递回到主函数,用for语句将删除位置元素前移,表长减一,返回OK。流程图如图2.7所示。遍历操作:依次遍历顺序表,输出即可。2.4 输入输出设计线性表初始磁盘数据设置为:L1:123,666,111L2:222,233程序利用case函数,根据用户端输入的对应功能序号实现各自功能。调试各个函数的输入输出结果见2.6程序测试与结果。2.5 源程序及注释#include #include #include #include #define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -
9、1#define OVERFLOW -2#define LIST_INIT_SIZE 30#define LISTINCREMENT 10typedef int status;typedef struct int item1;Elemtype;typedef struct Elemtype * elem; int length; int listsize;SqList;status InitList(SqList * L);status DestroyList(SqList * L);status ClearList(SqList *L);status ListEmpty(SqList L);
10、int ListLength(SqList L);status GetElem(SqList L,int i,Elemtype * e);int LocatElem(SqList L,Elemtype e,status (* compare)(Elemtype x,Elemtype y);status PriorElem(SqList L,Elemtype cur_e,Elemtype * pre_e);status NextElem(SqList L,Elemtype cur_e,Elemtype * next_e);status ListInsert(SqList * L, int i,E
11、lemtype e);status ListDelete(SqList * L, int i,Elemtype * e);status ListTrabverse(SqList L,void (* visit)(Elemtype e);/*-*/status equal(Elemtype x, Elemtype y);void display(Elemtype e);/*-*/void menu(void);/*-*/BOOL SaveListToFile(SqList *L1,SqList *L2);BOOL LoadList(SqList *L1,SqList *L2);int main(
12、void) SqList L1,L2; int op=0,i; int jud1,jud2; Elemtype e1,e2,a; Elemtype *pe1, *pe2; pe1=&e1; pe2=&e2; L1.elem=L2.elem=NULL; /status j; /* L1.elem=(Elemtype *) malloc(sizeof(Elemtype)*10); L1.length=4; L1.elem0.item1=1; L1.elem1.item1=2; L1.elem2.item1=3; L1.elem3.item1=4; */ printf( load from file
13、?n1 for yes and 0 for non); i=-1; scanf(%d,&i); while(i!=1&i!=0) printf(n Please input correctly!n again: ); scanf(%d,&i); if(i) LoadList(&L1,&L2); getchar();getchar(); do system(CLS); menu(); printf( Please input your option0-12: ); scanf(%d,&op); while(op12|op0) printf(Please input the correct opt
14、ion number again: ); scanf(%d,&op); switch(op) case 0: break; case 1: printf(n here is InitList(),which being realizedn); jud1=InitList(&L1); jud2=InitList(&L2); getchar();getchar(); if(jud1=1) printf(n InitList L1 successfully!n); else printf(n InitList L1 fail!n); if(jud2=1) printf(n InitList L2 s
15、uccessfully!n); else printf(n InitList L2 fail!n); getchar();getchar(); break; case 2: printf(n here is DestroyList(),which being realizedn); jud1=DestroyList(&L1); jud2=DestroyList(&L2); getchar();getchar(); if(jud1=1) printf(n Destroy L1 successfully!n); else printf(n Error: There is no L1 exists!
16、n); if(jud2=1) printf(n Destroy L2 successfully!n); else printf(n Error: There is no L2 exists!n); getchar();getchar(); break; case 3: printf(n here is ClearList(),which being realizedn); jud1=ClearList(&L1); jud2=ClearList(&L2); getchar();getchar(); if(jud1=1) printf(n Clear L1 successfully!n); els
17、e printf(n Clear L1 error!n); if(jud2=1) printf(n Clear L2 successfully!n); else printf(n Clear L2 error!n); getchar();getchar(); break; case 4: printf(n here is ListEmpty(),which being realizedn); getchar();getchar(); if(L1.elem = NULL) printf(n Error: There is no L1 exists!n); else jud1=ListEmpty(
18、L1); if(jud1=1) printf(n L1 is empty!n); else printf(n L1 is not emptyn); if(L2.elem = NULL) printf(n Error: There is no L2 exists!n); else jud2=ListEmpty(L2); if(jud2=1) printf(n L2 is empty!n); else printf(n L2 is not emptyn); getchar();getchar(); break; case 5: printf(n here is ListLength() ,whic
19、h being realizedn); jud1=ListLength(L1); jud2=ListLength(L2); getchar();getchar(); if(L1.elem=NULL) printf(n error!n); else printf(n the length of L1 is %dn,jud1); if(L2.elem=NULL) printf(n error!n); else printf(n the length of L2 is %dn,jud2); getchar();getchar(); break; case 6: printf(n here is Ge
20、tElem(),which being realizedn); printf(n please input the which number of you want to get: ); scanf(%d,&i); jud1=GetElem(L1,i,pe1); jud2=GetElem(L2,i,pe2); if(jud1=1) printf(n find it in L1, it is %d!n,e1.item1); else printf(n not find it in L1!n); if(jud2=1) printf(n find it in L2, it is %d!n,e2.it
21、em1); else printf(n not find it in L2!n); getchar();getchar(); break; case 7: printf(n here is LocatElem(),which being realizedn); printf(n Input the element you want to search: ); scanf(%d,&a.item1); if(L1.elem = NULL) printf(n error!n); else jud1 = LocatElem(L1,a,&equal); if(jud1) printf(n the ele
22、ment you find locates No.%d in L1n,jud1); else printf(n the element is not found!n); if(L2.elem = NULL) printf(n error!n); else jud2 = LocatElem(L2,a,&equal); if(jud2) printf(n the element you find locates No.%d in L2n,jud2); else printf(n the element is not found!n); getchar();getchar(); break; cas
23、e 8: printf(n here is PriorElem(),which being realizedn); printf(n Which list you want to use?n L1 or L2(Input 1 or 2): ); scanf(%d,&i); while(i!=1&i!=2) printf(n Please input again: ); scanf(%d,&i); if(i=1&L1.elem!=NULL) printf(n Input which element you want find its prior: ); scanf(%d,&a.item1); j
24、ud1=PriorElem(L1,a,&e1); if(jud1=1) printf(n Find the prior, it isn);display(e1); else printf(n Not find it!n); else if(i=2&L2.elem!=NULL) printf(n Input which element you want find its prior: ); scanf(%d,&a.item1); jud2=PriorElem(L2,a,&e2); if(jud2=1) printf(n Find the prior, it isn);display(e2); e
25、lse printf(n Not find it!n); else printf(n Error: This list is not existed!n); getchar();getchar(); break; case 9: printf(n here is NextElem(),which being realizedn); printf(n Which list you want to use?n L1 or L2(Input 1 or 2): ); scanf(%d,&i); while(i!=1&i!=2) printf(n Please input again: ); scanf
26、(%d,&i); if(i=1&L1.elem!=NULL) printf(n Input which element you want find its next: ); scanf(%d,&a.item1); jud1=NextElem(L1,a,&e1); if(jud1=1) printf(n Find the next, it isn);display(e1); else printf(n Not find it!n); else if(i=2&L2.elem!=NULL) printf(n Input which element you want find its next: );
27、 scanf(%d,&a.item1); jud2=NextElem(L2,a,&e2); if(jud2=1) printf(n Find the next, it isn);display(e2); else printf(n Not find it!n); else printf(n Error: This list is not existed!n); getchar();getchar(); break; case 10: printf(n here is ListInsert(),which being realizedn); i=-1; printf(n Which list you want to insert?n L1 or L2(Input 1 or 2): ); scanf(%d,&i); while(i!=1&i!=2) printf(n Please input again: ); scanf(%d,&i); if(i=1&L1
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1