华南农业大学数据结构实验答案包含STL版.docx
《华南农业大学数据结构实验答案包含STL版.docx》由会员分享,可在线阅读,更多相关《华南农业大学数据结构实验答案包含STL版.docx(135页珍藏版)》请在冰豆网上搜索。
华南农业大学数据结构实验答案包含STL版
8576顺序线性表的基本操作
时间限制:
1000MS内存限制:
1000K
提交次数:
9027通过次数:
2456
题型:
编程题语言:
无限制
Description
编写算法,创建初始化容量为LIST_INIT_SIZE的顺序表T,并实现插入、删除、遍历操作。
本题目给出部分代码,请补全内容。
#include
#include
#defineOK1
#defineERROR0
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
#defineElemTypeint
typedefstruct
{
int*elem;
intlength;
intlistsize;
}SqList;
intInitList_Sq(SqList&L)
{
//算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
//请补全代码
}
intLoad_Sq(SqList&L)
{
//输出顺序表中的所有元素
inti;
if(_________________________)printf("TheListisempty!
");//请填空
else
{
printf("TheListis:
");
for(_________________________)printf("%d",_________________________);//请填空
}
printf("\n");
returnOK;
}
intListInsert_Sq(SqList&L,inti,inte)
{
//算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
//i的合法值为1≤i≤L.length+1
//请补全代码
}
intListDelete_Sq(SqList&L,inti,int&e)
{
//算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
//i的合法值为1≤i≤L.length
//请补全代码
}
intmain()
{
SqListT;
inta,i;
ElemTypee,x;
if(_________________________)//判断顺序表是否创建成功
{
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(_________________________)printf("InsertError!
\n");//判断i值是否合法,请填空
elseprintf("TheElement%disSuccessfullyInserted!
\n",x);
break;
case2:
scanf("%d",&i);
if(_________________________)printf("DeleteError!
\n");//判断i值是否合法,请填空
elseprintf("TheElement%disSuccessfullyDeleted!
\n",e);
break;
case3:
Load_Sq(T);
break;
case0:
return1;
}
}
}
输入格式
测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束
输入样例
1
12
1
13
2
1
3
0
输出样例
ASequenceListHasCreated.
1:
Insertelement
2:
Deleteelement
3:
Loadallelements
0:
Exit
Pleasechoose:
TheElement2isSuccessfullyInserted!
1:
Insertelement
2:
Deleteelement
3:
Loadallelements
0:
Exit
Pleasechoose:
TheElement3isSuccessfullyInserted!
1:
Insertelement
2:
Deleteelement
3:
Loadallelements
0:
Exit
Pleasechoose:
TheElement3isSuccessfullyDeleted!
1:
Insertelement
2:
Deleteelement
3:
Loadallelements
0:
Exit
Pleasechoose:
TheListis:
2
1:
Insertelement
2:
Deleteelement
3:
Loadallelements
0:
Exit
Pleasechoose:
作者
yqm
解法一:
(正规解法)
#include
#include
#defineOK1
#defineERROR0
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
#defineElemTypeint
typedefstruct
{
int*elem;
intlength;
intlistsize;
}SqList;
intInitList_Sq(SqList&L)
{
//算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
//请补全代码
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.length=0;
L.listsize=LIST_INIT_SIZE;
return0;
}
intLoad_Sq(SqList&L)
{
//输出顺序表中的所有元素
inti;
if(L.length==0)printf("TheListisempty!
");//请填空
else
{
printf("TheListis:
");
for(i=0;i}
printf("\n");
returnOK;
}
intListInsert_Sq(SqList&L,inti,inte)
{
//算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
//i的合法值为1≤i≤L.length+1
//请补全代码
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;
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)
{
//算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
//i的合法值为1≤i≤L.length
//请补全代码
ElemType*p,*q;
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");//判断i值是否合法,请填空
elseprintf("TheElement%disSuccessfullyInserted!
\n",x);
break;
case2:
scanf("%d",&i);
if(!
ListDelete_Sq(T,i,e))printf("DeleteError!
\n");//判断i值是否合法,请填空
elseprintf("TheElement%disSuccessfullyDeleted!
\n",e);
break;
case3:
Load_Sq(T);
break;
case0:
return1;
}
}
}
解法二:
(C++STLlist)
#include
#include
#include
usingnamespacestd;
intmain()
{
listT;
inta,i;
inte,x;
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(i<1||i>(int)T.size()+1)printf("InsertError!
\n");//判断i值是否合法
else
{
intj=1,p=0;
list:
:
iteratoriter=T.begin();
if(i==1)
{
T.push_front(x);
p=1;
}
while(iter!
=T.end())
{
if(j==i&&i!
=1)
{
T.insert(iter,x);
p=1;
iter++;
break;
}
else
{
j++;
iter++;
}
}
if(!
p)
T.push_back(x);
printf("TheElement%disSuccessfullyInserted!
\n",x);
}
break;
case2:
scanf("%d",&i);
if(i<1||i>(int)T.size())printf("DeleteError!
\n");//判断i值是否合法
else
{
intj=1;
list:
:
iteratoriter;
for(iter=T.begin();iter!
=T.end();++iter)
{
if(j==i)
{
list:
:
iteratortmp;
tmp=iter;
e=*iter;
iter++;
T.erase(tmp);
break;
}
else
j++;
}
printf("TheElement%disSuccessfullyDeleted!
\n",e);
}
break;
case3:
if(T.empty())
printf("TheListisempty!
\n");
else
{
list:
:
iteratorplist;
printf("TheListis:
");
for(plist=T.begin();plist!
=T.end();plist++)
printf("%d",*plist);
printf("\n");
}
break;
case0:
return1;
}
}
}
解法三:
(数组)
#include
#include
#include
intmain()
{
intT[1000];
memset(T,0,sizeof(T));
inta,i,k=1,e,x;
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(i<1||i>k)printf("InsertError!
\n");//判断i值是否合法,请填空
else
{
for(intj=k-1;j>=i;j--)
T[j+1]=T[j];
T[i]=x;
k++;
printf("TheElement%disSuccessfullyInserted!
\n",x);
}
break;
case2:
scanf("%d",&i);
if(i<1||i>k-1)printf("DeleteError!
\n");//判断i值是否合法,请填空
else
{
e=T[i];
for(intj=i;jT[j]=T[j+1];
k--;
printf("TheElement%disSuccessfullyDeleted!
\n",e);
}
break;
case3:
if(k==1)printf("TheListisempty!
");
else
{
printf("TheListis:
");
for(intj=1;j}
printf("\n");
break;
case0:
return1;
}
}
}
8577合并顺序表
时间限制:
1000MS内存限制:
1000K
提交次数:
5339通过次数:
2251
题型:
编程题语言:
无限制
Description
编写算法,将两个非递减有序顺序表A和B合并成一个新的非递减有序顺序表C。
本题不提供代码,请同学们独立完成,所需子函数参考前面完成的内容。
输入格式
第一行:
顺序表A的元素个数
第二行:
顺序表A的各元素(非递减),用空格分开
第三行:
顺序表B的元素个数
第四行:
顺序表B的各元素(非递减),用空格分开
输出格式
第一行:
顺序表A的元素列表
第二行:
顺序表B的元素列表
第三行:
合并后顺序表C的元素列表
输入样例
5
13579
5
246810
输出样例
ListA:
13579
ListB:
246810
ListC:
12345678910
作者
yqm
解法一:
(正规解法)
#include
#include
#include
#defineOK1
#defineERROR0
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
#defineElemTypeint
#defineOVERFLOW-2
typedefstruct
{
int*elem;
intlength;
intlistsize;
}SqList;
intInitList_Sq(SqList&L,intn)
{
inti;
L.elem=(ElemType*)malloc(n*sizeof(ElemType));
L.listsize=n;
L.length=n;
for(i=0;iscanf("%d",&L.elem[i]);
returnOK;
}
intLoad_Sq(SqList&L)
{
inti;
if(L.length==0)return0;//请填空
else
{
for(i=0;iprintf("%d",L.elem[i]);
printf("%d",L.elem[L.length-1]);
}
printf("\n");
//if(ch=='A')ch='B';
//elseif(ch=='B')ch='C';
returnOK;
}
voidmergeList_Sq(SqListLa,SqListLb,SqList&Lc)
{
int*pa,*pb,*pc,*pa_last,*pb_last;
pa=La.elem;
pb=Lb.elem;
Lc.listsize=Lc.length=La.length+Lb.length;
pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
if(!
Lc.elem)exit(OVERFLOW);
pa_last=La.elem+La.length-1;
pb_last=Lb.elem+Lb.length-1;
while(pa<=pa_last&&pb<=pb_last)
{
if(*pa<=*pb)
*pc++=*pa++;
else
*pc++=*pb++;
}
while(pa<=pa_last)
*pc++=*pa++;
while(pb<=pb_last)
*pc++=*pb++;
}
intmain()
{
SqListT,R,Y;
inta,b;
scanf("%d",&a);
InitList_Sq(T,a);
scanf("%d",&b);
InitList_Sq(R,b);
mergeList_Sq(T,R,Y);
printf("ListA:
");
Load_Sq(T);
printf("ListB:
");
Load_Sq(R);
printf("ListC:
");
Load_Sq(Y);
}
解法二(C++STLlist)
#include
#include
#include
#include
usingnamespacestd;
voidload(listL)
{
list:
:
iteratorplist;
for(plist=L.begin();plist!
=L.end();plist++)
printf("%d",*plist);
printf("\n");
}
intmain()
{
listT,R;
inta,b,x;
scanf("%d",&a);
for(inti=0;i{
scanf("%d",&x);
T.push_back(x);
}
scanf("%d",&b);
for(inti=0;i
{
scanf("%d",&x);
R.push_back(x);
}
printf("ListA:
");
load(T);
printf("ListB:
");
load(R);
T.merge(R);
printf("ListC:
");
load(T);
}
解法三:
(数组)
#include
#include
#include
intInitList_Sq(intL[],intn)
{
inti;
for(i=0;iscanf("%d",&L[i]);
return1;
}
intLoad_Sq(intL[],intn)
{
inti;
if(n==0)return0;//请填空
else
for(i=0;iprintf("%d",L[i]);
printf("\n");
return1;
}
voidmergeList_Sq(inta[],intb[],intc[],inta_length,intb_length)
{
inti=0,j=0,k=0;
while(i