seqlist.hc++
1 #ifndef __SEQLIST_H__ 2 #define __SEQLIST_H__ 3 4 #include<cstdio> 5 #include<malloc.h> 6 #include<assert.h> 7 #define SEQLIST_INIT_SIZE 8 8 #define INC_SIZE 3 //空間增量的大小 9 typedef int ElemType; 10 typedef struct Seqlist { 11 ElemType *base; 12 int capacity; //順序表容量 13 int size; //表的大小 14 }Seqlist; 15 16 bool Inc(Seqlist *list);//增長順序表的容量 17 void InitSeqlist(Seqlist *list); //初始化順序表 18 void push_back(Seqlist *list, ElemType x); //在順序表的末尾插入元素 19 void push_front(Seqlist *list, ElemType x); //在順序表的頭部插入元素 20 void show_list(Seqlist *list); //顯示順序表中的元素 21 void pop_back(Seqlist *list); //刪除順序表最後一個元素 22 void pop_front(Seqlist *list); //刪除順序表第一個元素 23 void insert_pos(Seqlist *list, int pos, ElemType x);//在順序表的選定位置上插入數據 24 int find(Seqlist *list, ElemType key); //在順序表中查找元素key的下標 25 int length(Seqlist *list);//求順序表的長度 26 void delete_pos(Seqlist *list, int pos); //刪除順序表中特定位置的數據元素 27 void delete_val(Seqlist *list, int key);//刪除順序表中值爲key的數據元素 28 void sort(Seqlist *list);//冒泡排序 29 void reverse(Seqlist *list);//逆置順序列表 30 void clear(Seqlist *list);//清除順序表中的全部元素 31 void destroy(Seqlist *list);//摧毀順序表 32 void merge(Seqlist *lt, Seqlist *la, Seqlist *lb);//合併兩個順序列表 33 34 #endif //__SEQLIST_H__
seqlist.cppui
1 #include"seqlist.h" 2 3 bool Inc(Seqlist *list) { 4 ElemType *newbase = (ElemType*)realloc(list, sizeof(ElemType)*(list->capacity + INC_SIZE)); //從新分配內存空間 5 if (newbase == NULL) { 6 printf("內存空間已滿,沒法再分配內存空間!\n"); 7 return false; 8 } 9 list->base = newbase; 10 list->capacity += INC_SIZE; 11 return true; 12 } 13 14 void InitSeqlist(Seqlist *list) { 15 list->base = (ElemType*)malloc(sizeof(ElemType)*SEQLIST_INIT_SIZE); 16 assert(list->base != NULL); 17 list->capacity = SEQLIST_INIT_SIZE; 18 list->size = 0; 19 } 20 21 void push_back(Seqlist *list, ElemType x) { 22 if (list->size >= list->capacity && !Inc(list)) { //Inc(list)用來判斷增長順序表容量是否成功,只有在失敗的狀況下才會進入if語句中 23 printf("順序表容量已滿,沒法再在表尾繼續插入新元素!\n"); 24 return; 25 } 26 list->base[list->size] = x; 27 list->size++; 28 } 29 30 void push_front(Seqlist *list, ElemType x) { 31 if (list->size >= list->capacity && !Inc(list)) { 32 printf("順序表容量已滿,沒法再在表頭插入新元素!\n"); 33 return; 34 } 35 for (int i = list->size;i > 0;i--) { 36 list->base[i] = list->base[i - 1]; 37 } 38 list->base[0] = x; 39 list->size++; 40 } 41 42 void show_list(Seqlist *list) { 43 for (int i = 0;i < list->size;i++) { 44 printf("%d ", list->base[i]); 45 } 46 printf("\n"); 47 } 48 49 void pop_back(Seqlist *list) { 50 if (list->size == 0) { 51 printf("順序表已空,沒法再在表尾刪除元素!\n"); 52 return; 53 } 54 list->size--; 55 } 56 57 void pop_front(Seqlist *list) { 58 if (list->size == 0) { 59 printf("順序表已空,沒法再在表頭刪除元素!\n"); 60 return; 61 } 62 for (int i = 0;i < list->size - 1;i++) { 63 list->base[i] = list->base[i + 1]; 64 } 65 list->size--; 66 } 67 68 void insert_pos(Seqlist *list, int pos, ElemType x) { 69 if (pos<0 || pos>list->size) { 70 printf("插入位置不合法,沒法插入元素!\n"); 71 return; 72 } 73 if (list->size >= list->capacity && !Inc(list)) { 74 printf("順序表容量已滿,沒法在插入新的元素!\n"); 75 return; 76 } 77 for (int i = list->size;i > pos;i--) { 78 list->base[i] = list->base[i - 1]; 79 } 80 list->base[pos] = x; 81 list->size++; 82 } 83 84 int find(Seqlist *list, ElemType key) { 85 for (int i = 0;i < list->size;i++) { 86 if (list->base[i] == key) 87 return i; 88 } 89 return -1; 90 } 91 92 int length(Seqlist *list) { 93 return list->size; 94 } 95 96 void delete_pos(Seqlist *list, int pos) { 97 if (pos < 0 || pos >= list->size) { 98 printf("刪除位置不合法,沒法刪除元素!\n"); 99 return; 100 } 101 for (int i = pos;i < list->size - 1;i++) { 102 list->base[i] = list->base[i + 1]; 103 } 104 list->size--; 105 } 106 107 void delete_val(Seqlist *list, int key) { 108 int pos = find(list, key); 109 if (pos == -1) { 110 printf("順序表中沒有這個元素!\n"); 111 return; 112 } 113 delete_pos(list, pos); 114 } 115 116 void sort(Seqlist *list) { 117 for (int i = 0;i < list->size - 1;i++) {//排序的趟數(例如5個數據須要比較4趟) 118 for (int j = 0;j < list->size - 1 - i;j++) {//每一趟比較中的比較次數(例如5個數據在第0趟須要比較4次) 119 if (list->base[j] > list->base[j + 1]) { 120 ElemType temp = list->base[j]; 121 list->base[j] = list->base[j + 1]; 122 list->base[j + 1] = temp; 123 } 124 } 125 } 126 } 127 128 void reverse(Seqlist *list) { 129 if (list->size == 0 || list->size == 1) return; 130 int low = 0, high = list->size - 1; 131 while (low < high) { 132 ElemType temp = list->base[low]; 133 list->base[low] = list->base[high]; 134 list->base[high] = temp; 135 low++; 136 high--; 137 } 138 } 139 140 void clear(Seqlist *list) { 141 list->size = 0; 142 } 143 144 void destroy(Seqlist *list) { 145 free(list->base); 146 list->base = NULL; 147 list->capacity = 0; 148 list->size = 0; 149 } 150 151 void merge(Seqlist *lt, Seqlist *la, Seqlist *lb) { 152 lt->capacity = la->size + lb->size; 153 lt->base = (ElemType*)malloc(sizeof(ElemType)*lt->capacity); 154 assert(lt->base != NULL); 155 156 int ia = 0, ib = 0, ic = 0; 157 while (ia < la->size&&ib < lb->size) { 158 if (la->base[ia] < lb->base[ib]) { 159 lt->base[ic++] = la->base[ia++]; 160 } 161 else { 162 lt->base[ic++] = lb->base[ib++]; 163 } 164 } 165 while (ia < la->size) { 166 lt->base[ic++] = la->base[ia++]; 167 } 168 while (ib < lb->size) { 169 lt->base[ic++] = lb->base[ib++]; 170 } 171 lt->size = la->size + lb->size; 172 show_list(lt); 173 }
main.cppurl
1 #include"seqlist.h" 2 3 void main() { 4 Seqlist list; 5 InitSeqlist(&list); 6 7 ElemType item; 8 int pos; 9 int select = 1; 10 while (select) { 11 printf("*******************************************\n"); 12 printf("*[1] push_back [2] push_front *\n"); 13 printf("*[3] show_list [4] pop_back *\n"); 14 printf("*[5] pop_front [6] insert_pos *\n"); 15 printf("*[7] find [8] length *\n"); 16 printf("*[9] delete_pos [10] delete_value *\n"); 17 printf("*[11] sort [12] reverse *\n"); 18 printf("*[13] clear [14] merge *\n"); 19 printf("*[0] quit_system *\n"); 20 printf("*******************************************\n"); 21 printf("請選擇:>>"); 22 scanf("%d", &select); 23 if (select == 0) break; 24 switch (select) { 25 case 1: 26 printf("請輸入要插入的數據(-1結束):>"); 27 while (scanf("%d", &item), item != -1) {//先輸入item的值,只要item不等於-1就接着循環 28 push_back(&list, item); 29 } 30 break; 31 case 2: 32 printf("請輸入要插入的數據(-1結束):>"); 33 while (scanf("%d", &item), item != -1) { 34 push_front(&list, item); 35 } 36 break; 37 case 3: 38 show_list(&list); 39 break; 40 case 4: 41 pop_back(&list); 42 break; 43 case 5: 44 pop_front(&list); 45 break; 46 case 6: 47 printf("請輸入要插入的數據:>"); 48 scanf("%d", &item); 49 printf("請輸入要插入的位置:>"); 50 scanf("%d", &pos); 51 insert_pos(&list, pos, item); 52 break; 53 case 7: 54 printf("請輸入要查找的數據:>"); 55 scanf("%d", &item); 56 pos = find(&list, item); 57 if (pos == -1) 58 printf("查找的數據元素不在順序表中!\n"); 59 else 60 printf("查找的數據元素在順序表中的下標位置爲%d\n", pos); 61 break; 62 case 8: 63 printf("順序表的長度爲%d\n", length(&list)); 64 break; 65 case 9: 66 printf("請輸入要刪除數據在順序表中的下標位置:>"); 67 scanf("%d", &pos); 68 delete_pos(&list, pos); 69 break; 70 case 10: 71 printf("請輸入要刪除數據的值:>"); 72 scanf("%d", &item); 73 delete_val(&list, item); 74 break; 75 case 11: 76 sort(&list); 77 break; 78 case 12: 79 reverse(&list); 80 break; 81 case 13: 82 clear(&list); 83 break; 84 case 14: 85 Seqlist mylist, yourlist; 86 ElemType item1, item2; 87 InitSeqlist(&mylist); 88 InitSeqlist(&yourlist); 89 printf("請輸入順序表1中的元素值(-1結束):>"); 90 while (scanf("%d", &item1), item1 != -1) { 91 push_back(&mylist, item1); 92 } 93 printf("請輸入順序表2中的元素值(-1結束):>"); 94 while (scanf("%d", &item2), item2 != -1) { 95 push_back(&yourlist, item2); 96 } 97 merge(&list, &mylist, &yourlist); 98 destroy(&mylist); 99 destroy(&yourlist); 100 break; 101 default: 102 printf("輸入的選擇錯誤!請從新輸入!\n"); 103 break; 104 } 105 } 106 destroy(&list); 107 }