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
| #include <stdio.h> #define maxsize 100 typedef int ElemType;
typedef struct { ElemType data[maxsize]; int length; } SqList;
void InitList(SqList *L) { L->length = 0; }
int ListEmpty(SqList L) { return L.length == 0; }
int ListLength(SqList L) { return L.length; }
void ListInsert(SqList *L, int i, ElemType e) { if (i < 1 || i > L->length + 1) { printf("Error: Invalid index\n"); return; }
if (L->length >= maxsize) { printf("Error: List full\n"); return; }
for (int j = L->length; j >= i; j--) { L->data[j] = L->data[j - 1]; } L->data[i - 1] = e; L->length++; }
void ListDelete(SqList *L, int i) { if (i < 1 || i > L->length) { printf("Error: Invalid index\n"); return; }
for (int j = i; j < L->length; j++) { L->data[j - 1] = L->data[j]; } L->length--; }
void ListPrint(SqList L) { for (int i = 0; i < L.length; i++) { printf("%d ", L.data[i]); } printf("\n"); }
int main() { SqList L; InitList(&L); ListInsert(&L, 1, 10); ListInsert(&L, 2, 20); ListInsert(&L, 3, 30); ListInsert(&L, 4, 40); ListPrint(L); ListDelete(&L, 2); ListPrint(L); return 0; }
|