数据结构实验程序参考Word文件下载.docx
《数据结构实验程序参考Word文件下载.docx》由会员分享,可在线阅读,更多相关《数据结构实验程序参考Word文件下载.docx(25页珍藏版)》请在冰豆网上搜索。
%d"
&
i);
inputanumbertoinsert.x="
scanf("
x);
k=ListInsert_sq(&
sq,i,x);
/*if(k);
else;
*/
break;
3'
inputalocationtodelete.i="
k=ListDelete_sq(&
sq,i,&
if(k)printf("
Thedeletednumberis%d."
x);
/*else;
getch();
break;
4'
print(sq);
5'
Thelengthofthelistis:
%d."
sq.length);
0'
flag=0;
}
}while(flag==1);
}
voidcreat_Sqlist(SqList*L)
{inti=0,x;
while
(1)
{scanf("
if(x==0)break;
L->
data[i]=x;
i++;
length=i;
voidprint(SqListL)
{inti;
for(i=0;
i<
L.length;
i++)
%5d"
L.data[i]);
\n"
/*========menu========*/
charmenu(void)
{charch;
MENU\n"
/*puts()*/
===========================\n"
Seqentiallistoperation\n"
1.creat\n"
2.insert\n"
3.delete\n"
4.print\n"
5.length\n"
0.exit\n"
Choice(0,1,2,3,4,5,):
ch=getchar();
return(ch);
intListInsert_sq(SqList*L,inti,inte)
/*在顺序表L中第i个数据元素之前插入e*/
{intj,n;
n=L->
length;
if(n>
=MAX-1)returnERROR;
/*OVERFLOW表空间溢出*/
if((i<
1)||(i>
n+1))returnERROR;
/*插入位置不合法*/
else{for(j=n-1;
j>
=i-1;
j--)
data[j+1]=L->
data[j];
/*元素右移*/
data[i-1]=e;
/*插入元素e*/
L->
length++;
/*表长加1*/
returnOK;
intListDelete_sq(SqList*L,inti,int*e)
/*在顺序表L中删除第i个数据元素,并用e返回其值*/
{intj,n;
if((i<
1)||(i>
n))returnERROR;
/*i值不合法*/
else{*e=L->
data[i-1];
for(j=i;
j<
=n-1;
j++)
data[j-1]=L->
length--;
2.单链表操作
;
#defineNULL0
#defineLENsizeof(structLNode)
typedefstructLNode
{intdata;
structLNode*next;
}*LinkList;
creat_Linklist(LinkList*L);
intLocatElem_L(LinkListL,inte);
intListLength_L(LinkListL);
voidprint(LinkListhead);
Insert_Linklist(LinkList*L,inti,inte);
Delete_Linklist(LinkList*L,inti,int*e);
voidreverse(LinkList*L);
voiddel_same(LinkList*L);
{LinkListhead=NULL;
switch(ch)
creat_Linklist(&
head);
scanf("
Insert_Linklist(&
head,i,x);
inputalocationtodelete.n="
n);
k=Delete_Linklist(&
head,n,&
if(k)printf("
getch();
print(head);
inputanumbertolocate.x="
n=LocatElem_L(head,x);
printf("
Thelocationof%dis:
%d."
x,n);
6'
n=ListLength_L(head);
Thelengthofthelink_listis:
n);
7'
reverse(&
8'
del_same(&
creat_Linklist(LinkList*L)
{LinkListp,q;
intx;
p=(LinkList)malloc(LEN);
*L=p;
/*建立头结点,由*L指向*/
while(x!
=0)
{q=(LinkList)malloc(LEN);
/*建立下一个结点,由q所指向*/
q->
data=x;
p->
next=q;
p=q;
/*把q结点链到单链表中*/
next=NULL;
intLocatElem_L(LinkListL,inte)
{LinkListp;
inti;
p=L->
next;
i=1;
while((p!
=NULL)&
&
(p->
data!
=e))
{p=p->
if(p)returni;
elsereturnERROR;
intListLength_L(LinkListL)
intn;
n=0;
while(p!
=NULL)
{n=n+1;
p=p->
return(n);
voidprint(LinkListhead)
{LinkListp;
p=head->
{printf("
p->
data);
Insert_Linklist(LinkList*L,inti,inte)
{LinkListhead,p,s;
intj;
head=*L;
j=0;
p=head;
/*指针初始化,j为计数器*/
while((p->
next!
(j<
i-1))
j++;
}/*查找第i-1个结点,由p指向*/
if(!
p->
next)
Error!
returnERROR;
}/*i<
1或大于表长*/
else{s=(LinkList)malloc(LEN);
s->
data=e;
next=p->
/*生成新结点*/
next=s;
/*完成插入*/
Delete_Linklist(LinkList*L,inti,int*e)
{LinkListp,q;
p=*L;
while(p->
next&
j<
i-1)
if(!
next||j<
1)
returnERROR;
else{q=p->
next=q->
*e=q->
data;
free(q);
Link-listoperation\n"
5.getelement\n"
6.length\n"
7.reverse\n"
8.deleteallsamenode\n"
Choice(0,1,2,3,4,5,6,7,8):
/*voidreverse(LinkList*L)
{LinkListp,q,r;
p=(*L)->
if(p)
{q=p->
while(q)
{r=q->
next=p;
q=r;
(*L)->
}*/
voidreverse(LinkList*L)
{LinkListp,q;
while(p)
{q=p;
p=p->
next=(*L)->
voiddel_same(LinkList*L)
{LinkListp,q,pre;
{pre=p;
q=p->
{if(q->
data==p->
data)
{pre->
q=pre->
else{pre=q;
q=q->
3.栈操作
(1)顺序栈
typedefintelemtype;
stack.h"
Seqentialstackoperations\n"
1.push\n"
2.pop\n"
3.gettop\n"
0.exit\n"
Choice(0,1,2,3):
{SqStackst;
inti,flag=1,n,k;
charchoice;
InitStack(&
st);
do{choice=menu();
switch(choice)
\npushdata=?
k=Push(&
st,n);
pushend."
elseprintf("
Thestackisfull."
k=Pop(&
st,&
\npopdata=%d\n"
\nThestackisempty."
getch();
k=GetTop(st,&
if(k)printf("
\ntopdata=%d\n"
(2)链栈
#defineMax100
#defineelementtypeint
{elementtypedata[Max];
inttop;
}stacktype;
voidPush(stacktype*s,elementtypex)
{if(s->
top==Max-1)
OVERFLOW\n"
exit(0);
top++;
data[s->
top]=x;
intstackempty(stacktypes)
{if(s.top==-1)
return
(1);
else
return(0);
voidPop(stacktype*s,elementtype*e)
{if(stackempty(*s))
Underfllow!
{*e=s->
top];
top--;
voidMENU()
{printf("
**************MENU***************\n"
1:
Push\n"
2:
Pop\n"
0:
Quit\n"
*********************************\n"
Pleasemakeachioce:
{stacktypes;
intc;
charx,e;
s.top=-1;
do
{MENU();
c);
switch(c)
{case1:
printf("
pushnumber:
Push(&
s,x);
case2:
Pop(&
s,&
e);
popnumber:
%d\n"
e);
case0:
default:
ERROR!
}while(c!
=0);
4.二叉树基本操作
#defineMAX20
typedefstructbtnode
{chardata;
structbtnode*lchild,*rchild;
}Bitnode,*Bitree;
Bitnode*CreatBitree_pre()/*先序建立二叉树*/
{Bitnode*t;
if(ch=='
'
)t=NULL;
else{t=(Bitnode*)malloc(sizeof(Bitnode));
t->
data=ch;
lchild=CreatBitree_pre();
rchild=CreatBitree_pre();
}
returnt;
Bitnode*CreatBitree_level()/*按层次顺序建立二叉树*//*此算法可修改得更完善些*/
{Bitnode*Q[100];
intfront=1,rear=0;
/*队列指针如此设定,便于操作*/
Bitnode*root=NULL,*s;
while((ch=getchar())!
='
#'
)/*'
为输入结束标志*/
{if(ch=='
)s=NULL;
/*输入空格为虚结点*/
{s=(Bitnode*)malloc(sizeof(Bitnode));
/*建立新结点*/
lchild=NULL;
rchild=NULL;
Q[++rear]=s;
/*虚结点指针NULL或新结点地址入队*/
if(rear==1)root=s;
/*第一个结点为根结点*/
{if(s&
Q[front])/*孩子和双亲结点都不是虚结点*/
if(rear%2==0)
Q[front]->
lchild=s;
/*新结点为左孩子*/
elseQ[front]->
rchild=s;
/*新结点为右孩子*/
if(rear%2==1)front++;
/*左右孩子都已处理结点出队*/
returnroot;
preorder(Bitnode*t)
{if(t)
{printf("
%c"
t->
preorder(t->
lchild);
rchild);
inorder(Bitnode*t)
{if(t)
{inorder(t->
printf("
t->
inorder(t->
inorder2(Bitnode*t)/*中序遍历非递归算法*/
{Bitnode*p,*stack[MAX];
inttop=-1;
p=t;
{while(p)
{stack[++top]=p;
lchild;
if(top>
{p=stack[t