/*===========初始化===============*/
List*List_Init(void){/*为List分配堆内存*/
List *L =malloc(sizeof(List));/*动态分配失败处理*/if(L ==NULL){fprintf(stderr,"Init malloc is error\n");returnNULL;}/*数组长度指向0*/
L->len =0;/*测试*/puts("Init_List is success");return L;}
(3)清空
/*==============清空==============*/voidList_Clear(List *L){/*清空只需要将长度置0*/
L->len =0;/*测试*/puts("Clear_List is success");}
(4)销毁
/*===========销毁============*/voidList_Destory(List *L){/*已销毁判断*/if(L ==NULL){fprintf(stderr,"Destory List is error, List can not be destory\n");exit(1);}/*释放L,并置为空指针*/free(L);
L =NULL;/*测试*/puts("Destory List is success");}
(5)获取长度
/*=========打印长度============*/intList_GetLen(const List *L){return L->len;}
(6)判断是否为空
/*===========判断是否为NULL===========*/
bool List_IsEmpty(const List *L){if(L->len ==0){return true;}return false;}
(7)判断是否已满
/*==========判断是否已满=============*/
bool List_IsFull(const List *L){if(L->len == MaxSize){return true;}return false;}
(8)头插式创建
/*==========头插式创建============*/
bool List_CreatHead(List *L, ElemType arr[],int length){int i =0;/*长度判断*/if(L->len + length > MaxSize){fprintf(stderr,"Length of List is not enough\n");return false;}/*元素全部向后移动length个位置,空出前length位置*/for(i = L->len -1;i >=0;i--){
L->data[i + length]= L->data[i];}/*将length个元素逆序复制到头部前length空间*/for(i =0;i < length;i++){
L->data[i]= arr[length - i -1];}/*长度加length*/
L->len += length;return true;}
(9)尾插式创建
/*=============尾插式创建==============*/
bool List_CreatTail(List *L, ElemType arr[],int length){int i=0;/*长度判断*/if(L->len + length > MaxSize){fprintf(stderr,"Length of List is not enough\n");return false;}/*遍历元素赋值*/for(i =0;i < length;i++){
L->data[i + L->len]= arr[i];}/*长度加length*/
L->len += length;return true;}
(10)查找元素
/*==============查找元素,返回下标==========*/intList_IndexOf(const List *L, ElemType e){int i =0;for(i =0;i < L->len;i++){if(L->data[i]== e){/*查找成功*/return i;}}/*查找失败*/return-1;}
(11)获取第loc个元素
/*===========获取第loc个元素===========*/
ElemType List_GetElem(const List *L,int loc){/*判断是否越界*/if(loc <1|| loc > L->len){fprintf(stderr,"the index is not suitable\n");exit(1);}/*返回loc - 1位置元素*/return L->data[loc -1];}
(12)在loc位置插入元素
/*=======在loc位置插入元素=====*/
bool List_Insert(List *L,int loc, ElemType e){int i =0;/*判断是否越界*/if(loc <1|| loc > L->len +1){fprintf(stderr,"the index is not suitable\n");return false;}/*将loc - 1起的元素全部向后搬运一个位置*/for(i = L->len;i >= loc;i --){
L->data[i]= L->data[i -1];}/*插入元素*/
L->data[loc -1]= e;/*长度加1*/
L->len ++;return true;}
(13)删除loc位置元素
/*===========删除第loc个元素============*/
bool List_Delete(List *L,int loc, ElemType *e){int i =0;/*判断是否越界*/if(loc <1|| loc > L->len){fprintf(stderr,"the index is not suitable\n");return false;}/*e保留删除值*/*e = L->data[loc -1];/*将loc位置元素前前搬运一个位置*/for(i = loc; i < L->len; i ++){
L->data[i -1]= L->data[i];}/*长度减一*/
L->len --;return true;}
(14)修改第loc位置元素
/*============修改第loc个元素============*/
bool List_Modify(List *L,int loc, ElemType e){/*判断是否越界*/if(loc <1|| loc > L->len){fprintf(stderr,"the index is not suitable\n");return false;}
L->data[loc -1]= e;return true;}
(15)打印顺序表
/*==============打印================*/voidList_Show(const List *L){int i =0;for(i =0;i < L->len;i++){printf("%d\t",L->data[i]);}putchar('\n');}
/*===========初始化===============*/voidList_Init(List* L){/*为data分配堆内存,长度为初始长度*/
L->data =malloc(sizeof(ElemType)* InitSize);/*动态分配失败处理*/if(L->data ==NULL){fprintf(stderr,"Init malloc is error\n");exit(1);}/*数组长度指向0*/
L->len =0;/*最大长度为初始长度*/
L->size = InitSize;/*测试*/puts("Init_List is success");}
(3)清空
/*==============清空==============*/voidList_Clear(List *L){/*已销毁判断*/if(L->data ==NULL){fprintf(stderr,"Destory List is error, List can not be destory\n");exit(1);}/*清空只需要将长度置0*/
L->len =0;/*测试*/puts("Clear_List is success");}
(4)销毁
/*===========销毁============*/voidList_Destory(List *L){/*已销毁判断*/if(L->data ==NULL){fprintf(stderr,"Destory List is error, List can not be destory\n");exit(1);}/*释放L->data,并置为空指针*/free(L->data);
L->data =NULL;/*表的最大容量设置为-1*/
L->size =-1;
L->len =-1;/*测试*/puts("Destory List is success");}
(5)获取长度
/*=========获取长度============*/intList_GetLen(const List *L){/*已销毁判断*/if(L->data ==NULL){fprintf(stderr,"Destory List is error, List can not be destory\n");exit(1);}if(L->size ==-1){fprintf(stderr,"list is not exist\n");}return L->len;}
(6)判断是否为空
/*===========判断是否为NULL===========*/
bool List_IsEmpty(const List *L){/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}if(L->len ==0){return true;}return false;}
(7)数组扩容
/*=============数组扩容===========*/voidList_Malloc(List *L,int length){int len = L->len + length;
ElemType *new_data =NULL;int i =0;/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}/*判断是否需要扩容*/while(L->size < len){/*两倍扩容*/
L->size *=2;}/*分配新数组用来存扩容的内容*/
new_data =malloc(sizeof(ElemType)* L->size);/*将旧数组的内容拷贝过来*/for(i =0;i < L->len;i++){
new_data[i]= L->data[i];}/*释放旧的data*/free(L->data);/*L->data修改位new_data*/
L->data = new_data;}
(8)头插式创建
/*==========头插式创建============*/
bool List_CreatHead(List *L, ElemType arr[],int length){int i =0;/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}/*长度判断*/if(L->len + length > L->size){/*扩容*/List_Malloc(L,length);}/*元素全部向后移动length个位置,空出前length位置*/for(i = L->len -1;i >=0;i--){
L->data[i + length]= L->data[i];}/*将length个元素逆序复制到头部前length空间*/for(i =0;i < length;i++){
L->data[i]= arr[length - i -1];}/*长度加length*/
L->len += length;return true;}
/*==============查找元素,返回下标==========*/intList_IndexOf(const List *L, ElemType e){int i =0;/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}for(i =0;i < L->len;i++){if(L->data[i]== e){/*查找成功*/return i;}}/*查找失败*/return-1;}
(11)获取第loc个元素
/*===========获取第loc个元素===========*/
ElemType List_GetElem(const List *L,int loc){/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}/*判断是否越界*/if(loc <1|| loc > L->len){fprintf(stderr,"the index is not suitable\n");exit(1);}/*返回loc - 1位置元素*/return L->data[loc -1];}
(12)在loc位置插入元素
/*=======在loc位置插入元素=====*/
bool List_Insert(List *L,int loc, ElemType e){int i =0;/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}/*判断是否越界*/if(loc <1){fprintf(stderr,"the index is not suitable\n");return false;}/*扩容*/List_Malloc(L,1);/*将loc - 1起的元素全部向后搬运一个位置*/for(i = L->len;i >= loc;i --){
L->data[i]= L->data[i -1];}/*插入元素*/
L->data[loc -1]= e;/*长度加1*/
L->len ++;return true;}
(13)删除loc位置元素
/*===========删除第loc个元素============*/
bool List_Delete(List *L,int loc, ElemType *e){int i =0;/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}/*判断是否越界*/if(loc <1|| loc > L->len){fprintf(stderr,"the index is not suitable\n");return false;}/*e保留删除值*/*e = L->data[loc -1];/*将loc位置元素前前搬运一个位置*/for(i = loc; i < L->len; i ++){
L->data[i -1]= L->data[i];}/*长度减一*/
L->len --;return true;}
(14)修改第loc位置元素
/*============修改第loc个元素============*/
bool List_Modify(List *L,int loc, ElemType e){/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}/*判断是否越界*/if(loc <1|| loc > L->len){fprintf(stderr,"the index is not suitable\n");return false;}
L->data[loc -1]= e;return true;}
(15)打印顺序表
/*==============打印================*/voidList_Show(const List *L){int i =0;/*链表已销毁*/if(L->size ==-1){fprintf(stderr,"list is not exist\n");exit(1);}for(i =0;i < L->len;i++){printf("%d\t",L->data[i]);}putchar('\n');}