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
|
#include <iostream> using namespace std; #define ok 1 #define error 0 #define true 1 #define false 0 #define maxsize 20 typedef int elemtype;
typedef struct { elemtype date[maxsize]; int length; } SqList;
typedef int Status;
Status InitList(SqList *L) { L->length = 0; return ok; }
Status visit(elemtype c) { cout << c; return ok; }
Status InstEmpty(SqList L) { if (L.length == 0) return true; else return false;
}
Status ClearList(SqList *L) { L->length = 0; return ok; }
Status GetElen(SqList *L, int i, elemtype *e) { if (L->length == 0 || i < 1 || i > L->length) return error; *e = L->date[i - 1]; return ok; }
Status ListInsert(SqList *L, int i, elemtype e) { if (L->length == maxsize ) return error; if ( i < 1 || i > L->length + 1) return error; if (i <= L->length) { for (int k = L->length - 1; k >= i - 1; k--) L->date[k + 1] = L->date[k]; } L->date[i - 1] = e; L->length++;
return ok; }
Status ListDelete(SqList *L, int i) { if ( i < 1 || i > L->length + 1) return error; if (i <= L->length) { for (int k = L->length - 2; k >= i - 1; k--) L->date[k] = L->date[k + 1]; } L->length--;
return ok;
}
int ListLength(SqList L) { return L.length; }
Status ListTraverse(SqList L) { for (int i = 0; i < L.length; i++) { cout << L.date[i] << endl; } return ok; }
int main() { Status i; SqList L; i = InitList(&L); printf("初始化L后:L.length=%d\n", L.length); int a = 2; i = ListInsert(&L, 1, a); i = ListInsert(&L, 1, 5); i = ListTraverse(L); i = ListDelete(&L, 2); i = ListTraverse(L); return 0; }
|