1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| #include<stdio.h> #include<malloc.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define LIST_INIT_SIZE 10 #define LISTINCREMRNT 10 typedef int Status; typedef int ElemType;
typedef struct { ElemType* elem; int length; int listsize; }SqList;
Status InitList(SqList* L) {
L->elem = (ElemType*)__vcrt_malloc_normal(LIST_INIT_SIZE * sizeof(ElemType)); if (!(L->elem)) exit(OVERFLOW); L->length = 0; L->listsize = LIST_INIT_SIZE;
return OK; }
Status ListInsert(SqList* L, int i, ElemType e) { if (i<1 || i>L->length + 1) return ERROR;
ElemType* q = &(L->elem[i - 1]), *p = NULL; for (p = &(L->elem[L->length - 1]); p >= q; --p) * (p + 1) = *p; *q = e; ++(L->length); if (L->length == L->listsize) { ElemType* newbase = (ElemType*)realloc(L->elem, (L->listsize + LISTINCREMRNT) * sizeof(ElemType)); if (!newbase) exit(OVERFLOW); L->elem = newbase; L->listsize += LISTINCREMRNT; } return OK; }
int getLength(SqList* L) { return L->length; }
char* isEmtey(SqList* L) { return L->length == 0 ? "空" : "不空"; }
Status clearList(SqList* L) { __vcrt_free_normal(L->elem); L->length = 0; return OK; }
Status destryList(SqList* L) {
__vcrt_free_normal(L); L = NULL; return OK; }
ElemType getElemByIndex(SqList* L, int i) { return L->elem[i - 1]; }
int getIndexByElem(SqList* L, ElemType e) { for (int j = 0; j < L->length; ++j) { if ((L->elem[j]) == e) return j + 1; } return -1; }
ElemType changeElemByIndex(SqList* L, int i, ElemType e) { return L->elem[i - 1] = e; }
Status deleteElmeByIndex(SqList* L, int i) { for (int j = i - 1; j < L->length - 1; ++j) L->elem[j] = L->elem[j + 1]; --(L->length); ElemType* newbase = NULL;
while (((L->listsize) - (L->length)) >= 10) { newbase = (ElemType*)realloc(L->elem, (L->listsize - LISTINCREMRNT) * sizeof(ElemType)); if (!newbase) { exit(OVERFLOW); } else { L->elem = newbase; L->listsize -= LISTINCREMRNT; } }
return OK; }
Status myPrintf(SqList* L) { for (int i = 0; i < L->length; i++) { printf("第%d个值------------%d\n",i+1, (L->elem[i])); } return OK; } int main(void) { SqList* L = (SqList*)__vcrt_malloc_normal(sizeof(SqList)); printf("线性表创建状态----%d\n", InitList(L)); printf("当前容量----------%d\n", L->listsize); printf("长度--------------%d\n", getLength(L)); printf("是否为空----------%s\n", isEmtey(L)); for (int i = 0; i <10; i++) { printf("插入状态%d---------%d\n",i, ListInsert(L, i + 1, i)); } printf("当前容量----------%d\n", L->listsize); printf("长度--------------%d\n", getLength(L)); printf("是否为空----------%s\n", isEmtey(L)); printf("开始遍历表\n"); printf("遍历状态----------%s\n", myPrintf(L) == OK ? "成功" : "失败"); printf("是否为空----------%s\n", isEmtey(L)); printf("长度--------------%d\n", getLength(L)); printf("通过索引5取值得---%d\n", getElemByIndex(L,5)); ElemType e = 4; printf("通过值4取索引得----%d\n", getIndexByElem(L,e)); e = 66; printf("通过索引6改值后为--%d\n", changeElemByIndex(L, 6,e)); int temp = L->length; for(int i=5;i<=temp;++i) printf("删除元素-----------%s\n", deleteElmeByIndex(L, 5) == OK ? "成功" : "失败"); printf("遍历状态----------%s\n", myPrintf(L) == OK ? "成功" : "失败"); printf("是否为空----------%s\n", isEmtey(L)); printf("长度--------------%d\n", getLength(L)); printf("当前容量----------%d\n", L->listsize); printf("开始清空表\n"); printf("清空状态----------%s\n", clearList(L) == OK ? "成功" : "失败"); printf("是否为空----------%s\n", isEmtey(L)); printf("长度--------------%d\n", getLength(L)); printf("当前容量----------%d\n", L->listsize);
printf("摧毁表的状态------%s\n", destryList(L) == OK ? "成功" : "失败");
system("pause");
return 0; }
|