1 #include <stdio.h>
2
3 #define MAZSIZE 20
4 typedef int ElemType;
5 typedef struct {
6 ElemType data[MAZSIZE];
7 int length;
8 }ArrList;
9
10 #define OK 1
11 #define ERROR 0
12 #define true 1;
13 #define false 0;
14 typedef int Status;
15
16 Status InitList(ArrList *L)
17 {
18 L->length = 0;
19 return OK;
20 }
21
22 Status ListEmpty(ArrList L)
23 {
24 if(L.length == 0)
25 return true;
26
27 return false;
28 }
29
30 Status ClearList(ArrList *L)
31 {
32 L->length = 0;
33
34 return OK;
35 }
36
37 Status GetElem(ArrList L , int i, ElemType *e)
38 {
39 if (i > L.length || i < 1 || L.length==0)
40 return ERROR;
41 *e = L.data[i-1];
42
43 return OK;
44 }
45
46 Status LocateElem(ArrList L, ElemType e)
47 {
48 if (L.length == 0)
49 return ERROR;
50 int i;
51 for (i = 0; i < L.length; i++)
52 {
53 if (L.data[i] == e)
54 break;
55 }
56 if (i >= L.length)
57 return ERROR;
58 return i + 1;
59 }
60
61 Status ListInsert(ArrList *L, int i, ElemType e)
62 {
63 if (L->length == MAZSIZE)
64 return ERROR;
65 if ((i<1) || (i> L->length+1))
66 return ERROR;
67 int j;
68 if (i <= L->length)
69 {
70 for (j = L->length; j > i - 1; j--)
71 {
72 L->data[j] = L->data[j - 1];
73 }
74 }
75 L->data[i-1] = e;
76 L->length++;
77 return OK;
78 }
79
80 Status ListDelete(ArrList *L, int i, ElemType *e)
81 {
82 if (L->length == 0)
83 return ERROR;
84 if ((i<1) && (i>L->length))
85 return ERROR;
86 int j;
87 *e = L->data[i - 1];
88 if (i < L->length)
89 {
90 for (j = i - 1; j < L->length; j++)
91 {
92 L->data[j] = L->data[j + 1];
93 }
94 }
95 L->length--;
96 return OK;
97 }
98
99 int ListLength(ArrList *L)
100 {
101 return L->length;
102 }
103
104 Status CreateList(ArrList *L)
105 {
106 int i;
107 for (i = 0; i<MAZSIZE; i++)
108 {
109 L->data[i] = i + 1;
110 L->length++;
111 }
112 return OK;
113 }
114 void TraverseList(const ArrList *L)
115 {
116 int i;
117 for (i = 0; i < L->length; i++)
118 printf(" %d ", L->data[i]);
119 printf("\n");
}