线性表的顺序表示

线性表了有如下几个特征:

1.集合中必存在唯一的一个"第一个元素";

2.集合中必存在唯一的一个"最后的元素";

3.除最后元素之外,其它数据元素均有唯一的"后继";

4.除第一元素之外,其它数据元素均有唯一的"前驱"。

 

线性表的顺序表示指的是用物理上的一段连续的地址来存储数据元素。

如果第一个元素的在内存上的地址为a1,每个元素占用的空间是l,那么第n个元素的地址就是a1+(n-1) x l。

 

数组描述的线性表顺序结构表示:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #define TRUE 1
  5 #define FALSE 0
  6 #define OK 1
  7 #define ERROR 0
  8 #define INIT_SIZE 10        //初始化表长
  9 #define INCREMENT_SIZE 5    //分配增量
 10 
 11 typedef int Status;
 12 typedef int Elemtype;
 13 
 14 /*
 15  * 存储结构
 16  */
 17 typedef struct
 18 {
 19     Elemtype *elem;    //存储空间基址
 20     int length;        //当前长度
 21     int size;        //当前分配的表长大小
 22 }SqList;
 23 
 24 /*
 25  * 初始化一个空的线性表
 26  */
 27 Status InitList(SqList *L)
 28 {
 29     L->elem = (Elemtype *) malloc(INIT_SIZE * sizeof(Elemtype));
 30     if (!L->elem)
 31     {
 32         return ERROR;
 33     }
 34     L->length = 0;
 35     L->size = INIT_SIZE;
 36     return OK;
 37 }
 38 
 39 /*
 40  * 销毁线性表
 41  */
 42 Status DestroyList(SqList *L)
 43 {
 44     free(L->elem);
 45     L->length = 0;
 46     L->size = 0;
 47     return OK;
 48 }
 49 
 50 /*
 51  * 清空线性表
 52  */
 53 Status ClearList(SqList *L)
 54 {
 55     L->length = 0;
 56     return OK;
 57 }
 58 
 59 /*
 60  * 判断线性表是否为空
 61  */
 62 Status isEmpty(const SqList L)
 63 {
 64     if (0 == L.length)
 65     {
 66         return TRUE;
 67     }
 68     else
 69     {
 70         return FALSE;
 71     }
 72 }
 73 
 74 /*
 75  * 获取长度
 76  */
 77 Status getLength(const SqList L)
 78 {
 79     return L.length;
 80 }
 81 
 82 /*
 83  * 根据位置获取元素
 84  */
 85 Status GetElem(const SqList L, int i, Elemtype *e)
 86 {
 87     if (i < 1 || i > L.length)
 88     {
 89         return ERROR;
 90     }
 91     *e = L.elem[i-1];
 92     return OK;
 93 }
 94 
 95 /*
 96  * 比较两个元素是否相等
 97  */
 98 Status compare(Elemtype e1, Elemtype e2)
 99 {
100     if (e1 == e2)
101     {
102         return 0;
103     }
104     else if (e1 < e2)
105     {
106         return -1;
107     }
108     else
109     {
110         return 1;
111     }
112 }
113 
114 /*
115  * 查找元素
116  */
117 Status FindElem(const SqList L, Elemtype e, Status (*compare)(Elemtype, Elemtype))
118 {
119     int i;
120     for (i = 0; i < L.length; i++)
121     {
122         if (!(*compare)(L.elem[i], e))
123         {
124             return i + 1;
125         }
126     }
127     if (i >= L.length)
128     {
129         return ERROR;
130     }
131 }
132 
133 /*
134  * 查找前驱元素
135  */
136 Status PreElem(const SqList L, Elemtype cur_e, Elemtype *pre_e)
137 {
138     int i;
139     for (i = 0; i < L.length; i++)
140     {
141         if (cur_e == L.elem[i])
142         {
143             if (i != 0)
144             {
145                 *pre_e = L.elem[i - 1];
146             }
147             else
148             {
149                 return ERROR;
150             }
151         }
152     }
153     if (i >= L.length)
154     {
155         return ERROR;
156     }
157 }
158 
159 /*
160  * 查找后继元素
161  */
162 Status NextElem(const SqList L, Elemtype cur_e, Elemtype *next_e)
163 {
164     int i;
165     for (i = 0; i < L.length; i++)
166     {
167         if (cur_e == L.elem[i])
168         {
169             if (i < L.length - 1)
170             {
171                 *next_e = L.elem[i + 1];
172                 return OK;
173             }
174             else
175             {
176                 return ERROR;
177             }
178         }
179     }
180     if (i >= L.length)
181     {
182         return ERROR;
183     }
184 }
185 
186 /*
187  * 插入元素
188  */
189 Status InsertElem(SqList *L, int i, Elemtype e)
190 {
191     Elemtype *new_ele;
192     if (i < 1 || i > L->length + 1)
193     {
194         return ERROR;
195     }
196     if (L->length >= L->size)
197     {
198         new_ele = (Elemtype*) realloc(L->elem, (L->size + INCREMENT_SIZE) * sizeof(Elemtype));
199         if (!new_ele)
200         {
201             return ERROR;
202         }
203         L->elem = new_ele;
204         L->size += INCREMENT_SIZE;
205     }
206     Elemtype *p = &L->elem[i - 1];
207     Elemtype *q = &L->elem[L->length - 1];
208     for (; q >= p; q--)
209     {
210         *(q + 1) = *q;
211     }
212     *p = e;
213     ++L->length;
214     return OK;
215 }
216 
217 /*
218  * 删除元素并返回其值
219  */
220 Status DeleteElem(SqList *L, int i, Elemtype *e)
221 {
222     if (i < 1 || i > L->length)
223     {
224         return ERROR;
225     }
226     Elemtype *p = &L->elem[i - 1];
227     *e = *p;
228     for (; p < &L->elem[L->length]; p++)
229     {
230         *(p) = *(p + 1);
231     }
232     --L->length;
233     return OK;
234 }
235 
236 /*
237  * 访问元素
238  */
239 void visit(Elemtype e)
240 {
241     printf("%d ", e);
242 }
243 
244 /*
245  * 遍历线性表
246  */
247 Status TraverseList(const SqList L, void (*visit)(Elemtype))
248 {
249     int i;
250     for(i = 0; i < L.length; i++)
251     {
252         visit(L.elem[i]);
253     }
254     return OK;
255 }
256 
257 /*
258  * 主函数测试
259  */
260 int main()
261 {
262     SqList L;
263     if (InitList(&L))
264     {
265         Elemtype e;
266         printf("init_success\n");
267         int i;
268         for (i = 0; i < 10; i++)
269         {
270             InsertElem(&L, i + 1, i);
271         }
272         printf("length is %d\n", getLength(L));
273         if (GetElem(L, 1, &e)) {
274             printf("The first element is %d\n", e);
275         }
276         else
277         {
278             printf("element is not exist\n");        
279         }
280         if (isEmpty(L))
281         {
282             printf("list is empty\n");
283         }
284         else
285         {
286             printf("list is not empty\n");
287         }
288         printf("The 5 at %d\n", FindElem(L, 5, *compare));
289         PreElem(L, 6, &e);
290         printf("The 6's previous element is %d\n", e);
291         NextElem(L, 6, &e);
292         printf("The 6's next element is %d\n", e);
293         DeleteElem(&L, 1, &e);
294         printf("delete first element is %d\n", e);
295         printf("list:");
296         TraverseList(L,visit);
297         if (DestroyList(&L))
298         {
299             printf("\ndestroy_success\n");
300         }
301     }
302 }

 

posted @ 2015-05-24 01:02  niceforbear  阅读(316)  评论(0编辑  收藏  举报