經過三天的時間終於把順序表的操作實現搞定了。(主要是在測試部分停留了太長時間)
1;線性表順序存儲的概念:指的是在內存中用一段地址連續的存儲單元依次存儲線性表中的元素。
2;采用的實現方式:一段地址連續的存儲單元可以用固定數組或者動態存儲結構來實現,這里采用動態分配存儲結構。
3;順序表的定義及操作集合:頭文件為defs.h
1 #ifndef _DEFS_H 2 #define _DEFS_H 3 4 #include<stdio.h> 5 #include<stdlib.h> 6 #include<malloc.h> 7 8 #define LIST_INIT_MAX 10 //長表為10 9 #define LIST_INCREMENT 2 //短表為2 10 typedef struct 11 { 12 int * elem; //采用動態存儲分配結構 13 int length; 14 int listsize; 15 }sqlist; 16 //線性表操作集合 17 void InitList(sqlist *L); //初始化,動態分配一塊存儲空間 18 void DestroyList(sqlist *L); //釋放這一段存儲空間(撤銷對應於動態) 19 void ClearList(sqlist *L); 20 void ListEmpty(sqlist L); 21 int ListLength(sqlist L); 22 int GetElem(sqlist L, int i, int *e); 23 void LocateList(sqlist L, int e); //在表中查找值為e的元素 24 int PriorElem(sqlist L, int cur_e, int *pri_e); //求當前元素的前驅 25 int NextElem(sqlist L, int cur_e, int *Nex_e); //求當前元素的后繼 26 int ListInsert(sqlist &L, int i, int e); //插入操作 27 int ListDelete(sqlist &L, int i, int *e); //刪除操作 28 void TravelList(sqlist L); //便歷操作 29 #endif
4;順序表結構體示意圖
5;InitList函數實現

1 #include"defs.h" 2 3 void InitList(sqlist *L) 4 { 5 L->elem = (int *)malloc(LIST_INIT_MAX*sizeof(int)); //初始化指針 6 if (!L->elem) 7 exit(-1); 8 L->length = 0; //初始化當前元素個數 9 L->listsize = LIST_INIT_MAX; //初始化表長 10 }
6;DestroyList函數實現

1 #include"defs.h" 2 3 void DestroyList(sqlist *L) 4 { 5 free(L->elem); //釋放空間 6 L->elem = NULL; //將指針置空 7 L->length = 0; //當前元素個數為0 8 L->listsize = 0; //表長為0 9 }
7;ClearList函數實現

1 #include"defs.h" 2 3 void ClearList(sqlist *L) 4 { 5 L->length = 0; //令當前元素個數為0 6 }
8;ListEmpty函數實現

1 #include"defs.h" 2 3 void ListEmpty(sqlist L) 4 { 5 if (L.length == 0) 6 printf("表為空.\n"); 7 else 8 printf("表不為空.\n"); 9 }
9;ListLength函數實現

1 #include"defs.h" 2 3 int ListLength(sqlist L) 4 { 5 return L.length; //返回表的當前元素個數 6 }
10;GetElem函數實現

1 #include"defs.h" 2 3 int GetElem(sqlist L, int i, int *e) //1<= i <=L.length 4 { 5 if (i<1 || i>L.length) 6 { 7 printf("取值位置不正確。\n"); 8 return 0; 9 } 10 *e = *(L.elem + i - 1); 11 return 0; 12 }
11;LocateElem函數實現

1 #include"defs.h" 2 3 void LocateElem(sqlist L, int e) 4 { 5 int i; 6 int * p = L.elem; 7 8 for (i=0; i<L.length; i++, p++) 9 if (*p == e) 10 printf("找到值為%d的元素,其位置為%d\n", e, i+1); 11 12 printf("在順序表中沒有找到值為%d的元素.\n", e); 13 }
12;PriorElem函數實現

1 PriorElem.c 2 #include"defs.h" 3 4 int PriorElem(sqlist L, int cur_e, int *pri_e) //第一個元素無前驅 5 { 6 int i; 7 int *p = L.elem; 8 9 for (i=0; i<L.length; i++,p++) //順序表長度已知,故用for循環 10 { 11 if (i==0 && *p==cur_e) 12 { 13 printf("當前元素為第一個元素,無前驅.\n"); 14 return 0; 15 } 16 if (*p == cur_e) //找到了當前元素且不是第一個元素, 17 { 18 *pri_e = *--p; //將其前驅賦給引用參數 19 return 0; 20 } 21 } 22 printf("順序表中無當前值%d。\n", cur_e); 23 return 0; 24 }
13;NextElem函數實現

1 NextElem.c 2 #include"defs.h" 3 4 int NextElem(sqlist L, int cur_e, int *nex_e) //最后一個元素無后繼 5 { 6 int i; 7 int *p = L.elem; 8 9 for (i=0; i<L.length; i++) //順序表長度已知,故用for 10 { 11 if (i==L.length-1 || *p==cur_e) 12 { 13 printf("當前元素為最后一個元素,無后繼.\n"); 14 return 0; 15 } 16 if (*p == cur_e) 17 { 18 *nex_e = *++p; //將后繼賦給引用參數帶回 19 return 0; 20 } 21 } 22 printf("順序表中無當前值%d。\n", cur_e); 23 return 0; 24 }
14;ListInsert函數實現

1 ListInsert.c 2 #include"defs.h" 3 4 int ListInsert(sqlist *L, int i, int e) //1<= i <=L->length+1 5 { 6 int *newbase, *p, *q; 7 8 if (i<1 || i>L->length+1) //插入位置不合理 9 { 10 printf("插入位置不合理.\n"); 11 return 0; 12 } 13 if (L->length == L->listsize) //表已滿 14 { 15 newbase = (int *)realloc(L->elem, (L->listsize + LIST_INCREMENT) * sizeof (int)); //用newbase指針是為了保護L->elem 16 if (!newbase) 17 { 18 printf("繼續分配內存空間失敗.\n"); 19 exit(-1); 20 } 21 L->listsize += LIST_INCREMENT; 22 } 23 p = L->elem + i - 1; //p指向插入的位置 24 q = L->elem + L->length - 1; //q指向表中元素最后一個位置 25 26 for (; q>=p; q--) //從最后一個元素開始依次向后移動表中元素 27 *(q+1) = *q; 28 *q = e; //插入元素 29 L->length++; // 表長增一 30 return 0; 31 }
15;ListDelete函數實現

1 ListDelete.c 2 3 #include"defs.h" 4 5 int ListDelete(sqlist *L, int i, int *e) //1<= i <=L->length 6 { 7 int *p, *q; 8 9 if (i<1 || i>L->length) 10 { 11 printf("刪除位置不合理.\n"); 12 return 0; 13 } 14 15 p = L->elem + i - 1; //p指向要刪除的元素的位置 16 q = L->elem + L->length - 1; //q指向表中最后一個元素位置 17 18 *e = *p; //將要刪除的元素保存起來 19 for (; p<=q; p++) //從要刪除元素的后面一個元素開始移動元素 20 *p = *(p+1); 21 L->length--; //表長減一 22 return 0; 23 }
16;TravelList函數實現

1 TravelList.c 2 3 #include "defs.h" 4 5 void TravelList(sqlist L) 6 { 7 int i 8 int *p = L.elem; 9 10 for (i=0; i<L.length; i++,p++) 11 { 12 printf("第%d個元素為:%d\n", i+1, *p); 13 } 14 15 }
17;makefile的實現
1 object = main.o InitList.o DestroyList.o ClearList.o ListEmpty.o \ 2 ListLength.o GetElem.o LocateElem.o PriorElem.o NextElem.o \ 3 ListInsert.o ListDelete.o TravelList.o 4 5 test : $(object) 6 gcc -g -Wall -o test $(object) 7 main.o : defs.h 8 InitList.o : defs.h 9 DestroyList.o : defs.h 10 ClearList.o : defs.h 11 ListEmpty.o : defs.h 12 ListLength.o : defs.h 13 GetElem.o : defs.h 14 LocateElem.o : defs.h 15 PriorElem.o : defs.h 16 NextElem.o : defs.h 17 ListInsert.o : defs.h 18 ListDelete.o : defs.h 19 TravelList.o : defs.h 20 21 .PHONY : clean 22 clean : 23 rm *.o -f
18;順序表的優缺點:順序表由於其存儲結構的特點,特別適合查找(其時間復雜度為O(1)),不適合頻繁插入和刪除(每一次插入和刪除的時間復雜度都是O(n))