首页 > 编程知识 正文

顺序表的实现方法,利用顺序表的操作,实现以下函数

时间:2023-05-03 11:48:28 阅读:190491 作者:2253

6-2顺序表基本操作(10分) ) )。

在本问题中,要求实现顺序表要素的追加、删除、检索以及顺序表输出这4个基本操作函数。 l是序列图,表示函数statuslistinsert_sq(sqlistl,int pos, ElemType e )在顺序表的pos的位置插入要素e ) pos应该从1开始),函数statuslistdelete_sq )删除顺序表的pos位置的要素,参照型参数函数intlistlocate_sq ) sqlistl,elemtype )检查并返回顺序表中查询元素e的顺序)如果要返回从1开始的顺序来实现,必须考虑表的扩展。

函数接口定义: statuslistinsert_sq(sqlistL,int pos,ElemType e ); statuslistdelete_sq(sqlistL,int pos,ElemType e ); intlistlocate_sq(SQListL,ElemType e ); voidlistprint_sq(sqlistL; 这里,l是顺序表。 pos是位置; e表示元素。 如果插入和删除操作期间的pos参数不正确,函数返回ERROR,否则返回OK。

审判程序示例://库函数头文件包含# include stdio.h # include malloc.h # include stdlib.h//函数状态代码定义# define true1# define define list _ init _ size 100 # definelistincrement 10 typedefintelemtype; //线形表中的元素均为整数型typedef struct{ ElemType* elem; //存储空间基地址int length; //表中元素的个数int listsize; //表容量大小}SqList; //statuslistinsert_sq(sqlistL,int pos,elemtype )由顺序表类型定义的statuslistdelete_sq(sqlistL,int pos,elemtype ); intlistlocate_sq(SQListL,ElemType e ); voidlistprint_sq(sqlistL; //结构初始化和销毁操作statusinitlist_sq(sqlistL )//初始化l为空的有序列表l.elem=(elemtype * ) malloc ) list_init_size*sizeof L.elem ) exit(overflow ); L.listsize=LIST_INIT_SIZE; L.length=0; 返回确定; (}int main ) ) { SqList L; if(initlist_sq ) l!=ok(printf ) initlist_sq:初始化失败! n '; 返回- 1; }for(intI=1; i=10; I ) listinsert_sq(L,I,I ); 输入操作编号; //操作次数scanf('%d”,operationNumber ); while (操作编号!=0) { int operationType; //操作类型scanf('%d”,操作类型); if (操作类型==1) (//操作int pos,elem; scanf('%d%d )、pos和elem ); listinsert_sq(L,pos,elem ); }elseif(operationtype==2) ) /删除操作int pos; ElemType elem; scanf('%d ',pos ); listdelete_sq(L,pos,elem ); printf(%d(n ),elem ); (elseif(operationtype==3) ) /搜索定位操作ElemType elem; Scanf('%d ',elem ); intpos=listlocate_sq(L,elem ); if(pos=1pos=l.Length ) printf('%dn ',pos ); else printf (非寻找! n '; }elseif(operationtype==4) )//输出操作ListPrint_Sq(L ) l; }操作编号- -; } return 0; (/)在此处回答(*/输入格式) :请在第一行输入表示操作数的整数operationNumber,然后输入operationNumber行。 各行包含操作信息)“操作的种类、编号、操作内容”。 编号1表示插入操作,下两个参数表示插入的位置和插入的要素的值编号2表示删除操作,下一个参数表示删除的位置编号3表示检索操作,下一个参数表示顺序表输出操作http://www.Sina.com/com 对于操作2,输出删除的元素的值,对于操作3,输出该元素的位置,如果不存在则输出。在操作4中,按顺序输出整个顺序表的元素。 两个元素之间由空格分隔,最后一个元素后没有空格。

输入采样: 41 1 112 23 34输出采样: 1311 2 3 4 5 6 7 8 9 10

voidlistprint_sq(sqlistL ) for ) intI=0; iL.length-1; I ) {printf('%d ',L.elem[i]; }printf((%d(n ),L.elem[L.length-1]; (intlistlocate_sq ) sqlistL,ElemType e ) for ) intI=0; iL.length; I ) if(L.Elem[I]==e ) return i 1; } return -1; }statuslistinsert_sq(sqlistL,int pos,ElemType e ) if ) pos1|||posl.length1) /! 返回错误; if (l.length=l.list size (Elemtype* n=) elemtype * ) realloc ) L.Elem,) l.list size list increment (sizeof ) sseof ) if (! n ) exit(overflow ); L.elem=n; L.listsize=LISTINCREMENT; }elemtype*q=(L.elem[pos-1]; for (elemtype * p=(l.elem [ l.length-1 ] ); p=q; -p(* ) p1 )=*p; *q=e; L.length; 返回确定; }statuslistdelete_sq(sqlistL,int pos,ElemType e ) if ) pos1|||posl.length ) return ERROR; Elemtype*p=(L.Elem[pos-1] ); e=*p; ElemType* q=L.elem L.length-1; for(p; p=q; p () ) (p-1 )=*p; (} --L.length; 返回确定; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。