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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
| #include <stdio.h> #include <stdlib.h>
#define LIST_LENGTH 100 #define LIST_CREMENT 20
typedef int tElem; typedef tElem * pElem; typedef struct List { pElem elem; int listLength; int listSize; } List; typedef enum Status // 枚举数据 { ok = 1,Error = 0 } Status;
Status InitList(List * l) { l->elem = (pElem) malloc(sizeof(tElem) * LIST_LENGTH); if(l->elem == 0) { printf("空间申请失败!"); return Error; }
l->listLength = 0; l->listSize += LIST_LENGTH; }
void ListEmpty(List * l) { if(l->listLength == 0) { printf("线性表为空 \n"); } else { printf("线性表的长度为 %d\n",l->listLength); } }
Status ListInsert(List * l,int i,tElem e) { if(i < 0 || i > l->listLength) { printf("ListInsert() 参数错误!\n"); return Error; }
if(l->listLength >= l->listSize) { l->elem = (pElem) realloc(l,sizeof(tElem) * LIST_CREMENT); if(l->elem == 0) { printf("空间申请失败!"); return Error; } else { l->listSize += LIST_CREMENT; } }
for(int j = l->listLength; j > i; j++) { l->elem[j] = l->elem[j - 1]; } l->elem[i] = e; l->listLength++; return ok; }
Status GetElem(List * l,int i) { if(i < 0 || i > l->listLength) { printf("ListInsert() 参数错误!\n"); return Error; }
printf("线性表第 %d 个元素是 %d\n",i,l->elem[i]); return ok; }
Status PriorElem(List * l,tElem e) { int i = -1; for(int j = 0; j < l->listLength; j++) { if(l->elem[j] == e) i = j; } if(i == -1) { printf("元素 %d 不在线性表中!\n",e); return Error; } else if (i == 0) { printf("元素 %d 是线性表的第一个元素,无前一个元素!\n",e); return Error; } else { printf("元素 %d 的前一个元素是:%d\n",e,l->elem[i-1]); return ok; } }
Status NextElem(List * l,tElem e) { int i = -1; for(int j = 0; j < l->listLength; j++) { if(l->elem[j] == e) i = j; } if(i == -1) { printf("元素 %d 不在线性表中!\n",e); return Error; } else if (i == l->listLength - 1) { printf("元素 %d 是线性表的最后一个元素,无后一个元素!\n",e); return Error; } else { printf("元素 %d 的后一个元素是:%d\n",e,l->elem[i+1]); return ok; } }
void ListTraverse(List * l) { if(l->listLength > 0) { for(int i = 0; i < l->listLength; i++) { printf("线性表的第 %d 个元素是:%d\n",i,l->elem[i]); } } else { printf("线性表为空!\n"); } }
Status ListDelete(List * l,int i) { if(i < 0 || i > l->listLength) { printf("ListInsert() 参数错误!\n"); return Error; }
for(int j = i; j < l->listLength; j++) { l->elem[j] = l->elem[j + 1]; }
l->listLength --; return ok; }
void ClearList(List * l) { l->listLength = 0; }
void DestroyList(List * l) { free(l->elem); l->listLength = 0; l->listSize = 0; }
int main() { List plist;
InitList(&plist);
printf("第一次判断线性表的长度:"); ListEmpty(&plist); printf("\n");
ListInsert(&plist,0,12); ListInsert(&plist,1,23); ListInsert(&plist,2,34); printf("第二次判断线性表的长度:"); ListEmpty(&plist); printf("\n");
GetElem(&plist,1); printf("\n");
PriorElem(&plist,10); PriorElem(&plist,12); PriorElem(&plist,34); printf("\n");
NextElem(&plist,10); NextElem(&plist,34); NextElem(&plist,12); printf("\n");
ListTraverse(&plist); printf("\n");
ListDelete(&plist,0); ListTraverse(&plist); printf("\n");
ClearList(&plist); ListTraverse(&plist); printf("\n");
DestroyList(&plist); ListTraverse(&plist); printf("\n");
return 0; }
|