数据结构上机实验报告文档格式.docx
《数据结构上机实验报告文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构上机实验报告文档格式.docx(27页珍藏版)》请在冰点文库上搜索。
}
此表为:
"
);
OutputList(l);
请输入要查找元素的值\n"
scanf("
%d"
&
i);
a=FindList(l,i);
要查找元素的位置第%d个\n"
a+1);
请输入要删除元素的值\n"
a=DeleteList1(&
l,i);
要删除元素位置第%d个\n"
请输入要删除元素的位置\n"
DeleteList2(&
intInitList(List*L,intms)
L->
list=(int*)malloc(ms*sizeof(int));
if(!
list)exit
(1);
size=0;
MAXSIZE=ms;
intInsertList(List*L,intitem,intrc)
inti;
if(rc<
0||rc>
size+1)return-1;
if(L->
size>
=L->
MAXSIZE)
return-1;
list[rc]=item;
for(i=L->
size;
i>
rc;
--i)
{L->
list[i]=L->
list[i-1];
++L->
intDeleteList1(List*L,intitem)
inti,j;
if(item==L->
list[i])
break;
if(i<
size)
for(j=i;
j<
size-1;
j++)
list[j]=L->
list[j+1];
--L->
returni;
elsereturn-1;
intDeleteList2(List*L,intrc)
inti,j,a;
for(j=rc-1;
size--;
intFindList(ListL,intitem)
inti=0;
while(i<
=L.size&
&
item!
=L.list[i])++i;
=L.size)returni;
elsereturn0;
voidOutputList(ListL)
L.size;
L.list[i]);
\n"
}
实验结果:
实验二判断回文串运算与括号匹配运算
1.括号匹配运算
任意输入一串字符,逐个取符号并判断段是否为括号符,若满足条件则进栈,否则判断是否与满足括号符的字符相匹配,若匹配,则继续执行判断,直到字符串为空,若此时栈为空,则输出不满足条件;
若不匹配,也直接输出不满足条件。
NN
YY
N
Y
#include"
stdio.h"
stdlib.h"
string.h"
typedefstructStack
{
char*pTop;
char*pBottom;
intnum;
}STACK;
voidInitStack(STACK&
pS)
pS.pTop=(char*)malloc(100*sizeof(char));
if(pS.pTop==NULL)
动态内存分配失败!
return;
pS.pBottom=pS.pTop;
*pS.pTop=0;
pS.num=1000;
voidpush(STACK&
pS,chark)
if((pS.pTop-pS.pBottom)>
=pS.num)
栈溢出"
else
*pS.pTop++=k;
voidpop(STACK&
if(pS.pBottom==pS.pTop)
为空栈"
pS.pTop--;
boolisempty(STACK&
s)
if(s.pBottom==s.pTop)
returntrue;
returnfalse;
boolCheck(STACK&
charch[100];
请输入检测的括号串:
%s"
ch);
for(inti=0;
strlen(ch);
i++)
switch(ch[i])
case'
['
:
push(s,'
]'
if(*(s.pTop-1)=='
)
pop(s);
('
)'
if(isempty(s))
intmain(void)
Stacks;
InitStack(s);
if(Check(s))
匹配成功\n"
匹配失败\n"
return0;
2.判断回文串运算
栈的特点是先进后出,而回文串是一个正读和反读都一样的字符串,所以可以利用栈的特点将串入栈,在出栈,最后将出栈的结果和用户输入的串进行比较即可。
char*top;
char*base;
intstacksize;
s.top=(char*)malloc(100*sizeof(char));
if(s.top==NULL)
s.base=s.top;
*s.top=0;
s.stacksize=100;
//分配100个
s,chark)
if((s.top-s.base)>
=s.stacksize)
*s.top++=k;
s,char&
k)
if(s.base==s.top)
k=*--s.top;
boolcheck(STACK&
s)
charstr[100];
chardata[100];
请输入检测的字符串:
str);
strlen(str);
++i)
push(s,str[i]);
for(intj=0;
++j)
pop(s,data[j]);
for(intn=0;
n<
++n)
if(str[n]!
=data[n])
}else{
continue;
if(check(s))
是回文串"
不是回文串"
实验三稀疏矩阵的转置运算
编写一个完整的程序,实现稀疏矩阵三元组的存贮及其转置矩阵的算法设计。
在主函数中程序中调用转置函数、输出原矩阵函数和转置后的矩阵函数。
#defineMAXSIZE1000
typedefstruct
introw,col;
inte;
}Triple;
intm,n,len;
Tripledata[MAXSIZE+1];
}TSMatrix;
voidTransposeTSMatrix(TSMatrix*A,TSMatrix*B)
inti,j,k;
B->
m=A->
n;
B->
n=A->
m;
len=A->
len;
if(B->
len>
0)
j=1;
for(k=1;
k<
=A->
k++)
for(i=1;
if(A->
data[i].col==k)
data[j].row=A->
data[i].col;
data[j].col=A->
data[i].row;
data[j].e=A->
data[i].e;
j++;
intmain()
intx,y,i,j;
intk=1,p=1;
TSMatrixB,C;
intA[100][100];
请输入要转置行列式的行和列的值:
%d%d"
x,&
y);
请输入此稀疏矩阵的元素:
=x;
for(j=1;
=y;
j++)
A[i][j]);
if(A[i][j]!
=0)
B.data[k].row=i;
B.data[k].col=j;
B.data[k].e=A[i][j];
k++;
B.m=x;
B.n=y;
B.len=k-1;
TransposeTSMatrix(&
B,&
C);
转置后的矩阵为:
=C.m;
i++){
=C.n;
j++){
if((C.data[p].row==i)&
(C.data[p].col==j))
%d"
C.data[p].e);
p++;
elseprintf("
0"
实验四非递归算法实现二叉树的前序和中序遍历
前序遍历:
1.访问根节点2.前序遍历左子树3.前序遍历右子树
中序遍历:
1.中序遍历左子树2.访问根节点3.中序遍历右子树
#include<
#defineINIT_STACK_SIZE100
#defineSTACKINCREMENT10
typedefstructBiNode
chardata;
structBiNode*lchild,*rchild;
intvisitcount;
}BiNode,*BiTree;
BiNode**base;
BiNode**top;
}SqStack;
voidInitStack(SqStack&
S)
if(!
(S.base=(BiNode**)malloc(sizeof(BiTree)*INIT_STACK_SIZE)))
}
S.top=S.base;
S.stacksize=INIT_STACK_SIZE;
return;
voidPush(SqStack&
S,BiNode*e)
if(S.top-S.base>
=S.stacksize)
{
if(!
(S.base=(BiNode**)realloc(S.base,sizeof(BiTree)*(STACKINCREMENT+S.stacksize))))
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
voidPop(SqStack&
S,BiNode**e)
if(S.base==S.top)
*e=*--S.top;
intGetTop(SqStackS,BiNode**e)
if(S.base==S.top)
{
return0;
*e=*(S.top-1);
return1;
intStackEmpty(SqStackS)
if(S.top==S.base)
else
voidCreateBiTree(BiTree&
T)
charch;
scanf("
%c"
&
ch);
if(ch=='
'
T=NULL;
else
if(!
(T=(BiNode*)malloc(sizeof(BiNode))))
T->
data=ch;
CreateBiTree(T->
lchild);
rchild);
voidPreorderTraverse_1(BiTreeT)
BiTreep;
SqStackS;
InitStack(S);
Push(S,T);
while(!
StackEmpty(S))
while(GetTop(S,&
p)&
p)
printf("
%c"
p->
data);
Push(S,p->
Pop(S,&
p);
Push(S,p->
voidInOrderTraverse_1(BiTreeT)
Push(S,T);
while(!
while(GetTop(S,&
Push(S,p->
}
Pop(S,&
printf("
BiTreeT;
请按先序次序输入二叉树中结点的值(字符),空格字符表示空树:
CreateBiTree(T);
先序遍历方法结果为:
PreorderTraverse_1(T);
\n\n"
中序遍历方法结果为:
InOrderTraverse_1(T);
实验体会
通过这次写实验报告,我深切的理解了这门课的本质。
刚开始学这门课时,当时还不清楚这门课程的目的,现在,我真正的理解了。
在这次设计的过程中,我还遇到了,很多的问题。
在执行时出现了问题。
后来问同学,指出我的错误,不过获益不少。
我又重新整理思路,虽然走了很多弯路,但是让我认识到,一定要创新,大胆,不能按照旧的思路去干新的事情。
这次的实验报告,让我受益匪浅,不仅有知识方面的,还有生活和精神上的。
总之,我会继续我的兴趣编程,相信在编程的过程中,能不断的提高自己。