静态顺序表的实现

     实现对顺序表的初始化,头插,头删,尾插,尾删, 任意下标的删除, 任意下标处的的元素删除,任意下标处的元素插入,任意元素的下标返回,任意下标处的元素返回, 删除某个元素, 删除线性表中的所有元素,对线性表判空处理, 以及检查线性表有几个元素
     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.mzph.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;它一次处理一行内容。处理时…

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;所以颜色的…

打动态库和静态库

一.动态库和静态库的定义 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.管道 管道是一种进程之间通信的一种方式, 我们把从…

单例模式及C++实现代码

http://www.cnblogs.com/cxjchen/p/3148582.html 单例模式 单例模式&#xff0c;可以说设计模式中最常应用的一种模式了&#xff0c;据说也是面试官最喜欢的题目。但是如果没有学过设计模式的人&#xff0c;可能不会想到要去应用单例模式&#xff0c;面对单例模式适用的情况&am…

命名管道

1.命名管道的创建 (1) 通过命令创建 mkfifo filename (2)在程序中创建 int mkfifo(const char* filename, mode_t mode); 2. 命名管道和匿名管道的区别 (1)匿名管道由pipe函数创建并且打开     (2)命名管道有mkfifo函数创建由open函数打开     (3) fifo 之间的两…

HYSBZ - 1101——莫比乌斯反演

【题目描述】 HYSBZ - 1101 【题目分析】 昨天测试出了一道差不多的题目&#xff0c;我只能想到暴力&#xff0c;各种优化&#xff0c;最后都是运行了好久TLE&#xff0c;最后才知道要用到莫比乌斯反演&#xff0c;就想着今天研究一下&#xff0c;得出的结论就是&#xff0c;我…