您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 畜牧/养殖 > 华南农业大学数据结构实验答案(包含STL版)
8576顺序线性表的基本操作时间限制:1000MS内存限制:1000K提交次数:9027通过次数:2456题型:编程题语言:无限制Description编写算法,创建初始化容量为LIST_INIT_SIZE的顺序表T,并实现插入、删除、遍历操作。本题目给出部分代码,请补全内容。#includestdio.h#includemalloc.h#defineOK1#defineERROR0#defineLIST_INIT_SIZE100#defineLISTINCREMENT10#defineElemTypeinttypedefstruct{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,表示程序结束输入样例1121132130输出样例ASequenceListHasCreated.1:Insertelement2:Deleteelement3:Loadallelements0:ExitPleasechoose:TheElement2isSuccessfullyInserted!1:Insertelement2:Deleteelement3:Loadallelements0:ExitPleasechoose:TheElement3isSuccessfullyInserted!1:Insertelement2:Deleteelement3:Loadallelements0:ExitPleasechoose:TheElement3isSuccessfullyDeleted!1:Insertelement2:Deleteelement3:Loadallelements0:ExitPleasechoose:TheListis:21:Insertelement2:Deleteelement3:Loadallelements0:ExitPleasechoose:作者yqm解法一:(正规解法)#includestdio.h#includemalloc.h#defineOK1#defineERROR0#defineLIST_INIT_SIZE100#defineLISTINCREMENT10#defineElemTypeinttypedefstruct{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;iL.length;i++)printf(%d,L.elem[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(i1||iL.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(i1||iL.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)#includestdio.h#includemalloc.h#includelistusingnamespacestd;intmain(){listintT;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(i1||i(int)T.size()+1)printf(InsertError!\n);//判断i值是否合法else{intj=1,p=0;listint::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(i1||i(int)T.size())printf(DeleteError!\n);//判断i值是否合法else{intj=1;listint::iteratoriter;for(iter=T.begin();iter!=T.end();++iter){if(j==i){listint::iteratortmp;tmp=iter;e=*iter;iter++;T.erase(tmp);break;}elsej++;}printf(TheElement%disSuccessfullyDeleted!\n,e);}break;case3:if(T.empty())printf(TheListisempty!\n);else{listint::iteratorplist;printf(TheListis:);for(plist=T.begin();plist!=T.end();plist++)printf(%d,*plist);printf(\n);}break;case0:return1;}}}解法三:(数组)#includestdio.h#includemalloc.h#includestring.hintmain(){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
本文标题:华南农业大学数据结构实验答案(包含STL版)
链接地址:https://www.777doc.com/doc-4977259 .html