华南农业大学数据结构上机实验题答案.docx
《华南农业大学数据结构上机实验题答案.docx》由会员分享,可在线阅读,更多相关《华南农业大学数据结构上机实验题答案.docx(121页珍藏版)》请在冰豆网上搜索。
华南农业大学数据结构上机实验题答案
华南农业大学
实验一实验一实验一实验一实验一实验一实验一实验一实验一实验一实验一实验一实验一
111111*********11111111111111111111111111111111111111111111*********1111111111
#include
#include
#defineOK1
#defineERROR0
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
#defineElemTypeint
typedefstruct
{
int*elem,length,listsize;
}SqList;
intInitList_Sq(SqList&L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.length=0;
L.listsize=LIST_INIT_SIZE;
returnOK;
}
intLoad_Sq(SqList&L)
{
inti;
if(L.length==0)
printf("TheListisempty!
");
else
{
printf("TheListis:
");
for(i=0;iprintf("%d",L.elem[i]);
}
printf("\n");
returnOK;
}
intListInsert_Sq(SqList&L,inti,inte)
{
if(i<1||i>L.length+1)
returnERROR;
ElemType*newbase,*q,*p;
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
returnOK;
}
intListDelete_Sq(SqList&L,inti,int&e)
{
ElemType*q,*p;
if(i<1||i>L.length)
returnERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;p++)
*(p-1)=*p;
L.length--;
returnOK;
}
intmain()
{
SqListT;
inta,i;
ElemTypee,x;
if(InitList_Sq(T))
{
printf("ASequenceListHasCreated.\n");
}
while
(1)
{
printf("1:
Insertelement\n2:
Deleteelement\n3:
Loadallelements\n0:
Exit\nPleasechoose:
\n");
scanf("%d",&a);
switch(a)
{
case1:
scanf("%d%d",&i,&x);
if(!
ListInsert_Sq(T,i,x))
printf("InsertError!
\n");
else
printf("TheElement%disSuccessfullyInserted!
\n",x);
break;
case2:
scanf("%d",&i);
if(!
ListDelete_Sq(T,i,e))
printf("DeleteError!
\n");
else
printf("TheElement%disSuccessfullyDeleted!
\n",e);
break;
case3:
Load_Sq(T);
break;
case0:
return1;
}
}
}
222222222222222222222222222222222222222222222222222222222222222222222222222222
#include
#include
#defineOK1
#defineERROR0
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
#defineElemTypeint
typedefstruct
{
int*elem,length,listsize;
}SqList;
intInitList_Sq(SqList&L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.length=0;
L.listsize=LIST_INIT_SIZE;
returnOK;
}
intLoad_Sq(SqList&L)
{
inti;
for(i=0;iprintf("%d",L.elem[i]);
printf("\n");
returnOK;
}
intListLength(SqListL)
{
returnL.length;
}
intGetElem(SqListL,inti,ElemType&e)
{
e=L.elem[i-1];
returnOK;
}
intListInsert_Sq(SqList&L,inti,inte)
{
if(i<1||i>L.length+1)
returnERROR;
ElemType*p,*q,*newbase;
if(L.listsize<=L.length)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;p--)
*(p+1)=*p;
*q=e;
L.length++;
returnOK;
}
voidMergeList(SqListLa,SqListLb,SqList&Lc)
{
inti,j,k,La_len,Lb_len,ai,bj;
i=j=1;
k=0;
InitList_Sq(Lc);
La_len=ListLength(La);
Lb_len=ListLength(Lb);
while((i<=La_len)&&(j<=Lb_len))
{
GetElem(La,i,ai);
GetElem(Lb,j,bj);
if(ai<=bj)
{
ListInsert_Sq(Lc,++k,ai);
i++;
}
else
{
ListInsert_Sq(Lc,++k,bj);
j++;
}
}
while(i<=La_len)
{
GetElem(La,i++,ai);
ListInsert_Sq(Lc,++k,ai);
}
while(j<=Lb_len)
{
GetElem(Lb,j++,bj);
ListInsert_Sq(Lc,++k,bj);
}
Load_Sq(Lc);
}
intmain()
{
intan,bn,i,e;
SqListLa,Lb,Lc;
InitList_Sq(La);
scanf("%d",&an);
for(i=1;i<=an;i++)
{
scanf("%d",&e);
ListInsert_Sq(La,i,e);
}
printf("ListA:
");
Load_Sq(La);
InitList_Sq(Lb);
scanf("%d",&bn);
for(i=1;i<=an;i++)
{
scanf("%d",&e);
ListInsert_Sq(Lb,i,e);
}
printf("ListB:
");
Load_Sq(Lb);
printf("ListC:
");
MergeList(La,Lb,Lc);
return0;
}
333333333333333333333333333333333333333333333333333333333333333333333333333333
#include
#include
#defineOK1
#defineERROR0
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
#defineElemTypeint
typedefstruct
{
int*elem,length,listsize;
}SqList;
intInitList_Sq(SqList&L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!
L.elem)
{
printf("NO1");
returnERROR;
}
L.length=0;
L.listsize=LIST_INIT_SIZE;
returnOK;
}
intLoad_Sq(SqList&L)
{
inti;
if(!
L.length)
{
printf("ThisListisempty!
\n");
returnERROR;
}
else
{
for(i=0;iprintf("%d",L.elem[i]);
}
printf("\n");
returnOK;
}
intListInsert_Sq(SqList&L,inti,inte)
{
ElemType*newbase,*p,*q;
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!
newbase)
{
printf("NO2");
returnERROR;
}
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;p--)
*(p+1)=*p;
*q=e;
L.length++;
returnOK;
}
intswap(SqList&L,intn)
{
inti,j,temp;
for(i=0,j=n-1;j>i;i++,j--)
{
temp=L.elem[i];
L.elem[i]=L.elem[j];
L.elem[j]=temp;
}
returnOK;
}
intmain()
{
SqListT;
intn,i;
ElemTypex;
scanf("%d",&n);
InitList_Sq(T);
for(i=1;i{
scanf("%d",&x);
ListInsert_Sq(T,i,x);
}
printf("TheListis:
");
Load_Sq(T);
swap(T,n);
printf("TheturnedListis:
");
Load_Sq(T);
return0;
}
444444444444444444444444444444444444444444444444444444444444444444444444444444
#include
#include
#defineERROR0
#defineOK1
#defineElemTypeint
typedefstructLNode
{
intdata;
structLNode*next;
}LNode,*LinkList;
intCreateLink_L(LinkList&L,intn)
{
LinkListp,q;
inti;
ElemTypee;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
q=(LinkList)malloc(sizeof(LNode));
q=L;
for(i=0;i{
scanf("%d",&e);
p=(LinkList)malloc(sizeof(LNode));
p->data=e;
p->next=q->next;
q->next=p;
q=q->next;
}
returnOK;
}
intLoadLink_L(LinkList&L)
{
LinkListp=L->next;
if(!
p)
printf("TheListisempty!
");
else
{
printf("TheLinkListis:
");
while(p)
{
printf("%d",p->data);
p=p->next;
}
}
printf("\n");
returnOK;
}
intLinkInsert_L(LinkList&L,inti,ElemTypee)
{
LNode*p=L,*s;
intj=0;
while(p&&j{
p=p->next;
j++;
}
if(!
p||j>i-1)
returnERROR;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
returnOK;
}
intLinkDelete_L(LinkList&L,inti,ElemType&e)
{
LNode*p=L,*q;
intj=0;
while(p->next&&j{
p=p->next;
j++;
}
if(!
(p->next)||jreturnERROR;
q=p->next;
p->next=q->next;
e=q->data;
free(q);
returnOK;
}
intmain()
{
LinkListT;
inta,n,i;
ElemTypex,e;
printf("Pleaseinputtheinitsizeofthelinklist:
\n");
scanf("%d",&n);
printf("Pleaseinputthe%delementofthelinklist:
\n",n);
if(CreateLink_L(T,n))
{
printf("ALinkListHasCreated.\n");
LoadLink_L(T);
}
while
(1)
{
printf("1:
Insertelement\n2:
Deleteelement\n3:
Loadallelements\n0:
Exit\nPleasechoose:
\n");
scanf("%d",&a);
switch(a)
{
case1:
scanf("%d%d",&i,&x);
if(!
LinkInsert_L(T,i,x))
printf("InsertError!
\n");
else
printf("TheElement%disSuccessfullyInserted!
\n",x);
break;
case2:
scanf("%d",&i);
if(!
LinkDelete_L(T,i,e))
printf("DeleteError!
\n");
else
printf("TheElement%disSuccessfullyDeleted!
\n",e);
break;
case3:
LoadLink_L(T);
break;
case0:
return1;
}
}
}
555555555555555555555555555555555555555555555555555555555555555555555555555555
#include
#include
#defineERROR0
#defineOK1
#defineElemTypeint
typedefstructLNode
{
intdata;
structLNode*next;
}LNode,*LinkList;
intCreateLink_L(LinkList&L,intn)
{
LinkListp,q;
inti;
ElemTypee;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
q=(LinkList)malloc(sizeof(LNode));
q=L;
for(i=0;i{
scanf("%d",&e);
p=(LinkList)malloc(sizeof(LNode));
p->data=e;
p->next=q->next;
q->next=p;
q=q->next;
}
returnOK;
}
intLoadLink_L(LinkList&L)
{
LinkListp=L->next;
if(!
p)
printf("TheListisempty!
");
else
{
while(p)
{
printf("%d",p->data);
p=p->next;
}
}
printf("\n");
returnOK;
}
voidMergeList_L(LinkList&La,LinkList&Lb,LinkList&Lc)
{
LinkListpa,pb,pc;
pa=La->next;
pb=Lb->next;
Lc=pc=La;
while(pa&&pb)
{
if(pa->data<=pb->data)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
else
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
}
pc->next=pa?
pa:
pb;
free(Lb);
}
intmain()
{
LinkListLa,Lb,Lc;
intn;
scanf("%d",&n);
CreateLink_L(La,n);
printf("ListA:
");
LoadLink_L(La);
scanf("%d",&n);
CreateLink_L(Lb,n);
printf("ListB:
");
LoadLink_L(Lb);
MergeList_L(La,Lb,Lc);
printf("ListC:
");
LoadLink_L(Lc);
return0;
}
666666666666666666666666666666666666666666666666666666666666666666666666666666
#include
#include
#defineOK1
#defineERROR0
#defineElemTypeint
typedefstructLNode
{
intdata;
structLNode*next;
}LNode,*LinkList;
intCreateLink_L(LinkList&L,intn)
{
LinkListp,q;
inti;
ElemTypee;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
q=(LinkList)malloc(sizeof(LNode));
q=L;
for(i=0;i{
scanf("%d",&e);
p=(LinkList)malloc(sizeof(LNode));
p->data=e;
p->next=q->next;
q->next=p;
q=q->next;
}
returnOK;
}
intLoadLink_L(LinkList&L)
{
LinkListp=L->next;
if(!
p)
printf("TheListisEmpty!
");
else
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
returnOK;
}
intinversion(LinkLis