静态顺序表的实现
?????實現對順序表的初始化,頭插,頭刪,尾插,尾刪, 任意下標的刪除, 任意下標處的的元素刪除,任意下標處的元素插入,任意元素的下標返回,任意下標處的元素返回, 刪除某個元素, 刪除線性表中的所有元素,對線性表判空處理, 以及檢查線性表有幾個元素
?????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.運行結果如下
????????????
????????????
總結
- 上一篇: LInux下du, df, top, f
- 下一篇: 进程相关概念