线性表

来源:互联网 发布:弗洛伊德算法怎么理解 编辑:程序博客网 时间:2024/06/05 11:22
#include<stdio.h>#include<stdlib.h>typedef int ElemType;struct List{ElemType *list;int size;int MaxSize;};void againMalloc(struct List *L){ElemType *p=(ElemType *)realloc(L->list,2*L->MaxSize*sizeof(ElemType));if(!p){printf("存储空间用完\n");exit(1);}L->list=p;L->MaxSize=2*L->MaxSize;}void initList(struct List *L,int ms){if(ms<=0){printf("ms值非法\n");exit(1);}L->MaxSize= ms;L->list=(ElemType *)malloc(ms*sizeof(ElemType));if(!L->size){printf("分配内存失败\n");exit(1);}L->size=0;}void clearList(struct List *L){if(L->list!=NULL){free(L->list);L->list=0;L->size=L->MaxSize=0;}}int sizeList(struct List *L){return L->size;}int emptyList(struct List *L){if(L->size==0){return 1;}else {return 0;}}ElemType getElem(struct List *L,int pos){if(pos<1 || pos>L->size){printf("元素号越界\n");exit(1);}return L->list[pos-1];}void traverseList(struct List *L){int i;for(i=0;i<L->size;i++){printf("%d ",L->list[i]);}printf("\n");}int findList(struct List *L,ElemType x){int i;for(i=0;i<L->size;i++){if(L->list[i]==x){return i;}else {return -1;}}}int updatePosList(struct List *L,int pos,ElemType x){if(pos<1 || pos>L->size){return 0;}L->list[pos-1]=x;return 1;}void insertFirstList(struct List *L,ElemType x){int i;if(L->size==L->MaxSize){againMalloc(L);}for(i=L->size-1;i>=0;i--){L->list[i+1]=L->list[i];}L->list[0]=x;L->size++;}void insertLastList(struct List *L,ElemType x){if(L->size==L->MaxSize){againMalloc(L);}L->list[L->size]=x;L->size++;}int insertPosList(struct List *L,int pos,ElemType x){int i;if(pos<1||pos>L->size+1){return 0;}if(L->size==L->MaxSize){againMalloc(L);for(i=L->size-1;i>pos-1;i++){L->list[i+1]=L->list[i];}L->list[pos-1]=x;L->size++;return 1;}}void insertOrderList(struct List *L,ElemType x){int i,j;if(L->size==L->MaxSize){againMalloc(L);}for(i=0;i<L->size;i++){if(x<L->list[i]){break;}for(j=L->size-1;j>=i;j--){L->list[j+1]=L->list[j];}L->list[i]=x;L->size++;}}ElemType deleteFirstList(struct List *L){ElemType temp;int i;if(L->size==0){printf("线性表为空\n");exit(1);}temp=L->list[0];for(i=1;i<L->size;i++){L->list[i-1]=L->list[i];}L->size--;return temp;}ElemType deleteLastList(struct List *L){if(L->size==0){printf("线性表为空,不能进行删除操作\n");exit(1);}L->size--;return L->list[L->size];}ElemType deletePosList(struct List *L,int pos){ElemType temp;int i;if(pos<1 || pos>L->size){printf("pos值越界,不能进行删除操作\n");exit(1);}temp=L->list[pos-1];for(i=pos;i<L->size;i++){L->list[i-1]=L->list[i];}L->size--;return temp;}int deleteValueList(struct List *L,ElemType x){int i,j;for(i=0;i<L->size;i++){if(L->list[i]==x){break;}if(i==L->size){return 0;}for(j=i+1;j<L->size;j++){L->list[j-1]=L->list[j];}L->size--;return 1;}}int main(){int a[10]={2,4,6,8,10,12,14,16,18,20};int i;struct List L;initList(&L,5);for(i=0;i<10;i++){insertFirstList(&L,a[i]);}insertPosList(&L,11,48);insertPosList(&L,1,64);printf("%d\n",getElem(&L,4));traverseList(&L);printf("%d\n",findList(&L,10));updatePosList(&L,3,20);deleteFirstList(&L);deleteFirstList(&L);deleteLastList(&L);deleteLastList(&L);deletePosList(&L,5);//deletePosList(&L,7);   元素越界了printf("%d\n",sizeList(&L));printf("%d\n",emptyList(&L));traverseList(&L);clearList(&L);}

0 0