首页 > 编程知识 正文

顺序表的基本操作,顺序队列的基本操作

时间:2023-05-04 21:20:21 阅读:190388 作者:1283

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

在本问题中,要求实现顺序表要素的追加、删除、检索以及顺序表输出这4个基本操作函数。 l是序列图,表示函数statuslistinsert_sq(sqlistl,int pos, ElemType e )在顺序表的pos的位置插入要素e ) pos应该从1开始),函数statuslistdelete_sq )删除顺序表的pos位置的要素,参照型参数函数intlist locate _ 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表示查找操作,以下参数表示找到的值编号4表示序列表的输出操作

输出格式:操作2中,将删除的要素的值输出到输出操作3中,输出该要素的位置,在不存在该要素的情况下,输出“NOT FOUND”; 在操作4中,按顺序输出整个顺序表的元素。 两个元素之间由空格分隔,最后一个元素后没有空格。

输入示例: 41 1 112 23 34输出示例: 1311 2 3 4 5 6 7 8 9 10参见以下内容:将元素插入statuslistinsert_sq(sqlistL,int pos,ElemType e )//线性表中的合法位置,首先插入if (POF ) newbase=(elemtype* ) realloc(L.elem,listincrement*sizeof ) elemtype ); if (! 新基础(exit ) overflow; L.elem=newbase; L.listsize=LISTINCREMENT; }for(intI=L.Length; i=pos; I--}{L.Elem[I]=L.Elem[I-1]; } L.elem[pos-1]=e; L.length; 返回确定; }statuslistdelete_sq(sqlistL,int pos,ElemType e ) /在线性列表的合法位置删除元素,首先删除if(pos1|||posl.length ) return ERROR; e=L.elem[pos-1]; for(intI=pos-1; iL.length-1; I ) { L.elem[i]=L.elem[i 1]; (} --L.length; 返回确定; (intlistlocate_sq ) sqlistL,ElemType e ) for ) intI=0; iL.length; I ) if(e==L.Elem[I] ) { return i 1; } } return 0; }voidlistprint_sq(sqlistL ) for ) intI=0; iL.length; I ) if(I==0) printf ) ' %d ',L.elem[i]; ELSEprintf('%d ',L.elem[i] ); }

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