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