靜態順序表的實現

?????實現對順序表的初始化,頭插,頭刪,尾插,尾刪, 任意下標的刪除, 任意下標處的的元素刪除,任意下標處的元素插入,任意元素的下標返回,任意下標處的元素返回, 刪除某個元素, 刪除線性表中的所有元素,對線性表判空處理, 以及檢查線性表有幾個元素
?????1.定義一個線性表
?????所謂的線性表就是一段地址連續的存儲單元一次存儲數據元素的線性結構,而連續的地址空間采用數組的結構,但數組有靜態的也有動態的,這里先討論靜態線性表的實現

這里寫圖片描述

?????由上圖可以看出,靜態線性表包括了元素以及元素的個數,因此定義一個線性表就是定義一個結構體
 7 typedef struct Seqlist8 {9         int size;10         SeqListType data[SeqListMaxSize];11 }SeqList;//定義一個順序表
?????定義好結構體后便需要對結構體進行初始化了,對其初始化,只需要將其元素的個數設為零即可。
 11 void InitSeqList(SeqList* seqlist)12 {13         seqlist -> size = 0;14 }
?????2.對線性表的頭插及就是在保證線性表元素個數不超過MAXSIZE的前提下每次將數據插入下標為零的位置處
111 void SeqListPushFront(SeqList* seqlist, SeqListType value)
112 {
113         if(seqlist == NULL)
114         {
115                 return;//非法輸入
116         }
117         if(seqlist -> size == SeqListMaxSize)
118         {
119                 return;//線性表已滿
120         }
121         seqlist -> size++;
122         int i = 0;
123         for(i = (seqlist -> size) - 1  ; i > 0; i--)
124         {
125                 seqlist -> data[i] = seqlist -> data[i - 1];
126         }
127         seqlist -> data[0] = value;
128 }
?????3.對線性表的頭刪就是只要線性表不為空,就將先行表的第1號下標的元素到第size - 1號元素一次向前移動

???????????????????????????這里寫圖片描述

153 void SeqListPopFront(SeqList* seqlist)
154 {
155         if(seqlist == NULL)
156         {
157                 return;
158         }
159         if(seqlist -> size == 0)
160         {
161                 return;
162         }
163         seqlist -> size--;
164         int i = 0;
165         for(i = 1; i <= seqlist -> size; i++)
166         {
167                 seqlist -> data[i -1] = seqlist -> data[i];
168         }
169 }
?????4.對線性表進行尾插,即就是先將線性表的size加1,再定義定義一個計數器讓其指向線性表的結尾,每次將新元素出入其末尾即
22 void SeqListPushBack(SeqList* seqlist, SeqListType value)23 {24         if(seqlist == NULL)25         {26                 return;//非法輸入       27         }28         if(seqlist -> size == SeqListMaxSize)29         {30                 return;//線性表已滿31         }32         seqlist -> size++;33         seqlist -> data[seqlist -> size - 1] = value;34 }

???????????????????????????這里寫圖片描述

?????5.對線性表的尾刪很簡單,就是將線性表的大小size減1即可
 72 void SeqListPopBash(SeqList* seqlist)73 {74         if(seqlist == NULL)75         {76                 return;//非法輸入77         }78         if(seqlist -> size == 0)79         {80                 return;//線性表為空81         }82         seqlist -> size--;83 }
?????6.對線性表的任意位置進行插入和刪除
?????(1)首先在對其插入時必須判斷線性表是否已滿,以及傳來的指針是否為空指針,其次,當刪除的時候也必須判斷線性表是否為空。當滿足以上要求時,再進行插入或者刪除才是合理的。在對線性表任意位置插入是及就是定義一個計數其,讓其等于要插入的下標,然后將此處的元素以及后面所有的元素都向后移動,最后將需要插入的元素插入到對應的下標位置處。

???????????????????????????這里寫圖片描述

198 void SeqListInsert(SeqList* seqlist, int pos, SeqListType value)
199 {
200         if(seqlist == NULL)
201         {
202                 return;//非法輸入
203         }
204         if(pos == 0)
205         {
206                 SeqListPushFront(seqlist, value);
207         }
208         if(pos > seqlist -> size)
209         {
210                 return;
211         }
212         seqlist -> size++;
213         int i = 0;
214         for( i = (seqlist -> size) - 1; i >= pos + 1; i--)
215         {
216                 seqlist -> data[i] = seqlist -> data[i - 1];
217         }
218         seqlist -> data[pos] = value;
219 
220 }
?????(2)對線性表任意元素的刪除就是先找到這個元素,然后將這個元素后面的所有元素向前一次移動,

???????????????????????????這里寫圖片描述

428 void SeqListRemove(SeqList* seqlist, SeqListType to_remove)
429 {
430         if(seqlist == NULL)
431         {
432                 return;
433         }
434         if(seqlist -> size == 0)
435         {
436                 return;
437         }
438         int pos = SeqListFind(seqlist, to_remove);//找到 to_remove的下標
439         if(pos == -1)
440         {
441                 return;//沒有找到
442         }
443         SeqListErase(seqlist, pos);
444 }
?????7.返回線性表中某個元素的下標即先定義一個計數器,拿著這個元素對線性表進行便利,將這個元素與線性表中的元素進行比較,如果不相等計數器加1,指針后移,直到相等的時候返回i值

???????????????????????????這里寫圖片描述

381 int SeqListFind(SeqList* seqlist, SeqListType to_find)
382 {
383         if(seqlist == NULL)
384         {
385                 return -1;//非法輸入
386         }
387         if(seqlist -> size == 0)
388         {
389                 return -1;//非法輸入
390         }
391         int i = 0;
392         for(i = 0; i < seqlist -> size; i++)
393         {
394                 if(to_find == seqlist -> data[i])
395                 {
396                         return i;
397                 }
398         }
399         return -1;
400 }
?????8.刪除某個下標對應的元素就是定義一個指針指向該位置,然后將后面的元素一次賦值給前一個元素即可,最后將size減1

?????????????????這里寫圖片描述

249 void SeqListErase(SeqList* seqlist, int pos)
250 {
251         if(seqlist == NULL)
252         {
253                 return;
254         }
255         if(pos < 0 || pos >= seqlist -> size)
256         {
257                 return;//非法位置
258         }
259         if(seqlist -> size == 0)
260         {
261                 return;//線性表為空
262         }
263         int i = 0;
264         for(i = pos; i < seqlist -> size - 1; i ++)
265         {
266                 seqlist -> data[i] = seqlist -> data[i + 1];
267         }
268         seqlist -> size--;
269 
270 }
?????9.最后利用冒泡排序對其進行排序
518 void Swap(SeqListType* a, SeqListType* b)
519 {
520         SeqListType tmp = 0;
521         tmp = *a;
522         *a = *b;
523         *b = tmp;
524 }
576 int Greater(SeqListType* a, SeqListType* b)
577 {
578         return *a < *b;
579 }
580
581 void SeqListBubbleSortEx(SeqList* seqlist, Cmp Greater)
582 {
583         if(seqlist == NULL)
584         {
585                 return;//非法輸入
586         }
587         int count = 0;
588         for(count = 0; count < seqlist -> size -1; count++)
589         {
590                 int current = 0;
591                 for(current = 0; current < seqlist -> size -1 - count; current++)
592                 {
593                         if(!Greater(&seqlist -> data[current], &seqlist -> data[current + 1]))
594                         {
595                         Swap(&seqlist -> data[current], &seqlist -> data[current +1]);
596                         }
597 
598                 }
599         }
600 
601 }
?????10.計算線性表大小
631 int SeqListSize(SeqList* seqlist)
632 {
633         if(seqlist == NULL)
634         {
635                 return;
636         }
637         return seqlist -> size;
638 }
?????11.查看先行表是否為空
676 void TestEmpty()
677 {
678         TESTHEADER;
679         SeqList seqlist;
680         InitSeqList(&seqlist);
681         SeqListPushFront(&seqlist, '1');
682         SeqListPushFront(&seqlist, '6');
683         SeqListPushFront(&seqlist, '3');
684         SeqListPushFront(&seqlist, 'd');
685         SeqListPushFront(&seqlist, 'z');
686         SeqListPushFront(&seqlist, 'l');
687         SeqListPushFront(&seqlist, 'q');
688         SeqListPushFront(&seqlist, 'a');
689         int ret = SeqListEmpty(&seqlist);
690         printf("expected 0, actual %d\n", ret);
691 
692 }
?????12.設置下標為 pos 處的元素為 value
299 int SeqListSet(SeqList* seqlist, int pos, SeqListType value)
300 {
301         if(seqlist == NULL)
302         {
303                 return 1;
304         }
305         if(pos < 0 || pos > seqlist -> size)
306         {
307                 return 1;
308         }
309         seqlist -> data[pos] = value;
310 }
????? 13.返回下標為 pos 的元素的值
340 int SeqListGet(SeqList* seqlist, int pos, SeqListType* value)
341 {
342         if(seqlist == NULL)
343         {
344                 return 1;
345         }
346         if(seqlist -> size == 0)
347         {
348                 return 1;
349         }
350         *value = seqlist -> data[pos];
351         return 0;
352 }
?????14.整體代碼
  //seqlist.h文件1 #define pragama once2 3 #define  SeqListType char4 5 #define SeqListMaxSize 10006 7 typedef struct Seqlist8 {9         int size;10         SeqListType data[SeqListMaxSize];11 }SeqList;//定義一個順序表12 13 typedef int(*Cmp)(SeqListType*, SeqListType*);//函數指針的定義14 15 void SeqListInit(SeqList *seqlist);//初始化線性表16 17 void TestPrint(SeqList *seqlist);//打印18 19 void SeqListPushBack(SeqList* seqlist, SeqListType value);//尾插20 21 void SeqListPopBack(SeqList* seqlist);//尾刪22 23 void SeqlistPushFront(SeqList* seqlist, SeqListType value);//頭插24 25 void SeqListPopFront(SeqList* seqlist);//頭刪26 27 void SeqListInsert(SeqList* seqlist, int pos, SeqListType value);//在 pos 下標處插處 value28 29 void SeqListErase(SeqList* seqlist, int pos);//刪除 pos 下標處的元素30 31 int SeqListSet(SeqList* seqlist, int pos, SeqListType value);//設置下標 pos 處的元素值為 value32 33 int SeqListGet(SeqList* seqlist, int pos, SeqListType* value);//返回下標是 pos 的元素的值34 35 int SeqListFind(SeqList* seqlist, SeqListType to_find);//返回 to_find 的下標36 37 void SeqListRemove(SeqList* seqlist, SeqListType to_remove);//刪除某個元素 to_remove38 39 void SeqListRemoveAll(SeqList* seqlist, SeqListType to_remove);//刪除線性表中所有的 to_remove40 41 void SeqListBubbleSort(SeqList* seqlist);//對線性表的元素進行排序42 43 void SeqListBubbleSortEx(SeqList* seqlist, Cmp Greater);44 45 int SeqListSize(SeqList* seqlist);//獲取線性表的元素個數46 47 int SeqListEmpty(SeqList* seqlist);//判斷線性表是否為空                                                                                                                                                                
1 #include"seqlist.h"2 #include<stdio.h>3 4 #define TESTHEADER printf("==================%s============================\n", __FUNCTION__)5 /*6  *7  * 初始化8  *9  */10 11 void InitSeqList(SeqList* seqlist)12 {13         seqlist -> size = 0;14 }15 16 /*17  *18  * 尾插19  *20  *21  */22 void SeqListPushBack(SeqList* seqlist, SeqListType value)23 {24         if(seqlist == NULL)25         {26                 return;//非法輸入       27         }28         if(seqlist -> size == SeqListMaxSize)29         {30                 return;//線性表已滿31         }32         seqlist -> size++;33         seqlist -> data[seqlist -> size - 1] = value;34 }35 /36 ///測試代碼//37 /38 void TestPushBack()39 {40         TESTHEADER;41         SeqList seqlist;42         InitSeqList(&seqlist);43         SeqListPushBack(&seqlist, 'a');44         SeqListPushBack(&seqlist, 'c');45         SeqListPushBack(&seqlist, 'd');46         SeqListPushBack(&seqlist, 'b');47         SeqListPushBack(&seqlist, 'z');48         SeqListPushBack(&seqlist, 'g');49         SeqListPushBack(&seqlist, 'q');50         TestPrint(&seqlist);51 }52 ///53 //測試代碼/54 ///55 void TestPrint(SeqList* seqlist)56 {57         TESTHEADER;58         printf("size = %d\n", seqlist -> size);59         int i = 0;60         for(i = 0; i < seqlist -> size; i++)61         {62                 printf("[%c] ", seqlist -> data[i]);63         }64         printf("\n");65 }66 67 /*68  *69  * 尾刪70  *71  */72 void SeqListPopBash(SeqList* seqlist)73 {74         if(seqlist == NULL)75         {76                 return;//非法輸入77         }78         if(seqlist -> size == 0)79         {80                 return;//線性表為空81         }82         seqlist -> size--;83 }84 85 /86 測試代碼/87 /88 void TestPopBash()89 {90         TESTHEADER;91         SeqList seqlist;92         InitSeqList(&seqlist);93         SeqListPushBack(&seqlist, 'a');94         SeqListPushBack(&seqlist, 'd');95         SeqListPushBack(&seqlist, 'g');96         SeqListPushBack(&seqlist, 't');97         SeqListPushBack(&seqlist, 'x');98         SeqListPushBack(&seqlist, 'z');99         printf("尾刪前\n");
100         TestPrint(&seqlist);
101         SeqListPopBash(&seqlist);
102         printf("尾刪后\n");
103         TestPrint(&seqlist);
104 }
105 
106 /*
107  *
108  * 頭插
109  *
110  */
111 void SeqListPushFront(SeqList* seqlist, SeqListType value)
112 {
113         if(seqlist == NULL)
114         {
115                 return;//非法輸入
116         }
117         if(seqlist -> size == SeqListMaxSize)
118         {
119                 return;//線性表已滿
120         }
121         seqlist -> size++;
122         int i = 0;
123         for(i = (seqlist -> size) - 1  ; i > 0; i--)
124         {
125                 seqlist -> data[i] = seqlist -> data[i - 1];
126         }
127         seqlist -> data[0] = value;
128 }
129 /
130 //測試代碼///
131 /
132 
133 void TestPushFront()
134 {
135 
136         TESTHEADER;
137         SeqList seqlist;
138         InitSeqList(&seqlist);
139         SeqListPushFront(&seqlist, 'a');
140         SeqListPushFront(&seqlist, 'd');
141         SeqListPushFront(&seqlist, 'g');
142         SeqListPushFront(&seqlist, 't');
143         SeqListPushFront(&seqlist, 'x');
144         SeqListPushFront(&seqlist, 'z');
145         TestPrint(&seqlist);
146 }
147 
148 /*
149  *
150  * 頭刪
151  *
152  */
153 void SeqListPopFront(SeqList* seqlist)
154 {
155         if(seqlist == NULL)
156         {
157                 return;
158         }
159         if(seqlist -> size == 0)
160         {
161                 return;
162         }
163         seqlist -> size--;
164         int i = 0;
165         for(i = 1; i <= seqlist -> size; i++)
166         {
167                 seqlist -> data[i -1] = seqlist -> data[i];
168         }
169 }
170 /
171 //測試代碼///
172 /
173 
174 void TestPopFront()
175 {
176         TESTHEADER;
177         SeqList seqlist;
178         InitSeqList(&seqlist);
179         SeqListPushFront(&seqlist, 'r');
180         SeqListPushFront(&seqlist, 'w');
181         SeqListPushFront(&seqlist, 'q');
182         SeqListPushFront(&seqlist, 'c');
183         SeqListPushFront(&seqlist, 'z');
184         SeqListPushFront(&seqlist, 'd');
185         printf("刪除前\n");
186         TestPrint(&seqlist);
187         SeqListPopFront(&seqlist);
188         printf("頭刪一個元素\n");
189         TestPrint(&seqlist);
190 }
191 
192 /*
193  *
194  * 在 pos 處插入元素 value
195  *
196  */
197 
198 void SeqListInsert(SeqList* seqlist, int pos, SeqListType value)
199 {
200         if(seqlist == NULL)
201         {
202                 return;//非法輸入
203         }
204         if(pos == 0)
205         {
206                 SeqListPushFront(seqlist, value);
207         }
208         if(pos > seqlist -> size)
209         {
210                 return;
211         }
212         seqlist -> size++;
213         int i = 0;
214         for( i = (seqlist -> size) - 1; i >= pos + 1; i--)
215         {
216                 seqlist -> data[i] = seqlist -> data[i - 1];
217         }
218         seqlist -> data[pos] = value;
219 
220 }
221 
222 /
223 //測試代碼///
224 /
225 void TestInsert()
226 {
227         TESTHEADER;
228         SeqList seqlist;
229         InitSeqList(&seqlist);
230         SeqListPushFront(&seqlist, 'r');
231         SeqListPushFront(&seqlist, 'w');
232         SeqListPushFront(&seqlist, 'q');
233         SeqListPushFront(&seqlist, 'c');
234         SeqListPushFront(&seqlist, 'z');
235         SeqListPushFront(&seqlist, 'd');
236         printf("插入前\n");
237         TestPrint(&seqlist);
238         SeqListInsert(&seqlist, 4, 'A');
239         printf("在下標為4的位置插入一個元素\n");
240         TestPrint(&seqlist);
241 }
242 
243 /*
244  *
245  * 刪除下標為 pos 的元素
246  *
247  */
248 
249 void SeqListErase(SeqList* seqlist, int pos)
250 {
251         if(seqlist == NULL)
252         {
253                 return;
254         }
255         if(pos < 0 || pos >= seqlist -> size)
256         {
257                 return;//非法位置
258         }
259         if(seqlist -> size == 0)
260         {
261                 return;//線性表為空
262         }
263         int i = 0;
264         for(i = pos; i < seqlist -> size - 1; i ++)
265         {
266                 seqlist -> data[i] = seqlist -> data[i + 1];
267         }
268         seqlist -> size--;
269 
270 }
271 
272 /
273 //測試代碼///
274 /
275 void TestErase()
276 {
277         TESTHEADER;
278         SeqList seqlist;
279         InitSeqList(&seqlist);
280         SeqListPushFront(&seqlist, '1');
281         SeqListPushFront(&seqlist, '6');
282         SeqListPushFront(&seqlist, '3');
283         SeqListPushFront(&seqlist, 'd');
284         SeqListPushFront(&seqlist, 'z');
285         SeqListPushFront(&seqlist, 'l');
286         SeqListPushFront(&seqlist, 'q');
287         SeqListPushFront(&seqlist, 'a');
288         TestPrint(&seqlist);
289         printf("刪除下標為3的元素\n");
290         SeqListErase(&seqlist, 3);
291         TestPrint(&seqlist);
292 }
293 
294 /*
295  *
296  * 設置下標為 pos 處的元素為 value
297  *
298  */
299 int SeqListSet(SeqList* seqlist, int pos, SeqListType value)
300 {
301         if(seqlist == NULL)
302         {
303                 return 1;
304         }
305         if(pos < 0 || pos > seqlist -> size)
306         {
307                 return 1;
308         }
309         seqlist -> data[pos] = value;
310 }
311 
312 /
313 //測試代碼///
314 /
315 
316 void TestSet()    
317 {
318         TESTHEADER;
319         SeqList seqlist;
320         InitSeqList(&seqlist);
321         SeqListPushFront(&seqlist, '1');
322         SeqListPushFront(&seqlist, '6');
323         SeqListPushFront(&seqlist, '3');
324         SeqListPushFront(&seqlist, 'd');
325         SeqListPushFront(&seqlist, 'z');
326         SeqListPushFront(&seqlist, 'l');
327         SeqListPushFront(&seqlist, 'q');
328         SeqListPushFront(&seqlist, 'a');
329         printf("設置前\n");
330         TestPrint(&seqlist);
331         SeqListSet(&seqlist, 3, 'Q');
332         printf("設置后 : data[3] = 'Q'\n");
333         TestPrint(&seqlist);
334 }
335 /*
336  *
337  * 返回下標為 pos 的元素的值
338  *
339  */
340 int SeqListGet(SeqList* seqlist, int pos, SeqListType* value)
341 {
342         if(seqlist == NULL)
343         {
344                 return 1;
345         }
346         if(seqlist -> size == 0)
347         {
348                 return 1;
349         }
350         *value = seqlist -> data[pos];
351         return 0;
352 }
353 /
354 /測試代碼
355 /
356 
357 void TestGet()
358 {
359         TESTHEADER;
360         SeqList seqlist;
361         InitSeqList(&seqlist);
362         SeqListPushFront(&seqlist, '1');
363         SeqListPushFront(&seqlist, '6');
364         SeqListPushFront(&seqlist, '3');
365         SeqListPushFront(&seqlist, 'd');
366         SeqListPushFront(&seqlist, 'z');
367         SeqListPushFront(&seqlist, 'l');
368         SeqListPushFront(&seqlist, 'q');
369         SeqListPushFront(&seqlist, 'a');
370         SeqListType value = 0;
371         SeqListGet(&seqlist, 5, &value);
372         printf("expected ret = 3\n");
373         printf("actual ret = %c\n", value);
374 
375 }
376 /* 
377  *
378  *找元素 to_find 并且返回其下標
379  *
380  */
381 int SeqListFind(SeqList* seqlist, SeqListType to_find)
382 {
383         if(seqlist == NULL)
384         {
385                 return -1;//非法輸入
386         }
387         if(seqlist -> size == 0)
388         {
389                 return -1;//非法輸入
390         }
391         int i = 0;
392         for(i = 0; i < seqlist -> size; i++)
393         {
394                 if(to_find == seqlist -> data[i])
395                 {
396                         return i;
397                 }
398         }
399         return -1;
400 }
401 ///
402 ///測試代碼
403 ///
404 void TestFind()
405 {
406         TESTHEADER;
407         SeqList seqlist;
408         InitSeqList(&seqlist);
409         SeqListPushFront(&seqlist, '1');
410         SeqListPushFront(&seqlist, '6');
411         SeqListPushFront(&seqlist, '3');
412         SeqListPushFront(&seqlist, 'd');
413         SeqListPushFront(&seqlist, 'z');
414         SeqListPushFront(&seqlist, 'l');
415         SeqListPushFront(&seqlist, 'q');
416         SeqListPushFront(&seqlist, 'a');
417         int ret = SeqListFind(&seqlist, 'l');
418         printf("expected ret = 2\n");
419         printf("actual ret = %d\n", ret);
420 }
421 
422 /*
423  *
424  * 刪除某個元素 to_remove
425  *
426  */
427 
428 void SeqListRemove(SeqList* seqlist, SeqListType to_remove)
429 {
430         if(seqlist == NULL)
431         {
432                 return;
433         }
434         if(seqlist -> size == 0)
435         {
436                 return;
437         }
438         int pos = SeqListFind(seqlist, to_remove);//找到 to_remove的下標
439         if(pos == -1)
440         {
441                 return;//沒有找到
442         }
443         SeqListErase(seqlist, pos);
444 }
445 
446 
447 ///測試代碼/
448 
449 void TestRemove()
450 {
451         TESTHEADER;
452         SeqList seqlist;
453         InitSeqList(&seqlist);
454         SeqListPushFront(&seqlist, '1');
455         SeqListPushFront(&seqlist, '6');
456         SeqListPushFront(&seqlist, '3');
457         SeqListPushFront(&seqlist, 'd');
458         SeqListPushFront(&seqlist, 'z');
459         SeqListPushFront(&seqlist, 'l');
460         SeqListPushFront(&seqlist, 'q');
461         SeqListPushFront(&seqlist, 'a');
462         printf("expected : 把3刪除\n");
463         TestPrint(&seqlist);
464         printf("actual : ");
465         SeqListRemove(&seqlist, '3');
466         TestPrint(&seqlist);
467 }
468 
469 /*
470  *
471  * 刪除所有 to_remove 的元素
472  *
473  */
474 void SeqListRemoveAll(SeqList* seqlist, SeqListType to_remove)
475 {
476 
477         if(seqlist == NULL)
478         {
479                 return;//非法輸入
480         }
481         if(seqlist -> size == 0)
482         {
483                 return;//線性表為空
484         }
485         int pos = 0;
486         while(1)
487         {
488                 pos = SeqListFind(seqlist, to_remove);
489                 if(pos == -1)
490                 {
491                         return;
492                 }
493                 SeqListErase(seqlist, pos);
494         }
495 }
496 
497 
498 測試代碼
499 
500 void TestRemoveAll()
501 {
502         TESTHEADER;
503         SeqList seqlist;
504         InitSeqList(&seqlist);
505         SeqListPushFront(&seqlist, '3');
506         SeqListPushFront(&seqlist, '3');
507         SeqListPushFront(&seqlist, '1');
508         SeqListPushFront(&seqlist, '2');
509         SeqListPushFront(&seqlist, '3');
510         SeqListPushFront(&seqlist, 'a');
511         SeqListPushFront(&seqlist, 'b');
512         printf("刪除前\n");
513         TestPrint(&seqlist);
514         printf("刪除元素中所有的3\n");
515         SeqListRemoveAll(&seqlist, '3');
516         TestPrint(&seqlist);
517 }
518 void Swap(SeqListType* a, SeqListType* b)
519 {
520         SeqListType tmp = 0;
521         tmp = *a;
522         *a = *b;
523         *b = tmp;
524 }
525 /*
526  *
527  * 排序
528  *
529  *
530  */
531 void SeqListBubbleSort(SeqList* seqlist)
532 {
533         if(seqlist == NULL)
534         {
535                 return;
536         }
537         int count = 0;
538         for(count = 0; count < seqlist -> size -1; count++)
539         {
540                 int current = 0 ;
541                 for(current = 0; current < seqlist -> size -count - 1; current++)
542                 {
543                         if(seqlist -> data[current] > seqlist -> data[current + 1])
544                         {
545                                 Swap(&seqlist -> data[current], &seqlist -> data[current + 1]);
546                         }
547                 }
548         }
549 }
550 
551 ///測試代碼/
552 
553 void TestBubbleSort()
554 {
555         TESTHEADER;
556         SeqList seqlist;
557         InitSeqList(&seqlist);
558         SeqListPushFront(&seqlist, '1');
559         SeqListPushFront(&seqlist, '6');
560         SeqListPushFront(&seqlist, '3');
561         SeqListPushFront(&seqlist, 'd');
562         SeqListPushFront(&seqlist, 'z');
563         SeqListPushFront(&seqlist, 'l');
564         SeqListPushFront(&seqlist, 'q');
565         SeqListPushFront(&seqlist, 'a');
566         TestPrint(&seqlist);
567         SeqListBubbleSort(&seqlist);
568         TestPrint(&seqlist);
569 }
570 
571 /*
572  *
573  * 利用函數指針實現排序
574  *
575  */
576 int Greater(SeqListType* a, SeqListType* b)
577 {
578         return *a < *b;
579 }
580 
581 void SeqListBubbleSortEx(SeqList* seqlist, Cmp Greater)
582 {
583         if(seqlist == NULL)
584         {
585                 return;//非法輸入
586         }
587         int count = 0;
588         for(count = 0; count < seqlist -> size -1; count++)
589         {
590                 int current = 0;
591                 for(current = 0; current < seqlist -> size -1 - count; current++)
592                 {
593                         if(!Greater(&seqlist -> data[current], &seqlist -> data[current + 1]))
594                         {
595                         Swap(&seqlist -> data[current], &seqlist -> data[current +1]);
596                         }
597 
598                 }
599         }
600 
601 }
602 ///
603 //測試代碼//
604
605 
606 void TestBubbleSortEx()
607 {
608         TESTHEADER;
609         SeqList seqlist;
610         InitSeqList(&seqlist);
611         SeqListPushFront(&seqlist, '1');
612         SeqListPushFront(&seqlist, '6');
613         SeqListPushFront(&seqlist, '3');
614         SeqListPushFront(&seqlist, 'd');
615         SeqListPushFront(&seqlist, 'z');
616         SeqListPushFront(&seqlist, 'l');
617         SeqListPushFront(&seqlist, 'q');
618         SeqListPushFront(&seqlist, 'a');
619         printf("排序前\n");
620         TestPrint(&seqlist);                                       
621         printf("排序后\n");
622         SeqListBubbleSortEx(&seqlist, Greater);
623         TestPrint(&seqlist);
624 }
625 
626 /*
627  *
628  * 獲取線性表元素的個數
629  *
630  */
631 int SeqListSize(SeqList* seqlist)
632 {
633         if(seqlist == NULL)
634         {
635                 return;
636         }
637         return seqlist -> size;
638 }
639 
640 //
641 //測試代碼
642 //
643 
644 void TestSize()
645 {
646         TESTHEADER;
647         SeqList seqlist;
648         InitSeqList(&seqlist);
649         SeqListPushFront(&seqlist, '1');
650         SeqListPushFront(&seqlist, '6');
651         SeqListPushFront(&seqlist, '3');
652         SeqListPushFront(&seqlist, 'd');
653         SeqListPushFront(&seqlist, 'z');
654         SeqListPushFront(&seqlist, 'l');
655         SeqListPushFront(&seqlist, 'q');
656         SeqListPushFront(&seqlist, 'a');
657         printf("expected size = 8\n");
658         int size = SeqListSize(&seqlist);
659         printf("actual size = %d\n", size);
660 }
661 
662 /*
663  *
664  * 判斷線性表是否為空為空返回 1,不為空返回 0
665  *
666  */
667 int SeqListEmpty(SeqList* seqlist)
668 {
669         return seqlist -> size ? 0 : 1;
670 }
671 
672 /
673 /測試代碼
674 /
675 
676 void TestEmpty()
677 {
678         TESTHEADER;
679         SeqList seqlist;
680         InitSeqList(&seqlist);
681         SeqListPushFront(&seqlist, '1');
682         SeqListPushFront(&seqlist, '6');
683         SeqListPushFront(&seqlist, '3');
684         SeqListPushFront(&seqlist, 'd');
685         SeqListPushFront(&seqlist, 'z');
686         SeqListPushFront(&seqlist, 'l');
687         SeqListPushFront(&seqlist, 'q');
688         SeqListPushFront(&seqlist, 'a');
689         int ret = SeqListEmpty(&seqlist);
690         printf("expected 0, actual %d\n", ret);
691 
692 }
693 int main()
694 {
695         TestPushFront();
696         TestPushBack();
697         TestErase();
698         TestInsert();
699         TestPopFront();
700         TestPopBash();
701         TestSet();
702         TestGet();
703         TestFind();
704 
705         TestRemove();
706         TestRemoveAll();
707 
708 
709         //TestBubbleSort();
710 
711         TestBubbleSortEx();
712 
713         TestSize();
714 
715         TestEmpty();
716         return 0;
717 }
??????15.運行結果如下

????????????這里寫圖片描述
????????????這里寫圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384126.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384126.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384126.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

樹鏈剖分入門+HYSBZ - 1036樹的統計Count

今天學習了樹鏈剖分&#xff0c;記錄一下。 【題目背景】 HYSBZ - 1036樹的統計Count 【題目分析】 題目要求求任意結點之間路徑的和以及路徑上最大的結點&#xff0c;還有可能修改。如果正常做可能會很復雜&#xff08;我也不知道正常應該怎么做&#xff0c;應該要用到LCA什么…

Socket網絡編程--小小網盤程序(5)

http://www.cnblogs.com/wunaozai/p/3893469.html 各位好呀&#xff01;這一小節應該就是這個小小網盤程序的最后一小節了&#xff0c;這一節將實現最后的三個功能&#xff0c;即列出用戶在服務器中的文件列表&#xff0c;還有刪除用戶在服務器中的文件&#xff0c;最后的可以共…

進程相關概念

1.進程相關概念 進程是代碼的一次動態執行&#xff0c;擔當分配系統資源的角色&#xff0c;進程信息是被放在一個一個數據結構中&#xff0c;是一個結構體task_struct 2.進程控制塊內容 //linux下的進程控制塊 struct task_struct {volatile long state;// 說明了該進程是否可以…

SPOJ - QTREE3Query on a tree again!——樹鏈剖分

【題目描述】 SPOJ - QTREE3Query on a tree again! 【題目分析】 題目要求是輸出從111到xxx的路徑上遇到的第一個黑色的點。我們可以用樹鏈剖分&#xff08;不了解的同學請出門左拐&#xff0c;詳見樹鏈剖分入門&#xff09; 我們用線段樹維護每個區間第一次遇到黑點的位置&a…

C++中的函數指針和函數對象總結

http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html 篇一、函數指針 函數指針&#xff1a;是指向函數的指針變量&#xff0c;在C編譯時&#xff0c;每一個函數都有一個入口地址&#xff0c;那么這個指向這個函數的函數指針便指向這個地址。 函數指針的用途是很…

開發工具

1.編輯器 &#xff08;1&#xff09;vim ????vim是從vi發展出來的一個文本編輯器。代碼補完、編譯錯誤跳轉等方便編程的功能特別豐富&#xff0c;在程序員中被廣泛使用。 &#xff08;2&#xff09;sed ????sed是一種流編輯器&#xff0c;它一次處理一行內容。處理時…

575 div3RGB Substring (hard version)——思維-

【題目描述】 The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is ‘R’, ‘G’ or ‘B’. You are also given an integer k . Your task is to change the minimum …

c++ 智能指針用法詳解

http://www.cnblogs.com/TenosDoIt/p/3456704.html 本文介紹c里面的四個智能指針: auto_ptr, shared_ptr, weak_ptr, unique_ptr 其中后三個是c11支持&#xff0c;并且第一個已經被c11棄用。 為什么要使用智能指針&#xff1a;我們知道c的內存管理是讓很多人頭疼的事&#xff0…

CodeForces - 786BLegacy——線段樹建圖+最短路

【題目描述】 CodeForces - 786BLegacy 【題目分析】 題目大概意思就是有三種操作&#xff1a; 從某個點到另一個點從某個點到另一個區間從某個區間到另一個點 然后詢問從其中一個點到其他所有點的距離——這很顯然是一個求單源最短路徑的。我們簡單的想法顯然是建一個圖&a…

自主編寫shell

1.替換原理 用fork創建子進程后執行的是和父進程相同的程序&#xff08;但有可能執行不同的代碼分支&#xff09;&#xff0c;子進程往往要調用一種exec函數以執行例外一個程序。當進程調用一種exec函數時&#xff0c;該進程的用戶空間代碼和數據完全被新程序替換&#xff0c;從…

HYSBZ - 2243染色——樹鏈剖分+線段樹建樹技巧

【題目描述】 HYSBZ - 2243染色 【題目分析】 我一直沒有看清楚題&#xff0c;以為求的是路徑上出現顏色的種類&#xff0c;然后就寫了一個區間染色的線段樹進行維護&#xff0c;過樣例的時候才發現題讀錯了&#xff0c;人家要求的是路徑上出現的顏色段&#xff0c;所以顏色的…

右值引用與轉移語義

https://www.ibm.com/developerworks/cn/aix/library/1307_lisl_c11/ 新特性的目的 右值引用 (Rvalue Referene) 是 C 新標準 (C11, 11 代表 2011 年 ) 中引入的新特性 , 它實現了轉移語義 (Move Sementics) 和精確傳遞 (Perfect Forwarding)。它的主要目的有兩個方面&#xff…

打動態庫和靜態庫

一.動態庫和靜態庫的定義 1.靜態庫 ????程序在編譯鏈接時把庫的代碼鏈接到可執行文件中。程序運行時就不再需要靜態庫 2.動態庫 ????程序在運行的時候才去鏈接動態庫的代碼&#xff0c;多個程序 共享使用代碼 3.動態鏈接 ????在執行文件之前&#xff0c;外部…

HYSBZ - 2157樹鏈剖分

【題目描述】 HYSBZ - 2157樹鏈剖分 【題目分析】 這道題給出的是邊權而不是點權&#xff0c;但是我們分析這個樹就會發現每個節點都只有一個父親&#xff0c;也就是每條邊的邊權都可以存放在兒子節點上&#xff0c;然后在遍歷路徑的時候我們在從前往后遍歷&#xff0c;但是注…

C++11中的右值引用

http://www.cnblogs.com/yanqi0124/p/4723698.html 在C98中有左值和右值的概念&#xff0c;不過這兩個概念對于很多程序員并不關心&#xff0c;因為不知道這兩個概念照樣可以寫出好程序。在C11中對右值的概念進行了增強&#xff0c;我個人理解這部分內容是C11引入的特性中最難以…

BZOJ2115XOR——線性基

【題目描述】 BZOJ2115XOR——線性基 【題目分析】 這道題看完以后很懵逼&#xff0c;人家要是走的很復雜呢&#xff1f;各種繞來繞去怎么辦&#xff1f; 首先我們應該注意到一個很明顯的道理&#xff1a;重復的路徑會和自身抵消&#xff0c;所以我們大可以隨便跑&#xff0c;…

單鏈表的相關操作

1.冒泡排序對單鏈表進行排序 void LinkListBubbleSort(LinkNode* head) {if(head NULL){ return;//空鏈表} if(head -> next NULL){ return;//只有一個結點} LinkNode* cur head;//趟數LinkNode* tail NULL;//尾指針LinkNode* tmp head;//次數for(; cur -…

socket網絡編程--epoll小結

http://www.cnblogs.com/wunaozai/p/3895860.html 以前使用的用于I/O多路復用為了方便就使用select函數&#xff0c;但select這個函數是有缺陷的。因為它所支持的并發連接數是有限的(一般小于1024)&#xff0c;因為用戶處理的數組是使用硬編碼的。這個最大值為FD_SETSIZE&#…

進程間通信(匿名管道)

1.進程通信的目的 (1) 數據傳輸: 一個進程需要將它的數據傳輸給另一個進程 ????(2) 資源共享: 多個進程之間共享同樣的資源 ????(3) 通知事件: 一個進程需要向另一個或一組進程發送消息, 通知它們發生了什么事情 2.管道 管道是一種進程之間通信的一種方式, 我們把從…

線性基入門

今天學習了神奇的線性基&#xff0c;主要是在解決異或問題時比較有用。 詳細的解釋和證明有大佬珠玉在前&#xff0c;如果感興趣可以移步 補充一下自己的理解&#xff1a; 可以聯系線性代數極大無關組進行理解&#xff0c;線性基就相當于異或的向量空間中的極大無關組&#xff…