首页 > 编程知识 正文

顺序表,顺序表查找操作

时间:2023-05-03 06:59:26 阅读:190481 作者:560

Think:

本blog主要包括序列图的四个基本操作

查询 ,插入, 移位, 删除

# include stdio.h # include stdlib.h # include malloc.h # definelistincreasment 100/*每个分配元素的数量*/#define LISTSIZE 10 /* int length; int listsize; } Sqlist; intsqinitial(sqlistL )/*初始化线性表)/l.elem=(elemtype * ) malloc ) listsize*sizeof ) elemtype ); if (! L.elem ) exit(overflow ); //存储分配失败L.length=0; L.listsize=LISTSIZE; 返回确定; (intlistinsert(sqlistL,int i,ElemType e ) )插入元素)/(if ) I1||IL.length1) printf ) ' error!' ); if (l.length=l.list size (elemtype* new base=) elemtype * ) realloc ) L.elem,(L.listsize LISTINCREASMENT ) ) new base (返回概述; //当前存储容量为L.elem=newbase; L.listsize=LISTINCREASMENT; /*表的容量不足部分存储器*/}elemtype*q=(l.elem[I-1] ); ElemType * p; for(p=) L.Elem[L.Length-1]; p=q; -p(* ) p1 )=*p; *q=e; L.length; 返回确定; }voidlistdelete(sqlistL,int i,ElemType e ) /位于线性列表第I位置的元素) if(I1||il.length ) printf ) ' error!' 删除); else { e=L.elem[i-1]; for (; iL.length; I ) { L.elem[i-1]=L.elem[i]; (L.Length----); }elemtypegetelem(sqlistL,int i ) if ) I1|||il.length ) printf ) ' error!' ); else { return L.elem[i-1]; }}int main () ) { Sqlist L; int t=1,d; SQinitial(L; printf (创建长度为7的序列图。 n '; for(t=1; t=7; t(/)创建长度为7的序列图(/)打印(pleaseinputthe % dthlistelem : ),t ); scanf('%d ',d ); listinsert(L,t,d ); (} printf )表长: %d(n ),L.length; /*输出表长*/for(t=1; t=L.length; t ) printf('%d ',L.elem[t-1]; /*表*/printf('n删除位置:'); //删除元素Scanf('%d”,t ); listdelete(L,t,d ); printf ('删除元素的值: %dn ),d ); printf ('删除后的表:'); for(t=1; t=L.length; t ) printf('%d ',L.elem[t-1]; //已删除的表printf('n要插入的位置'); //插入元素scanf('%d”,t ); printf (要插入的值); scanf('%d ',d ); listinsert(L,t,d ); printf ()插入后续表) ); for(t=1; t=L.length; t )//之后的表printf('%d”,L.elem[t-1] ); printf('n要检索的元素位置:'); //搜索元素Scanf('%d”,t ); d=getelem(L,t ); printf ('此元素的值为%d(n ),d ); 返回0; }

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