华南农业大学数据结构实验答案(包含STL版).doc
《华南农业大学数据结构实验答案(包含STL版).doc》由会员分享,可在线阅读,更多相关《华南农业大学数据结构实验答案(包含STL版).doc(99页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精品文档,仅供学习与交流,如有侵权请联系网站删除8576 顺序线性表的基本操作时间限制:1000MS 内存限制:1000K提交次数:9027 通过次数:2456 题型: 编程题 语言: 无限制Description编写算法,创建初始化容量为LIST_INIT_SIZE的顺序表T,并实现插入、删除、遍历操作。本题目给出部分代码,请补全内容。 #include#include#define OK 1 #define ERROR 0#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define ElemType inttypedef structi
2、nt *elem;int length;int listsize;SqList;int InitList_Sq(SqList &L)/ 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE/ 请补全代码int Load_Sq(SqList &L)/ 输出顺序表中的所有元素int i;if(_) printf(The List is empty!); / 请填空elseprintf(The List is: );for(_) printf(%d ,_); / 请填空printf(n);return OK;int ListInsert_Sq(SqList &L,int
3、 i,int e)/ 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e/ i的合法值为1iL.length +1/ 请补全代码int ListDelete_Sq(SqList &L,int i, int &e)/ 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值/ i的合法值为1iL.length/ 请补全代码int main()SqList T;int a, i;ElemType e, x;if(_) / 判断顺序表是否创建成功printf(A Sequence List Has Created.n);while(1)printf(1:Insert elementn2
4、:Delete elementn3:Load all elementsn0:ExitnPlease choose:n);scanf(%d,&a);switch(a)case 1: scanf(%d%d,&i,&x);if(_) printf(Insert Error!n); / 判断i值是否合法,请填空else printf(The Element %d is Successfully Inserted!n, x); break;case 2: scanf(%d,&i);if(_) printf(Delete Error!n); / 判断i值是否合法,请填空else printf(The El
5、ement %d is Successfully Deleted!n, e);break;case 3: Load_Sq(T);break;case 0: return 1;输入格式测试样例格式说明:根据菜单操作:1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开2、输入2,表示要实现删除操作,紧跟着要输入删除的位置3、输入3,表示要输出顺序表的所有元素4、输入0,表示程序结束输入样例11 211 32130输出样例A Sequence List Has Created.1:Insert element2:Delete element3:Load all elements
6、0:ExitPlease choose:The Element 2 is Successfully Inserted!1:Insert element2:Delete element3:Load all elements0:ExitPlease choose:The Element 3 is Successfully Inserted!1:Insert element2:Delete element3:Load all elements0:ExitPlease choose:The Element 3 is Successfully Deleted!1:Insert element2:Dele
7、te element3:Load all elements0:ExitPlease choose:The List is: 2 1:Insert element2:Delete element3:Load all elements0:ExitPlease choose:作者yqm 解法一:(正规解法)#include#include#define OK 1#define ERROR 0#define LIST_INIT_SIZE 100#define LISTINCREMENT 10#define ElemType inttypedef structint *elem;int length;i
8、nt listsize;SqList;int InitList_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; return 0;int Load_Sq(SqList &L)/ 输出顺序表中的所有元素int i;if(L.length=0) printf(The List is empty!); / 请填空elseprint
9、f(The List is: );for(i=0;iL.length;i+) printf(%d ,L.elemi); / 请填空printf(n);return OK;int ListInsert_Sq(SqList &L,int i,int e)/ 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e/ i的合法值为1iL.length +1/ 请补全代码 ElemType *newbase,*p,*q; if(iL.length+1)return ERROR; if(L.length=L.listsize) newbase=(ElemType*)realloc(L.elem,(L.l
10、istsize+LISTINCREMENT)*sizeof(ElemType); L.elem=newbase; L.listsize+=LISTINCREMENT; q=&(L.elemi-1); for(p=&(L.elemL.length-1);p=q;-p) *(p+1)=*p; *q=e; +L.length; return OK;int ListDelete_Sq(SqList &L,int i, int &e)/ 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值/ i的合法值为1iL.length/ 请补全代码 ElemType *p,*q; if(iL.leng
11、th)return ERROR; p=&(L.elemi-1); e=*p; q=L.elem+L.length-1; for(+p;p=q;+p) *(p-1)=*p; -L.length; return OK;int main()SqList T;int a, i;ElemType e, x;if(!InitList_Sq(T) / 判断顺序表是否创建成功printf(A Sequence List Has Created.n);while(1)printf(1:Insert elementn2:Delete elementn3:Load all elementsn0:ExitnPleas
12、e choose:n);scanf(%d,&a);switch(a)case 1: scanf(%d%d,&i,&x);if(!ListInsert_Sq(T,i,x) printf(Insert Error!n); / 判断i值是否合法,请填空else printf(The Element %d is Successfully Inserted!n, x);break;case 2: scanf(%d,&i);if(!ListDelete_Sq(T,i,e) printf(Delete Error!n); / 判断i值是否合法,请填空else printf(The Element %d is
13、 Successfully Deleted!n, e);break;case 3: Load_Sq(T);break;case 0: return 1;解法二:(C+STL list)#include#include#includeusing namespace std;int main() list T; int a, i; int e, x; printf(A Sequence List Has Created.n); while(1) printf(1:Insert elementn2:Delete elementn3:Load all elementsn0:ExitnPlease ch
14、oose:n); scanf(%d,&a); switch(a) case 1: scanf(%d%d,&i,&x); if(i(int)T.size()+1) printf(Insert Error!n); / 判断i值是否合法 else int j=1,p=0; list:iterator iter=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
15、); printf(The Element %d is Successfully Inserted!n, x); break; case 2: scanf(%d,&i); if(i(int)T.size() printf(Delete Error!n); / 判断i值是否合法 else int j=1; list:iterator iter; for(iter=T.begin();iter!=T.end();+iter) if(j=i) list:iterator tmp; tmp=iter; e=*iter; iter+; T.erase(tmp); break; else j+; prin
16、tf(The Element %d is Successfully Deleted!n, e); break; case 3: if(T.empty() printf(The List is empty!n); else list:iterator plist; printf(The List is: ); for(plist = T.begin(); plist != T.end(); plist+) printf(%d ,*plist); printf(n); break; case 0: return 1;解法三:(数组)#include#include#includeint main(
17、) int T1000; memset(T,0,sizeof(T); int a, i,k=1,e, x; printf(A Sequence List Has Created.n); while(1) printf(1:Insert elementn2:Delete elementn3:Load all elementsn0:ExitnPlease choose:n); scanf(%d,&a); switch(a) case 1: scanf(%d%d,&i,&x); if(ik) printf(Insert Error!n); / 判断i值是否合法,请填空 else for(int j=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 华南 农业大学 数据结构 实验 答案 包含 STL
限制150内