数据结构课程实验报告CS1305U14927韩晓阳资料.docx

上传人:b****2 文档编号:23553243 上传时间:2023-05-18 格式:DOCX 页数:97 大小:607.61KB
下载 相关 举报
数据结构课程实验报告CS1305U14927韩晓阳资料.docx_第1页
第1页 / 共97页
数据结构课程实验报告CS1305U14927韩晓阳资料.docx_第2页
第2页 / 共97页
数据结构课程实验报告CS1305U14927韩晓阳资料.docx_第3页
第3页 / 共97页
数据结构课程实验报告CS1305U14927韩晓阳资料.docx_第4页
第4页 / 共97页
数据结构课程实验报告CS1305U14927韩晓阳资料.docx_第5页
第5页 / 共97页
点击查看更多>>
下载资源
资源描述

数据结构课程实验报告CS1305U14927韩晓阳资料.docx

《数据结构课程实验报告CS1305U14927韩晓阳资料.docx》由会员分享,可在线阅读,更多相关《数据结构课程实验报告CS1305U14927韩晓阳资料.docx(97页珍藏版)》请在冰豆网上搜索。

数据结构课程实验报告CS1305U14927韩晓阳资料.docx

数据结构课程实验报告CS1305U14927韩晓阳资料

课程实验报告

课程名称:

数据结构

专业班级:

CS1305

学号:

U201314927

姓名:

韩晓阳

指导教师:

许贵平

报告日期:

2015年6月2日

计算机科学与技术学院

1课程实验概述

计算机科学是一门研究数据表示和数据处理的科学。

数据是计算机化的信息,它是计算机可以直接处理的最基本和最重要的对象。

无论是进行科学计算或数据处理、过程控制以及对文件的存储和检索及数据库技术应用等,都是对数据进行加工处理的过程。

因此,要设计出一个结构好效率高的程序,必须研究数据的特性及数据间的相互关系及其对应的存储表示,并利用这些特性和关系设计出相应的算法和程序。

《数据结构》课程作为一门计算机专业的重要的专业基础课,内容丰富,涉及面广泛,它主要研究非线性数据的数据结构及其算法,它不仅是《汇编语言》、《操作系统原理》、《编译原理》、《数据库原理》等课程的前驱和基础课程,而且直接关系到软件设计水平的提高和专业素质的培养,在整个计算机专业体系中处于举足轻重的地位。

通过课程实验,能够使我们加深对基本结构与操作算法以及并行数据结构和并行算法的理解,提高进行数据结构设计与算法设计能力,以及提高我们运用数据结构知识分析和解决实际问题的综合能力。

本次课程实验着重于基本的数据类型以及基本数据类型的基本运算的熟练掌握,主要包括线性表的顺序存储结构、线性表的链式存储结构、二叉树二叉链表存储结构以及遍历等基本操作。

另外,通过整理实验资料,撰写规范的实验报告,培养良好习惯与技能。

 

2实验一基于顺序结构的线性表实现

2.1实验内容与要求

基于顺序存储结构,实现线性表ADT,具有10种基本运算。

要求:

⑴提供一个实现功能的演示系统。

⑵具体物理结构和数据元素类型自行选定。

⑶线性表数据可以使用磁盘数据永久保存。

2.2程序概要设计

将实现各个具体功能的函数分别编写,主函数中利用菜单函数menu()提供简易菜单界面,利用switch语句实现函数statusInitList、statusDestroyList、statusClearList、statusListEmpty、intListLength、statusGetElem、intLocatElem、statusPriorElem、statusNextElem、statusListInsert、statusListDelete、statusListTrabverse,在程序开始加入读取磁盘选项,程序结束加入保存入磁盘函数。

2.3数据结构与算法设计

2.3.1结构类型定义

元素类型选择整形,结构类型定义如下:

typedefstruct{

intitem1;

}Elemtype;

线性表结构类型定义如下:

typedefstruct{

Elemtype*elem;

intlength;

intlistsize;

}SqList;

2.3.2主函数算法设计

主函数首先由用户端进行选择是否从磁盘文件端读入数据,之后进行人机交互菜单函数界面,利用switch语句,根据用户输入的op值,链接到各个执行功能函数中,当输入op为0时退出。

另外在用户输入选择时,附加输入合法性判断,若非法要求用户重新输入。

主函数设计参考图2.1。

2.3.3子函数算法设计

初始化操作:

用malloc函数分配LIST_INIT_SIZE个大小为Elemtype的空间,若分配成功,将表长置零,大小置为LIST_INIT_SIZE;若失败返回OVERFLOW。

流程图如图2.2所示。

销毁操作:

参数合法性判断,若合法,释放L->elem的空间,返回OK;否则返回ERROR。

清空操作:

参数合法性判断,若合法,依次调用销毁操作和初始化操作,返回OK;否则返回ERROR。

判断表是否为空操作:

判断表长L.length,如果为0返回TRUE,否则返回FALSE。

求表长操作:

直接返回表长L.length。

取元素操作:

参数合法性判断,若合法,即取的位置i不小于并且不大于表长,表不空时,用for语句到相应位置,赋值回主函数,返回OK;若参数非法,返回ERROR。

流程图如图2.3所示。

寻找操作:

遍历线性表,如果找到了,返回下标位置,否则返回0。

寻找先驱操作:

先调用寻找操作,若i为0(表示无该元素)或者i为1(表示该元素在第一个位置,无先驱),返回OVERFLOW,否则返回i-1号位置的元素值。

流程图如图2.4所示。

寻找后继操作:

先调用寻找操作,若i为0(表示无该元素)或者i为表长(表示该元素在最后一个位置,无后继),返回OVERFLOW,否则返回i+1号位置的元素值。

流程图如图2.5所示。

插入操作:

参数合法性判断,如果插入位置小于1且大于表长加一,则返回ERROR;如果表长此时以及大于或者等于L->listsize,需要使用realloc动态分配扩大空间,用for语句将插入位置元素后移,插入元素,表长加一,返回OK。

流程图如图2.6所示。

删除操作:

参数合法性判断,如果插入位置小于1且大于表长加一,则返回ERROR;否则将i位置元素赋值给e用于传递回到主函数,用for语句将删除位置元素前移,表长减一,返回OK。

流程图如图2.7所示。

遍历操作:

依次遍历顺序表,输出即可。

2.4输入输出设计

线性表初始磁盘数据设置为:

L1:

123,666,111

L2:

222,233

程序利用case函数,根据用户端输入的对应功能序号实现各自功能。

调试各个函数的输入输出结果见2.6程序测试与结果。

2.5源程序及注释

#include

#include

#include

#include

#defineTRUE1

#defineFALSE0

#defineOK1

#defineERROR0

#defineINFEASIBLE-1

#defineOVERFLOW-2

#defineLIST_INIT_SIZE30

#defineLISTINCREMENT10

typedefintstatus;

typedefstruct{

intitem1;

}Elemtype;

typedefstruct{

Elemtype*elem;

intlength;

intlistsize;

}SqList;

statusInitList(SqList*L);

statusDestroyList(SqList*L);

statusClearList(SqList*L);

statusListEmpty(SqListL);

intListLength(SqListL);

statusGetElem(SqListL,inti,Elemtype*e);

intLocatElem(SqListL,Elemtypee,status(*compare)(Elemtypex,Elemtypey));

statusPriorElem(SqListL,Elemtypecur_e,Elemtype*pre_e);

statusNextElem(SqListL,Elemtypecur_e,Elemtype*next_e);

statusListInsert(SqList*L,inti,Elemtypee);

statusListDelete(SqList*L,inti,Elemtype*e);

statusListTrabverse(SqListL,void(*visit)(Elemtypee));

/*------------------------------------------------------*/

statusequal(Elemtypex,Elemtypey);

voiddisplay(Elemtypee);

/*------------------------------------------------------*/

voidmenu(void);

/*------------------------------------------------------*/

BOOLSaveListToFile(SqList*L1,SqList*L2);

BOOLLoadList(SqList*L1,SqList*L2);

intmain(void){

SqListL1,L2;

intop=0,i;

intjud1,jud2;

Elemtypee1,e2,a;

Elemtype*pe1,*pe2;

pe1=&e1;

pe2=&e2;

L1.elem=L2.elem=NULL;

//statusj;

/*

L1.elem=(Elemtype*)malloc(sizeof(Elemtype)*10);

L1.length=4;

L1.elem[0].item1=1;

L1.elem[1].item1=2;

L1.elem[2].item1=3;

L1.elem[3].item1=4;

*/

printf("loadfromfile?

\n1foryesand0forno\n");

i=-1;

scanf("%d",&i);

while(i!

=1&&i!

=0)

{

printf("\nPleaseinputcorrectly!

\nagain:

");

scanf("%d",&i);

}

if(i)

LoadList(&L1,&L2);

getchar();getchar();

do{

system("CLS");

menu();

printf("Pleaseinputyouroption[0-12]:

");

scanf("%d",&op);

while(op>12||op<0)

{

printf("Pleaseinputthecorrectoptionnumberagain:

");

scanf("%d",&op);

}

 

switch(op){

case0:

break;

case1:

{

printf("\nhereisInitList(),whichbeingrealized\n");

jud1=InitList(&L1);

jud2=InitList(&L2);

getchar();getchar();

if(jud1==1)

printf("\nInitListL1successfully!

\n");

else

printf("\nInitListL1fail!

\n");

if(jud2==1)

printf("\nInitListL2successfully!

\n");

else

printf("\nInitListL2fail!

\n");

getchar();getchar();

break;

}

case2:

{

printf("\nhereisDestroyList(),whichbeingrealized\n");

jud1=DestroyList(&L1);

jud2=DestroyList(&L2);

getchar();getchar();

if(jud1==1)

printf("\nDestroyL1successfully!

\n");

else

printf("\nError:

ThereisnoL1exists!

\n");

if(jud2==1)

printf("\nDestroyL2successfully!

\n");

else

printf("\nError:

ThereisnoL2exists!

\n");

getchar();getchar();

break;

}

case3:

{

printf("\nhereisClearList(),whichbeingrealized\n");

jud1=ClearList(&L1);

jud2=ClearList(&L2);

getchar();getchar();

if(jud1==1)

printf("\nClearL1successfully!

\n");

else

printf("\nClearL1error!

\n");

if(jud2==1)

printf("\nClearL2successfully!

\n");

else

printf("\nClearL2error!

\n");

getchar();getchar();

break;

}

case4:

{

printf("\nhereisListEmpty(),whichbeingrealized\n");

getchar();getchar();

if(L1.elem==NULL)

printf("\nError:

ThereisnoL1exists!

\n");

else

{

jud1=ListEmpty(L1);

if(jud1==1)

printf("\nL1isempty!

\n");

else

printf("\nL1isnotempty\n");

}

if(L2.elem==NULL)

printf("\nError:

ThereisnoL2exists!

\n");

else

{

jud2=ListEmpty(L2);

if(jud2==1)

printf("\nL2isempty!

\n");

else

printf("\nL2isnotempty\n");

}

getchar();getchar();

break;

}

case5:

{

printf("\nhereisListLength(),whichbeingrealized\n");

jud1=ListLength(L1);

jud2=ListLength(L2);

getchar();getchar();

if(L1.elem==NULL)

printf("\nerror!

\n");

else

printf("\nthelengthofL1is%d\n",jud1);

if(L2.elem==NULL)

printf("\nerror!

\n");

else

printf("\nthelengthofL2is%d\n",jud2);

getchar();getchar();

break;

}

case6:

{

printf("\nhereisGetElem(),whichbeingrealized\n");

printf("\npleaseinputthewhichnumberofyouwanttoget:

");

scanf("%d",&i);

jud1=GetElem(L1,i,pe1);

jud2=GetElem(L2,i,pe2);

if(jud1==1)

printf("\nfinditinL1,itis%d!

\n",e1.item1);

else

printf("\nnotfinditinL1!

\n");

if(jud2==1)

printf("\nfinditinL2,itis%d!

\n",e2.item1);

else

printf("\nnotfinditinL2!

\n");

getchar();getchar();

break;

}

case7:

{

printf("\nhereisLocatElem(),whichbeingrealized\n");

printf("\nInputtheelementyouwanttosearch:

");

scanf("%d",&a.item1);

if(L1.elem==NULL)

printf("\nerror!

\n");

else

{

jud1=LocatElem(L1,a,&equal);

if(jud1)

printf("\ntheelementyoufindlocatesNo.%dinL1\n",jud1);

else

printf("\ntheelementisnotfound!

\n");

}

if(L2.elem==NULL)

printf("\nerror!

\n");

else

{

jud2=LocatElem(L2,a,&equal);

if(jud2)

printf("\ntheelementyoufindlocatesNo.%dinL2\n",jud2);

else

printf("\ntheelementisnotfound!

\n");

}

getchar();getchar();

break;

}

case8:

{

printf("\nhereisPriorElem(),whichbeingrealized\n");

printf("\nWhichlistyouwanttouse?

\nL1orL2(Input1or2):

");

scanf("%d",&i);

while(i!

=1&&i!

=2)

{

printf("\nPleaseinputagain:

");

scanf("%d",&i);

}

if(i==1&&L1.elem!

=NULL)

{

printf("\nInputwhichelementyouwantfinditsprior:

");

scanf("%d",&a.item1);

jud1=PriorElem(L1,a,&e1);

if(jud1==1)

{printf("\nFindtheprior,itis\n");display(e1);}

else

printf("\nNotfindit!

\n");

}

elseif(i==2&&L2.elem!

=NULL)

{

printf("\nInputwhichelementyouwantfinditsprior:

");

scanf("%d",&a.item1);

jud2=PriorElem(L2,a,&e2);

if(jud2==1)

{printf("\nFindtheprior,itis\n");display(e2);}

else

printf("\nNotfindit!

\n");

}

else

printf("\nError:

Thislistisnotexisted!

\n");

getchar();getchar();

break;

}

case9:

{

printf("\nhereisNextElem(),whichbeingrealized\n");

printf("\nWhichlistyouwanttouse?

\nL1orL2(Input1or2):

");

scanf("%d",&i);

while(i!

=1&&i!

=2)

{

printf("\nPleaseinputagain:

");

scanf("%d",&i);

}

if(i==1&&L1.elem!

=NULL)

{

printf("\nInputwhichelementyouwantfinditsnext:

");

scanf("%d",&a.item1);

jud1=NextElem(L1,a,&e1);

if(jud1==1)

{printf("\nFindthenext,itis\n");display(e1);}

else

printf("\nNotfindit!

\n");

}

elseif(i==2&&L2.elem!

=NULL)

{

printf("\nInputwhichelementyouwantfinditsnext:

");

scanf("%d",&a.item1);

jud2=NextElem(L2,a,&e2);

if(jud2==1)

{printf("\nFindthenext,itis\n");display(e2);}

else

printf("\nNotfindit!

\n");

}

else

printf("\nError:

Thislistisnotexisted!

\n");

getchar();getchar();

break;

}

case10:

{

printf("\nhereisListInsert(),whichbeingrealized\n");

i=-1;

printf("\nWhichlistyouwanttoinsert?

\nL1orL2(Input1or2):

");

scanf("%d",&i);

while(i!

=1&&i!

=2)

{

printf("\nPleaseinputagain:

");

scanf("%d",&i);

}

if(i==1&&L1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1