顺序表学习

来源:互联网 发布:现场平面布置图软件 编辑:程序博客网 时间:2024/06/17 14:15
#include<stdio.h>#include<stdlib.h>#define LIST_SIZE_LIST 100typedef int ElemType;typedef struct {ElemType *elem;int length;int listsize;}SqList;//初始化顺序表int Init_List(SqList &L){L.elem=(ElemType *)malloc(LIST_SIZE_LIST*sizeof(ElemType));L.length=0;L.listsize=LIST_SIZE_LIST;return 0;}//插入元素int Insert_List(SqList &L,int pos,ElemType e){if((pos<1)||(pos>L.length+1))return -1;ElemType *q,*p;q=L.elem+pos-1;for(p=L.elem+L.length+1;p>=q;--p){        *(p+1)=*p;}L.elem[pos-1]=e;++L.length;return 0;}//得到元素int Get_List(SqList L,int pos ,ElemType &e){if((pos<1)||(pos>L.length))return -1; e=L.elem[pos-1]; return e;}//删除元素int Delete_List(SqList &L,int pos,ElemType &e){if((pos<1)||(pos>L.length))return -1;ElemType *q,*p;e=L.elem[pos-1];q=L.elem+L.length-1;p=L.elem+pos-1;for(++p;p<=q;++p){    *(p-1)=*p;} --L.length;return e;}int Clear_List(SqList &L){L.length=0;return 0;}
//主函数int main(){SqList L;int j,e,k;Init_List(L);for(j=1;j<=5;j++){Insert_List(L,j,j);}for(j=1;j<=5;j++){printf("%d  ",*(L.elem+j-1));}printf("\n");Insert_List(L,1,0);for(j=1;j<=L.length;j++){printf("%d  ",*(L.elem+j-1));}printf("\n");Get_List(L,3,e);printf("%d",e);printf("\n");Delete_List(L,3,k);printf("%d",k);printf("\n");for(j=1;j<=L.length;j++){printf("%d  ",*(L.elem+j-1));}printf("\n");Clear_List(L);}

                                             
0 0
原创粉丝点击