1 #include<stdio.h> 2 #include <malloc.h> 3 #include <stdbool.h> 4 5 #define LIST_INIT_SIZE 100 // 线性表空间初始分配量 6 #define LIST_INCREMENT 10 //线性表存储空间的分配增量 7 typedef int ElemType;//数组元素类型,假设是int型的 8 typedef struct { 9 int length; 10 ElemType data[LIST_INIT_SIZE]; 11 } Sqlist; 12 13 void InitList(Sqlist *L); 14 15 bool IsEmpty(Sqlist *L); 16 17 bool Insert(Sqlist *L, int site, ElemType e); 18 bool Delete(Sqlist *L,int site); 19 int main() { 20 Sqlist L; 21 InitList(&L); 22 if (IsEmpty(&L)) { 23 printf("不是空表\n"); 24 } else { 25 printf("是空表\n"); 26 } 27 if (Insert(&L, 1, 9)) { 28 printf("插入成功\n"); 29 } else { 30 printf("插入失败\n"); 31 } 32 if(Delete(&L,1)){ 33 printf("删除成功\n"); 34 } else{ 35 printf("删除失败\n"); 36 } 37 } 38 39 void InitList(Sqlist *L) { 40 //构建一个空的线性表 41 L = (Sqlist *) malloc(sizeof(Sqlist)); 42 L->length = 0; 43 printf("创建成功\n"); 44 } 45 46 bool IsEmpty(Sqlist *L) { 47 //判断是否是空表 48 if (L->length == 0) 49 return true; 50 return false; 51 } 52 53 bool Insert(Sqlist *L, int site,ElemType e) { 54 //插入数据 55 if (site < 1 || site > L->length + 1) 56 return false; 57 else { 58 site--; 59 for (int i = L->length; i > site; --i) { 60 L->data[i] = L->data[i - 1]; 61 } 62 L->data[site] = e; 63 L->length++; 64 return true; 65 } 66 } 67 68 bool Delete(Sqlist *L,int site){ 69 if (site < 1 || site > L->length + 1) 70 return false; 71 else{ 72 for (int i = site-1; i <L->length-1 ; ++i) { 73 L->data[i]=L->data[i+1]; 74 } 75 return true; 76 } 77 }