首页 > 编程知识 正文

数据结构单链表的实现,单链表的存储结构

时间:2023-05-03 07:42:16 阅读:160542 作者:1359

//顺序表基本运算算法实现算法的初始化,求是否为空表,返回长度,查询,插入,删除

# include stdio.h # include malloc.h # define maxsize 50 typedefintelemtype; typedef struct { elemtype data [ maxsize ]; //存储顺序表要素int length的//存储顺序表的长度(} SqList; //顺序表类型voidcreatelist(sqlist*L,ElemType a[],int n )//创建顺序表(L=(sqlist* ) malloc ) sizeof ) sqlist ) ); for(intI=0; in; I ) L-data[i]=a[i]; L-length=n; }voidinitlist(sqlist*L ) l=(sqlist * ) malloc ) sizeof ) sqlist ); L-length=0,分配用于存储线性表的空间; }voiddestroylist(sqlist*L ) ) free ) l; }boollistempty(SQList*L ) return ) L-Length==0); }intlistlength(SQList*L ) return (l-length ); }voiddisplist(sqlist*L ) for ) intI=0; iL-length; I ) printf('%d ',L-data[i]; 打印((n ); }boolgetelem(sqlist*L,int i,ElemType e ) if ) I1||il-length ) return false; e=L-data[i-1]; 返回真; }intlocateelem(SQLIST*L,ElemType e ) ) {int i=0; while(il-lengthL-data[I]!=e ) I; if(I=L-Length ) return 0; elsereturn i 1; }boollistinsert(sqlist*L,int i,ElemType e ) {int j; if(I1||il-Length1)返回假; I----; //将顺序表位顺序转换为elem下标for (j=l-length; ji; j----//data[I]和将随后的元素向后移动一个L-data[j]=L-data[j-1]; L-data[i]=e; L-length; //序列表长度增加1return true; }boollistdelete(SQList*L,int i,ElemType e ) {int j; if(I1||il-Length )返回假; I----; //将顺序表的位顺序转换为elem下标e=L-data[i]; for(j=I; jL-length-1; 将j(//data[I]之后的元素前移一个位置,L-data[j]=L-data[j 1]; L-Length----; //顺序表长度减去1return true; (}int main ) ) {SqList *L; int n=5,e,I; elemtype a [ ]={ 11,13,15,17,19 }; 创建列表(l,a,n ); 打印机(l : ); displist(L ); i=1; getelem(L,I,e ); printf((nl的第%d个元素的值为%d )、I、e ); e=13; I=locateelem(L,e ); printf((nl值为%d的要素的位置的下标为%d )、e、I ); printf('n请输入插入位置和插入的元素值:'); scanf('%d%d ),I,e ); listinsert(L,I,e ); printf('n )在第%d个位置插入%d后的l是)、I、e ); displist(L ); printf('n请输入删除位置:'); scanf('%d ',I ); listdelete(L,I,e ); printf ((从nl中删除的第%d个位置的元素值为%d:)、I、e ); printf('n )删除第%d个位置的元素后的l为)、I ); displist(L ); estroylist(L ); 返回1; }

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