数据结构实验报告及答案Word格式.docx
《数据结构实验报告及答案Word格式.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告及答案Word格式.docx(40页珍藏版)》请在冰豆网上搜索。
intlistsize;
}SqList;
statusInitList_Sq(SqList**L);
statusListInsert_Sq(SqList*L,inti,ElemTypee);
statusListDelete_Sq(SqList*L,inti);
intLocateElem(SqList*L,ElemTypee);
LinearList.c文件内部代码
#include<
malloc.h>
#include"
LinearList.h"
statusInitList_Sq(SqList**L)
*L=(SqList*)malloc(sizeof(SqList));
(**L).elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
(**L).length=0;
(**L).listsize=LIST_INIT_SIZE;
returnOK;
}
statusListInsert_Sq(SqList*L,inti,ElemTypee)
ElemType*newbase,*p,*q;
if(i<
1||i>
L->
length+1)
{
returnERROR;
}
if(L->
length>
=L->
listsize)
newbase=(ElemType*)realloc(L->
elem,(L->
listsize+LISTINCREMENT)*sizeof(ElemType));
L->
elem=newbase;
listsize+=LISTINCREMENT;
q=&
(L->
elem[i-1]);
for(p=L->
elem+L->
length-1;
p>
=q;
p--)
*(p+1)=*p;
*q=e;
L->
length++;
statusListDelete_Sq(SqList*L,inti)
ElemType*q,*p;
length)
p=&
elem[i]);
for(q=L->
p<
p++)
*(p-1)=*p;
length--;
intLocateElem(SqList*L,ElemTypee)
inti=0;
while(i<
length&
&
elem[i]!
=e)i++;
if(i>
return0;
else
returni+1;
Main.c文件代码为
#include<
stdio.h>
#include"
voidprintfElems(SqList*L)
inti;
for(i=0;
i<
length;
i++)
printf("
%d"
L->
printf("
\n"
);
voidmain()
SqList*list;
InitList_Sq(&
list);
//初始化顺序表
ListInsert_Sq(list,1,10);
//把10插入第一位置
ListInsert_Sq(list,1,9);
//把9插入第一位置
ListInsert_Sq(list,1,8);
//把8插入第一位置
ListInsert_Sq(list,1,7);
//把7插入第一位置
printfElems(list);
//打印出所有元素
ListDelete_Sq(list,1);
//删除
删除第一个之后\n"
////打印出所有元素
第%d个元素是10"
LocateElem(list,10));
//查找元素10的位置
链表代码为:
LinkList.h
typedefstructLNode
ElemTypedata;
structLNode*next;
}LNode,*LinkList;
statusInitLinkList(LinkList*L);
statusLinkListInsert(LinkListL,inti,ElemTypee);
statusLinkListDelete(LinkListL,inti);
intLink_LocateElem(LinkListL,ElemTypee);
LinkList.c
LinkList.h"
stdlib.h>
statusInitLinkList(LinkList*L)
*L=(LinkList)malloc(sizeof(LNode));
(*L)->
next=NULL;
statusLinkListInsert(LinkListL,inti,ElemTypee)
LinkLists,p=L;
intj=0;
while(p&
j<
i-1)
p=p->
next;
j++;
if(!
p||j>
s=(LinkList)malloc(sizeof(LNode));
s->
data=e;
next=p->
p->
next=s;
statusLinkListDelete(LinkListL,inti)
LinkListq,p=L;
while(p&
q=p->
next=q->
free(q);
intLink_LocateElem(LinkListL,ElemTypee)
while(L&
e!
data)
L=L->
i++;
L)
return-1;
returni;
Main.c
voidprintfElems_L(LinkListL)
L=L->
while(L)
data);
LinkListhead;
InitLinkList(&
head);
//初始化链表
LinkListInsert(head,1,10);
//第一个位置插入10
LinkListInsert(head,1,9);
//第一个位置插入9
LinkListInsert(head,1,8);
//第一个位置插入8
LinkListInsert(head,1,7);
//第一个位置插入7
printfElems_L(head);
//打印所有的元素
LinkListDelete(head,1);
//删除第一个元素
//打印所有元素
第%d个元素是10\n"
Link_LocateElem(head,10));
//查找元素10的位置*/
六.思考讨论题或体会或对改进实验的建议
总结实验中在编辑、编译、运行等各环节中所出现的问题及解决方法。
七.参考资料
《数据结构习题集》(C语言版)严蔚敏吴伟民编著清华大学出版社
《C程序设计题解与上机指导》(第三版) 谭浩强编著 清华大学出版社
2013-4-12实验成绩:
实验2栈与队列
3.熟悉栈与队列抽象数据类型的表示和实现方法。
4.掌握栈与队列的基本操作。
5.进入所选择的IDE环境。
6.书写程序源代码。
7.调试程序。
8.验证程序。
1. 借助栈实现对表达式中括弧是否配对的判断算法
Stack.h"
intisPiar(charc1,charc2)
if((c1=='
['
c2=='
]'
)||(c1=='
{'
c1=='
}'
('
)'
))
return1;
return0;
charc;
chartopValue;
sqStack*S;
InitStack(&
S);
while((c=getchar())!
=10)
if(StackEmpty(S))
{
Push(S,c);
}
else
GetTop(S,&
topValue);
if(isPiar(topValue,c))
{
Pop(S,&
}
else
Push(S,c);
if(StackEmpty(S))
格式正确\n"
格式错误\n"
– 2.假设循环队列中只设rear和length来表示队尾元素的位置和队中元素的个数,设计并实现这样的队列结构,试给出判断循环队列的队满条件,设计相应的入队和出队算法并编程实现。
Queue.c
queue.h"
statusInitQueue(cycleQueue**q)
*q=(cycleQueue*)malloc(sizeof(cycleQueue));
(*q)->
base=(ElemType*)malloc(sizeof(ElemType)*MAXQSIZE);
rear=0;
lenght=0;
statusEnQueue(cycleQueue*q,ElemTypee)
if(q->
lenght>
=MAXQSIZE)returnERROR;
q->
base[q->
rear]=e;
rear=(q->
rear+1)%MAXQSIZE;
lenght++;
statusDeQueue(cycleQueue*q,ElemType*e)
lenght==0)returnERROR;
*e=q->
base[(q->
rear-q->
lenght+MAXQSIZE)%MAXQSIZE];
lenght--;
2013-4-19实验成绩:
实验3串
5.理解串的模式匹配算法(包括KMP算法)。
6.明确串也是特殊的线性表,掌握其特殊性所在。
9.进入所选择的IDE环境。
10.书写程序源代码。
11.调试程序。
12.验证程序。
实验课题:
–编写函数计算一个子串在主串中出现的次数,如果主串中没有该子串则返回0。
(实验数据:
自拟)
typedefstruct{
char*ch;
}String;
String*makeString(char*ch)
char*c;
inti,j;
String*str=(String*)(malloc(sizeof(String)));
for(i=0,c=ch;
*c;
++i,++c);
str->
ch=ch;
length=i;
intcountSubString(String*str,String*subStr)
inti,j,sum=0;
(str->
length-subStr->
length+1);
for(j=0;
j<
subStr->
j++)
if(subStr->
ch[j]!
=str->
ch[j+i])break;
if(j==subStr->
length)sum++;
returnsum;
String*str=makeString("
abcdefababc"
String*subStr=makeString("
ab"
ab在abcdefababc出现次数为%d\n"
countSubString(str,subStr));
2013-4-26实验成绩:
实验4数组与广义表
7.掌握稀疏矩阵的表示方法及其运算的实现。
8.实现稀疏矩阵在三元组、十字链表等表示下的各运算并分析其效率。
13.进入所选择的IDE环境。
14.书写程序源代码。
15.调试程序。
16.验证程序。
–将一个以三元组表存储的稀疏数组以矩阵的形式打印出来(测试数据自拟)
(要求给出算法、运行结果的屏幕截图,源程序电子打包提交)
#defineMAXSIZE256
ElemTypee;
}Triple;
Tripledata[MAXSIZE];
intr,c,z;
}TSMatrix;
voidInitTSMatrix(TSMatrix**T,intr,intc)
*T=(TSMatrix*)malloc(sizeof(TSMatrix));
(*T)->
r=r;
c=c;
z=0;
voidInsertMatrix(TSMatrix*T,inti,intj,intv)
if(T->
z>
=MAXSIZE)return;
T->
data[T->
z].i=i;
z].j=j;
z].e=v;
z++;
voidprintfMatrix(TSMatrix*T)
inti=0;
intindex;
ElemType*arr=(ElemType*)malloc(sizeof(ElemType)*T->
r*T->
c);
for(;
c;
i++)arr[i]=0;
z;
index=(T->
data[i].i-1)*T->
c+(T->
data[i].j-1);
arr[index]=T->
data[i].e;
for(i=0;
if(i%T->
c==0)printf("
%d\t"
arr[i]);
free(arr);
TSMatrix*T;
inti,j,r,c,v;
请输入矩阵行数和列数\n"
scanf("
%d%d"
&
i,&
j);
InitTSMatrix(&
T,i,j);
请输入非零点的个数\n"
%d"
j;
请输入第%d个点格式为(行号列号数值)\n"
i+1);
scanf("
%d%d%d"
r,&
c,&
v);
InsertMatrix(T,r,c,v);
printfMatrix(T);
2013-5-10实验成绩:
实验5二叉树的操作
9.掌握二叉树的结构特征及存储结构特点。
10.掌握二叉树的基本运算。
17.进入所选择的IDE环境。
18.书写程序源代码。
19.调试程序。
20.验证程序。
–1.以二叉链表为存储结构,建立二叉树,在此基础上实现二叉要的前序遍历、中序遍历、后序遍历和层次遍历。
(测试数据自拟)
–2.求题1所建立的二叉树的高度
#include<
voidcreateTree(BiThrTree*T)
%c"
if(c=='
'
)
*T=NULL;
*T=(BiThrTree)malloc(sizeof(BiThrNode));
(*T)->
data=c;
createTree(&
(*T)->
lchild);
rchild);
voidPreOrderTraverse(BiThrTreeT,void(*visit)(TElemType))
if(T)
visit(T->
da