华中科技大学 数据结构 实验报告文档格式.docx
- 文档编号:3701203
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:64
- 大小:237.10KB
华中科技大学 数据结构 实验报告文档格式.docx
《华中科技大学 数据结构 实验报告文档格式.docx》由会员分享,可在线阅读,更多相关《华中科技大学 数据结构 实验报告文档格式.docx(64页珍藏版)》请在冰点文库上搜索。
2.3.1系统实现 3
2.3.2系统测试 3
2.4实验小结 3
参考文献 5
1课程实验概述
本次课程实验旨在加强学生课堂理论学习之后增强上机能力,熟练掌握并应用数据结构中的两种逻辑结构的典型代表——线性表和树。
线性表的顺序存储具有随机存取的功能,而链式存储能有效的利用动态存储空间,学会合理的选择这两种存储方式,看似简单,但在实际应用具有很大的用处。
而树(二叉树)是非线性逻辑结构的代表,树模型的建立可以说完全建立在递归思想之上,树的几乎所有操作都涉及到递归调用,当然我们也可以用栈来实现非递归调用,但是其思想也是相近的。
因此树的实验旨在帮助我们递归思想的建立和成熟。
2实验一基于顺序结构的线性表实现
2.1实验内容与要求
实验内容:
基于顺序存储结构,实现线性表ADT,具有10种基本运算。
具体要求:
1.提供一个实现功能的演示系统。
2.具体物理结构和数据元素类型自定。
3.线性表数据可以使用磁盘文件永久保存。
2.2程序概要设计
1.明确基本功能
程序需要实现的12个基本功能分别是:
IntiaList(初始化),DestroyList(摧毁线性表),ClearList(清空线性表),ListEmpty(判断表空)
,ListLength(求表长),GetElem(取表中元素),LocatElem(获得元素地址)
,PriorElem(取前继),NextElem(取后继)。
ListInsert(插入),ListDelete(删除),ListTrabverse(遍历显示),此外还有辅助功能:
Load(载入),Save(保存)
2.确定各功能实现的函数参数statusIntiaList(SqList*L);
statusDestroyList(SqList*L);
statusClearList(SqListL);
statusListEmpty(SqListL);
intListLength(SqListL);
statusGetElem(SqListL,inti,Elemtype*e);
62
intLocatElem(SqListL,Elemtype*e);
statusPriorElem(SqList*L,Elemtypecur,Elemtype*pre_e);
statusNextElem(SqListL,Elemtypecur,Elemtype*next_e);
statusListInsert(SqList*L,Elemtypee,inti);
statusListDelete(SqList*L,Elemtype*e,inti);
statusListTrabverse(SqListL,void(*visit)(Elemtypee));
voidSave(SqListL);
statusLoad(SqList*L);
2.3数据结构与算法设计
为了满足概述中的功能,结合线性表结构,给出以下结构的定义:
typedefintstatus;
typedefstruct{
intitem1;
}Elemtype;
Elemtype*elem;
intlength;
intlistsize;
}SqList,*L;
算法设计:
1.IntiaList:
初始化线性表,传入的是头结点地址。
申请一个大小为LIST_INT_SIZE、类型为Elemtype的线性存储空间,并用头结点中的首结点指针指向该空间首地址。
2.DestroyList:
销毁头结点中首结点址针指向的线性存储空间,传入的是头结点地址。
3.ClearList:
与Destroy类似但是又有不同,ClearList并不销毁物理空间,而是修改逻辑关系值。
4.ListEmpty:
判空功能,判断表是否为空表。
传入的是头结点值,而非地址,因为不需要对头结点进行修改。
若长度为0,表空,否则不空。
5.ListLength:
求表长功能,由于创建过程中已经把表长信息包含在头结点中,所以直接
调用并显示即可。
6.GetElem:
获取第i号元素,传入的是头结点值、元素编号i、以及出口表结点地址。
7.LocatElem:
取指定元素编号,传入头结点值、存储了所需元素信息的一个临时表结点值、equal函数地址。
采用顺序比较法从表头遍历并比较,一旦找到,返回编号
i。
8.PriorElem:
求指定元素的前一个元素的内容,传入头结点值、包含指定元素信息的一个临时表结点值、存储前一个元素的表结点地址。
主要思路是先调用
LocatElem确定指定元素的位置,如果不是首结点则可直接取到PriorElem的地址。
9.NextElem:
与PriorElem功能类似,不再赘述。
10.ListInset:
插入一个数据元素,传入头结点地址、插入位置i、临时表结点值。
在调用插入函数前构造一个临时表结点,用于存储数据元素信息。
进入插入子函数,插入前先判断插入位置是否合理,再判断是否“满载”。
如果满载则重新申请更大的存储空间。
接下来正式插入数据元素,“先空位置再插入”。
11.ListDelete:
删除一个数据元素,传入头结点地址、删除位置i。
删除前先判断位置是否合理,先删除元素,然后将所有删除元素之后的元素前移一个位置。
12.ListTrabverse:
传入头结点,循环访问各个元素,直至到表尾停止。
2.4输入输出设计
1.输入:
在输入方面,我采用了用户自定义输入的方式,既可以采用用户自行输入,又可以载入之前保存的数据。
在每次操作之后,会提示是否保存刚才的数据,
以便下次再次使用,以下是用户自行输入的功能实现:
voidcreat(SqList*L){inta=0;
printf("
Inputthenumberofyourdatas?
\n"
);
scanf("
%d"
&
a);
L->
elem=(Elemtype*)malloc(a*sizeof(Elemtype));
length=a;
inti;
Pleaseinputyourdata\n"
for(i=1;
i<
=a;
i++){scanf("
(L->
elem[i].item1));
}
listsize=LIST_INIT_SIZE;
printf("
Youhavetosaveyourdata"
getchar();
Save(*L);
2.输出:
采用遍历输出,即功能10的输出。
2.5源程序及注释
/*LinearTableOnSequenceStructure*/#include<
stdio.h>
#include<
malloc.h>
#include<
stdlib.h>
string.h>
#defineLIST_INIT_SIZE10
#defineOK1
#defineFALSE0
#defineTRUE1
#defineERROR-1
#defineOVERFLOW-2
#defineLISTINCREMENT1
/*------------------------------------------------------*/
intchanged=0;
statusIntiaList(SqList*L);
statusDestroyList(SqList*L);
intLocatElem(SqListL,Elemtype*e);
statuscompare(Elemtypee1,Elemtypee2);
statusequal(Elemtypex,Elemtypey);
voiddisplay(Elemtypee);
voidcreat(SqList*L);
voidmenu(void);
charfile0[20];
intmain(void){
SqListL;
L.listsize=LIST_INIT_SIZE;
intop=0;
charreq;
do{
system("
cls"
menu();
Doyouwanttoinputnewlieartable?
Y/N"
req=getchar();
getchar();
if(req=='
Y'
||req=='
y'
)creat(&
L);
else{
Doyouwanttoloadsaveddata?
){
while(!
Load(&
L)){L.elem=(Elemtype*)malloc((L.listsize++)*sizeof(Elemtype));
else{
Youstillusethedatausebefore!
Thenpleaseinputyouroption[0-12]:
"
op);
switch(op){
case0:
break;
case 1:
{printf("
\n here is IntiaList(),which beingrealized\n"
if(IntiaList(&
L)){
Successfullyrealized!
//printf("
Douyouwanttosave?
)
Notrealised!
!
break;
case2:
{printf("
\n hereisDestroyList(),which beingrealized\n"
if(DestroyList(&
L)){printf("
case3:
\n hereisClearList(),which beingrealized\n"
if(ClearList(L)){printf("
case4:
\n hereisListEmpty(),which beingrealized\n"
if(ListEmpty(L)){
Itisanemptylist\n"
Itisnotanemptylist\n"
case5:
\n hereisListLength(),which beingrealized\n"
if(&
L!
=NULL){
%d\n"
ListLength(L));
printf("
L.elem[1].item1);
Successfully realized!
The length of L is
case6:
\n hereisGetElem(),which beingrealized\n"
inti;
Elemtypee;
whichelemdoyouwanttopick?
i=?
i);
if(GetElem(L,i,&
e)){printf("
Andthe%dthelemdatais%d"
i,e.item1);
case7:
\n hereisLocatElem(),which beingrealized\n"
intt,i,a;
Whatdoyouwanttolocate?
item="
t);
for(i=1;
=L.length;
i++)
{
if(L.elem[i].item1==t)break;
inthislist!
if(i==L.length+1)printf("
Thereisnosuchitem
e=L.elem[i];
if(a=LocatElem(L,&
eisinthe%dthlocation\n"
a);
case8:
\n hereisPriorElem(),which beingrealized\n"
Elemtypee,cur;
Tofindtheprioofwho?
i="
cur=L.elem[i];
if(PriorElem(&
L,cur,&
Andtheprioofelem[%d]is%d\n"
case9:
\n hereisNextElem(),which beingrealized\n"
Tofindthenextofwho?
if(NextElem(L,cur,&
And the nextelem of elem[%d] is
}else{
case10:
\n hereisListInsert(),which beingrealized\n"
inti,j;
choosewheretoinsert?
\nInsertnewdata="
(e.item1));
if(ListInsert(&
L,e,i)){printf("
thelistdataisnow:
for(j=1;
j<
j++)printf("
%d"
L.elem[j].item1);
case11:
\n hereisListDelete(),which beingrealized\n"
Elemtype*e;
choosewheretodelete?
if(ListDelete(&
The deleted data is
(*e).item1);
realized\n"
exist!
empty!
break;
case12:
\n hereisListTrabverse(),which being
if(!
L.elem)
\n lieartableLdoesnot
else{if(ListTrabverse(L,display))printf("
\nSuccessfullyrealised!
elseprintf("
\n thislieartableis
default:
;
}while(op);
\n--------------------Welcomeagain!
\n"
return1;
statusIntiaList(SqList*L){
elem=(Elemtype*)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
if(!
L)exit(OVERFLOW);
length=0;
listsize=LIST_INIT_
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 华中科技大学 数据结构 实验报告 实验 报告