顺序表的基本实现

来源:互联网 发布:手机足球软件 编辑:程序博客网 时间:2024/06/06 02:07

  线性表是一种最简单、最基础的数据结构,线性表中的元素都是一对一的,除了第

个元素和最后一个元素之外,其他元素都是首尾相接。通俗地讲,线性表中的元素都

是连在一条线中的,不管是直线还是曲线。如果是直线,我们称这个线性表为顺序表;

如果是曲线,我们称其为链表。

本篇文章,我来主要整理出关于顺序表的内容。在通讯录的多版本实现这篇博客中,我们

 知道,根据顺序表的容量大小是否固定可将顺序表分为静态顺序表和动态顺序表。

对于有好的变成习惯的程序员来说,使用assert是比较多的,可是,并不是所有的指针都

可以用assert来断言,下边我来具体说一下,希望对那些不太会用断言的人有一点帮助~~

assert的正确使用:

assert是一个宏,原型定义在头文件assert.h中,在vs2015编译器下给出的定义如下图:


在执行assert宏时,它会先计算出表达式expression的值,如果值为假,那么它想stderr

打印出一条出错信息,通过调用abort来终止程序执行。

细心的你有可能会发现expression的前边怎么会有两个‘!’呢。是不是多余的呢??其实

并不是。如果expression不是0,!!expression = =1,否则,!!expression = =0,这下你就

知道了吧。为什么要这样做呢??这里用到了逻辑运算符'||'的短路功能。如果expression是真,后边的不执行,如果是假,打印出出错信息~~这个简直是妙~

看着assert好像是使用越多越好,其实并非这样。频繁的调用assert会极大地影响程序的

性能,增加额外开销。并且它只能用在debug版本,release版本并不可。

在程序结束后,可以通过在包含#include<assert.h>的语句之前插入#define NDEBUG来

禁用assert调用,比如(只给出头文件):

#include<stdio.h>

#define NDEBUG

#include<assert.h>

assert的用法总结:

(1)在函数开始处检验传入参入的合法性。这个我们平时使用的比较多。可是,如果

某个条件在为假时,对于程序 而言仍为合法值,此时就不要assert断言了。(关于这

个,在链表的博客中再述)

(2)每个assert最好只检验一个条件。否则,如果断言失败,我们无法直观的判断哪个

条件失败。

(3)assert里的参数不能使用改变环境的语句。比如:

assert(i++<100);这条语句是不正确的,这是因为如果在这条语句执行之前i=100,i++

<100这个条件为假,程序直接报错,i++就不会执行。

(4)有的地方,assert不能代替条件过滤。这里我举不出例子,有见解的朋友,可以在

博客下边的评论栏指出。

由于通讯录版本已经对静态顺序表做了解释,这里只给出代码和编码过程中的注意事项

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //SeqList.c  
  2. #include"SeqList.h"  
  3. void menu()  
  4. {  
  5.     printf("*******1.初始化顺序表*******\n");  
  6.     printf("*******2.显示出顺序表*******\n");  
  7.     printf("*******3.顺序表尾插*********\n");  
  8.     printf("*******4.顺序表尾删*********\n");  
  9.     printf("*******5.顺序表头插*********\n");  
  10.     printf("*******6.顺序表头删*********\n");  
  11.     printf("*******7.插入元素***********\n");  
  12.     printf("*******8.删除给定元素*******\n");  
  13.     printf("*******9.删除给定元素的全部**\n");  
  14.     printf("*******10.顺序表排序*********\n");  
  15.     printf("*******11.折半查找元素*******\n");  
  16.     printf("*******0.退出****************\n");  
  17. }  
  18. //初始化  
  19. void InitSeq(pSeqList  pSeq)  
  20. {  
  21.     assert(pSeq);  
  22.     pSeq->sz = 0;  
  23.     memset(pSeq,0,sizeof(TypeName) * MAX);  
  24.     printf("初始化成功!\n");  
  25. }  
  26. //打印  
  27. void PrintSeq(pSeqList  pSeq)  
  28. {  
  29.     assert(pSeq);  
  30.     int i = 0;  
  31.     printf("顺序表中的元素有:\n");  
  32.     for (i = 0;i < pSeq->sz;i++)  
  33.     {  
  34.         printf("%d ",pSeq->Data[i]);  
  35.     }  
  36.     printf("\n");  
  37. }  
  38. //尾插  
  39. void PushBack(pSeqList  pSeq, TypeName x)  
  40. {  
  41.     assert(pSeq);  
  42.     if (pSeq->sz == MAX)  
  43.     {  
  44.         printf("顺序表已满,不可插入");  
  45.         exit(EXIT_FAILURE);  
  46.     }  
  47.     pSeq->Data[pSeq->sz] = x;  
  48.     pSeq->sz++;  
  49.     printf("尾插成功!\n");  
  50. }  
  51. //尾删  
  52. void PopBack(pSeqList  pSeq)  
  53. {  
  54.     assert(pSeq);  
  55.     if (pSeq->sz == 0)  
  56.     {  
  57.         printf("顺序表已空,不可删除");  
  58.         exit(EXIT_FAILURE);  
  59.     }  
  60.     pSeq->sz--;  
  61.     printf("尾删成功!\n");  
  62. }  
  63. //插入  
  64. void Insert(pSeqList  pSeq, int pos, TypeName x)  
  65. {  
  66.     assert(pSeq);  
  67.     int i = 0;  
  68.     if (pSeq->sz == MAX || pos == MAX)  
  69.     {  
  70.         printf("顺序表已满或者插入位置不合理\n");  
  71.         exit(EXIT_FAILURE);  
  72.     }  
  73.     for (i = pSeq->sz - 1;i >= pos; i--)  
  74.     {  
  75.         pSeq->Data[i + 1] = pSeq->Data[i];  
  76.     }  
  77.     pSeq->Data[pos] = x;  
  78.     pSeq->sz++;  
  79.     printf("插入成功!\n");  
  80. }  
  81. //删除给定元素  
  82. void Remove(pSeqList  pSeq, TypeName x)  
  83. {  
  84.     assert(pSeq);  
  85.     if (pSeq->sz == 0)  
  86.     {  
  87.         printf("顺序表已空,不可删除");  
  88.         exit(EXIT_FAILURE);  
  89.     }  
  90.     int i = 0;  
  91.     int j = 0;  
  92.     for (i = 0;i < pSeq->sz;i++)  
  93.     {  
  94.         if (pSeq->Data[i] == x)  
  95.             break;  
  96.     }  
  97.     if (i == pSeq->sz)  
  98.     {  
  99.         printf("要删除的元素不存在\n");  
  100.         exit(EXIT_FAILURE);  
  101.     }  
  102.     for (j = i + 1;j < pSeq->sz;j++)  
  103.     {  
  104.         pSeq->Data[j-1] = pSeq->Data[j];  
  105.     }  
  106.     pSeq->sz--;  
  107.     printf("删除成功!\n");  
  108. }  
  109. //删除给定元素的所有  
  110. void RemoveAll(pSeqList  pSeq, TypeName x)  
  111. {  
  112.     assert(pSeq);  
  113.     int count = 0;  
  114.     if (pSeq->sz == 0)  
  115.     {  
  116.         printf("顺序表已空,不可删除");  
  117.         exit(EXIT_FAILURE);  
  118.     }  
  119.     int i = 0;  
  120.     int j = pSeq->sz-1;  
  121.     while(i < pSeq->sz)  
  122.     {  
  123.         if (pSeq->Data[i] == x )  
  124.         {  
  125.             while (pSeq->Data[j] == x)  
  126.             {  
  127.                 j--;  
  128.             }  
  129.             pSeq->Data[i] = pSeq->Data[j];  
  130.             count++;  
  131.               
  132.         }  
  133.         i++;  
  134.     }  
  135.     pSeq->sz -= count;  
  136.     printf("删除成功!\n");  
  137. }  
  138. //头插  
  139. void PushFront(pSeqList  pSeq, TypeName x)  
  140. {  
  141.     assert(pSeq);  
  142.     int i = 0;  
  143.     if (pSeq->sz == MAX)  
  144.     {  
  145.         printf("顺序表已满,不可插入");  
  146.         exit(EXIT_FAILURE);  
  147.     }  
  148.     for (i = pSeq->sz - 1;i >= 0;i--)  
  149.     {  
  150.         pSeq->Data[i + 1] = pSeq->Data[i];  
  151.     }  
  152.     pSeq->Data[0] = x;  
  153.     pSeq->sz++;  
  154.     printf("头插成功!\n");  
  155. }  
  156. //头删  
  157. void PopFront(pSeqList  pSeq)  
  158. {  
  159.     assert(pSeq);  
  160.     int i = 0;  
  161.     if (pSeq->sz == 0)  
  162.     {  
  163.         printf("顺序表已空,不可删除");  
  164.         exit(EXIT_FAILURE);  
  165.     }  
  166.     for (i = 1;i < pSeq->sz ;i++)  
  167.     {  
  168.         pSeq->Data[i - 1] = pSeq->Data[i];  
  169.     }  
  170.     pSeq->sz--;  
  171.     printf("头删成功!\n");  
  172. }  
  173. //排序  
  174. void Sort(pSeqList  pSeq)  
  175. {  
  176.     assert(pSeq);  
  177.     int i = 0;  
  178.     int j = 0;  
  179.     int flag = 0;  
  180.     TypeName tmp= 0;  
  181.     if (pSeq->sz == 0)  
  182.     {  
  183.         printf("顺序表已空,不可排序");  
  184.         exit(EXIT_FAILURE);  
  185.     }  
  186.     for (i = 0;i < pSeq->sz;i++)  
  187.     {  
  188.         flag = 1;  
  189.         for (j = 0;j < pSeq->sz - i - 1;j++)  
  190.         {  
  191.             if (pSeq->Data[j] > pSeq->Data[j + 1])  
  192.             {  
  193.                 tmp = pSeq->Data[j];  
  194.                 pSeq->Data[j] = pSeq->Data[j + 1];  
  195.                 pSeq->Data[j + 1] = tmp;  
  196.                 flag = 0;  
  197.             }  
  198.         }  
  199.         if (flag == 1)  
  200.             break;  
  201.     }  
  202.     //printf("排序成功!\n");  
  203. }  
  204. //折半查找  
  205. void BinarySearch(pSeqList  pSeq, TypeName x)  
  206. {  
  207.     assert(pSeq);  
  208.     int left = 0;  
  209.     int right = pSeq->sz - 1;;  
  210.     int middle = 0;  
  211.     if (pSeq->sz == 0)  
  212.     {  
  213.         printf("顺序表已空,不可查找");  
  214.         exit(EXIT_FAILURE);  
  215.     }  
  216.     Sort(pSeq);  
  217.     while (left <= right)  
  218.     {  
  219.         middle = (left + right)/2;  
  220.         if (pSeq->Data[middle] == x)  
  221.         {  
  222.             printf("查找成功!\n");  
  223.             return;  
  224.         }  
  225.         else if (pSeq->Data[middle] < x)  
  226.         {  
  227.             left = middle + 1;  
  228.         }  
  229.         else  
  230.         {  
  231.             right = middle - 1;  
  232.         }  
  233.     }  
  234.     printf("要查找的元素不存在!\n");  
  235. }  
  236. //SeqList.h  
  237. #ifndef __SEQLIST_H__  
  238. #define  __SEQLIST_H__  
  239. #define _CRT_SECURE_NO_WARNINGS 1  
  240. #include<stdio.h>  
  241. #include<assert.h>  
  242. #include<memory.h>  
  243. #include<windows.h>  
  244. #define MAX 100  
  245. typedef int TypeName;  
  246. typedef struct SeqList  
  247. {  
  248.     TypeName Data[MAX];  
  249.     int sz;  
  250. }SeqList,*pSeqList;  
  251. enum op  
  252. {  
  253.     EXIT,  
  254.     INIT,  
  255.     PRINT,  
  256.     PUSHBACK,  
  257.     POPBACK,  
  258.     PUSHFRONT,  
  259.     POPFRONT,  
  260.     INSERT,  
  261.     REMOVE,  
  262.     REMOVEALL,  
  263.     SORT,  
  264.     BINARYSEARCH  
  265. };  
  266.   
  267. void InitSeq(pSeqList  pSeq);  
  268. void PrintSeq(pSeqList  pSeq);  
  269. void PushBack(pSeqList  pSeq, TypeName x);  
  270. void PopBack(pSeqList  pSeq);  
  271. void PushFront(pSeqList  pSeq, TypeName x);  
  272. void PopFront(pSeqList  pSeq);  
  273. void Insert(pSeqList  pSeq,TypeName pos,TypeName x);//在给定位置插入给定元素  
  274. void Remove(pSeqList  pSeq,TypeName x);//删除表中的特定元素  
  275. void RemoveAll(pSeqList  pSeq,TypeName x);//删除表中所有值为x的元素  
  276. void Sort(pSeqList  pSeq);  
  277. void BinarySearch(pSeqList  pSeq,TypeName x);  
  278.   
  279.   
  280. #endif //__SEQLIST_H__  
  281. //test.c  
  282. #include"SeqList.h"  
  283. void test()  
  284. {  
  285.     SeqList seq;  
  286.     //pSeqList pseq = &seq;  
  287.     int pos = 0;  
  288.     int input = 1;  
  289.     TypeName x = 0;  
  290.     while (input)  
  291.     {  
  292.         menu();  
  293.         printf("请选择:>");  
  294.         scanf("%d", &input);  
  295.         switch (input)  
  296.         {  
  297.         case EXIT:  
  298.             break;  
  299.         case INIT:  
  300.             InitSeq(&seq);  
  301.             break;  
  302.         case PRINT:  
  303.             PrintSeq(&seq);  
  304.             break;  
  305.         case INSERT:  
  306.             printf("请输入插入元素的位置:>");  
  307.             scanf("%d",&pos);  
  308.             printf("请输入插入元素的值:>");  
  309.             scanf("%d", &x);  
  310.             Insert(&seq, pos, x);  
  311.             break;  
  312.         case PUSHBACK:  
  313.             printf("请输入插入元素的值:>");  
  314.             scanf("%d", &x);  
  315.             PushBack(&seq,x);  
  316.             break;  
  317.         case POPBACK:  
  318.             PopBack(&seq);  
  319.             break;  
  320.         case PUSHFRONT:  
  321.             printf("请输入插入元素的值:>");  
  322.             scanf("%d", &x);  
  323.             PushFront(&seq,x);  
  324.             break;  
  325.         case POPFRONT:  
  326.             PopFront(&seq);  
  327.             break;  
  328.         case SORT:  
  329.             Sort(&seq);  
  330.             break;  
  331.         case BINARYSEARCH:  
  332.             printf("请输入查找元素的值:>");  
  333.             scanf("%d", &x);  
  334.             BinarySearch(&seq, x);  
  335.             break;  
  336.         case REMOVE:  
  337.             printf("请输入要删除的元素:>");  
  338.             scanf("%d", &x);  
  339.             Remove(&seq, x);  
  340.             break;  
  341.         case REMOVEALL:  
  342.             printf("请输入要删除的元素:>");  
  343.             scanf("%d", &x);  
  344.             RemoveAll(&seq, x);  
  345.             break;  
  346.         }  
  347.     }  
  348. }  
  349. int main()  
  350. {  
  351.     test();  
  352.     system("pause");  
  353.     return 0;  
  354. }  

静态顺序表编码的注意:

(1)删除之前必须对顺序表是否为空进行检查,如果为空不可删除。

(2)插入之前必须对顺序表是否满进行检查,如果已满,不可插入。

(3)折半查找之前必须对元素排序。(这个属于两个版本共同需要注意的)

下边给出动态顺序表的实现代码:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //SeqList.h  
  2. #ifndef __SEQLIST_H__  
  3. #define __SEQLIST_H__  
  4. #define _CRT_SECURE_NO_WARNINGS  
  5. #include<stdio.h>  
  6. #include<stdlib.h>  
  7. #include<assert.h>  
  8. enum OP  
  9. {  
  10.     EXIT,  
  11.     PRINT,  
  12.     PUSHBACK,  
  13.     POPBACK,  
  14.     PUSHFRONT,  
  15.     POPFRONT,  
  16.     REMOVE,  
  17.     REMOVEALL,  
  18.     BOTTLESORT,  
  19.     INSERTSORT,  
  20.     SELECTSORT,  
  21.     BINARYSEARCH,  
  22.     ERASE  
  23. };  
  24. #define DEFAULT_SIZE 3  
  25. typedef int DataType;  
  26. typedef struct Seqlist  
  27. {  
  28.     DataType *data;  
  29.     int size;  
  30.     int capacity;  
  31. }SeqList,*pSeqList;  
  32. void InitSeqList(pSeqList pSeq);  
  33. void DestroySeqList(pSeqList pSeq);  
  34. void PushBack(pSeqList pSeq,DataType x);  
  35. void PopBack(pSeqList pSeq);  
  36. void PushFront(pSeqList pSeq,  DataType x);  
  37. void PopFront(pSeqList pSeq);  
  38. void Remove(pSeqList pSeq, DataType x);  
  39. void RemoveAll(pSeqList pSeq, DataType x);  
  40. void BubbleSort(pSeqList pSeq);  
  41. void InsertSort(pSeqList pSeq);  
  42. void SelectSort(pSeqList pSeq);  
  43. void BinarySearch(pSeqList pSeq);  
  44. void Erase(pSeqList pSeq,int pos);  
  45. void PrintSeqList(pSeqList pSeq);  
  46. #endif// __SEQLIST_H__  
  47. //SeqList.c  
  48. #include"SeqList.h"  
  49.   
  50. void check_capacity(pSeqList pSeq)  
  51. {  
  52.     assert(pSeq);  
  53.     if (pSeq->capacity == pSeq->size)  
  54.     {  
  55.         DataType *tmp = (DataType *)realloc(pSeq->data,(pSeq->capacity +DEFAULT_SIZE)*sizeof(DataType));  
  56.         if (NULL == tmp)  
  57.         {  
  58.             printf("out of memory.");  
  59.             exit(EXIT_FAILURE);  
  60.         }  
  61.         else  
  62.         {  
  63.             pSeq->data = tmp;  
  64.             pSeq->capacity += DEFAULT_SIZE;  
  65.         }  
  66.     }  
  67. }  
  68. void InitSeqList(pSeqList pSeq)  
  69. {  
  70.     assert(pSeq);  
  71.     pSeq->data = (DataType*)malloc(2*sizeof(DataType));  
  72.     if (NULL == pSeq->data)  
  73.     {  
  74.         printf("out of memory\n");  
  75.         exit(EXIT_FAILURE);  
  76.     }  
  77.     pSeq->size = 0;  
  78.     pSeq->capacity = 2;  
  79. }  
  80. void DestroySeqList(pSeqList pSeq)  
  81. {  
  82.     assert(pSeq);  
  83.     free(pSeq->data);  
  84.     pSeq->size = 0;  
  85.     pSeq->capacity = 0;  
  86.     pSeq->data = NULL;  
  87. }  
  88. void PushBack(pSeqList pSeq, DataType x)  
  89. {  
  90.     assert(pSeq);  
  91.     check_capacity(pSeq);  
  92.     pSeq->data[pSeq->size] = x;  
  93.     pSeq->size++;  
  94.     printf("尾插成功!\n");  
  95. }  
  96. void PopBack(pSeqList pSeq)  
  97. {  
  98.     assert(pSeq);  
  99.     if (0 == pSeq->size)  
  100.     {  
  101.         printf("seqlist is empty.\n");  
  102.         exit(EXIT_FAILURE);  
  103.     }  
  104.     pSeq->size--;  
  105.     printf("尾删成功!\n");  
  106. }  
  107. void PushFront(pSeqList pSeq, DataType x)  
  108. {  
  109.     assert(pSeq);  
  110.     check_capacity(pSeq);  
  111.     int i = 0;  
  112.     for (i = pSeq->size - 1;i >= 0;i--)  
  113.     {  
  114.         pSeq->data[i + 1] = pSeq->data[i];  
  115.     }  
  116.     pSeq->data[0] = x;  
  117.     pSeq->size++;  
  118.     printf("头插成功!\n");  
  119. }  
  120. void PopFront(pSeqList pSeq)  
  121. {  
  122.     assert(pSeq);  
  123.     if (0 == pSeq->size)  
  124.     {  
  125.         printf("seqlist is empty.\n");  
  126.         exit(EXIT_FAILURE);  
  127.     }  
  128.     int i = 0;  
  129.     for (i = 1;i < pSeq->size - 1;i++)  
  130.     {  
  131.         pSeq->data[i - 1] = pSeq->data[i];  
  132.     }  
  133.     pSeq->size--;  
  134.     printf("头删成功!\n");  
  135. }  
  136. void Remove(pSeqList pSeq, DataType x)  
  137. {  
  138.     assert(pSeq);  
  139.     int i = 0;  
  140.     int j = 0;  
  141.     if (0 == pSeq->size)  
  142.     {  
  143.         printf("seqlist is empty.\n");  
  144.         exit(EXIT_FAILURE);  
  145.     }  
  146.     for (i = 0;i < pSeq->size;i++)  
  147.     {  
  148.         if (x == pSeq->data[i])  
  149.             break;  
  150.     }  
  151.     for (j = i + 1;j < pSeq->size;j++)  
  152.     {  
  153.         pSeq->data[j - 1] = pSeq->data[j];  
  154.     }  
  155.     pSeq->size--;  
  156.     printf("删除指定元素成功!\n");  
  157. }  
  158. void RemoveAll(pSeqList pSeq, DataType x)  
  159. {  
  160.     assert(pSeq);  
  161.     int i = 0;  
  162.     int count = 0;  
  163.     int j = 0;  
  164.     if (0 == pSeq->size)  
  165.     {  
  166.         printf("seqlist is empty.\n");  
  167.         exit(EXIT_FAILURE);  
  168.     }  
  169.     /*for (i = 0;i < pSeq->size;i++) 
  170.     { 
  171.         if (pSeq->data[i] == x) 
  172.         { 
  173.             count++; 
  174.         } 
  175.     }*/  
  176.     i = 0;  
  177.     j = pSeq->size - 1;  
  178.     /*while (j >= pSeq->size - count && i < pSeq->size - count) 
  179.     { 
  180.         if (pSeq->data[j] != x && pSeq->data[i] == x) 
  181.         { 
  182.             pSeq->data[i] = pSeq->data[j]; 
  183.         } 
  184.         j--; 
  185.         i++; 
  186.     }*/  
  187.     while (i < pSeq->size)  
  188.     {  
  189.         if (pSeq->data[i] == x)  
  190.         {  
  191.             while (pSeq->data[j] == x)  
  192.             {  
  193.                 j--;  
  194.             }  
  195.             pSeq->data[i] = pSeq->data[j];  
  196.             count++;  
  197.         }  
  198.         i++;  
  199.     }  
  200.     pSeq->size -= count;  
  201.     printf("delete success!\n");  
  202. }  
  203. void BubbleSort(pSeqList pSeq)  
  204. {  
  205.     assert(pSeq);  
  206.     if (0 == pSeq->size)  
  207.     {  
  208.         printf("seqlist is empty.\n");  
  209.         exit(EXIT_FAILURE);  
  210.     }  
  211.     int i = 0;  
  212.     int j = 0;  
  213.     int k = pSeq->size - 1;  
  214.     int tmp = 0;  
  215.     int m = 0;  
  216.     int flag = 0;  
  217.     for (i = 0;i < pSeq->size - 1;i++)  
  218.     {  
  219.         flag = 1;  
  220.         for (j = 0;j < k;j++)  
  221.         {  
  222.             if (pSeq->data[j]>pSeq->data[j + 1])  
  223.             {  
  224.                 tmp = pSeq->data[j];  
  225.                 pSeq->data[j] = pSeq->data[j + 1];  
  226.                 pSeq->data[j + 1] = tmp;  
  227.                 m = j;//m is used for recording the position of last swap  
  228.                 flag = 0;  
  229.             }  
  230.         }  
  231.         k = m;  
  232.         if (flag == 1)  
  233.         {  
  234.             break;  
  235.         }  
  236.     }  
  237.     printf("successful bottlesort\n");  
  238. }  
  239. void InsertSort(pSeqList pSeq)  
  240. {  
  241.     assert(pSeq);  
  242.     if (0 == pSeq->size)  
  243.     {  
  244.         printf("seqlist is empty.\n");  
  245.         exit(EXIT_FAILURE);  
  246.     }  
  247.     int i = 0;  
  248.     int j = 0;  
  249.     int tmp = 0;  
  250.     for (i = 1;i < pSeq->size;i++)  
  251.     {  
  252.         tmp = pSeq->data[i];  
  253.         for (j = i - 1;j >= 0 && pSeq->data[j] > tmp;j--)  
  254.         {  
  255.             pSeq->data[j + 1] = pSeq->data[j];  
  256.         }  
  257.         pSeq->data[j + 1] = tmp;  
  258.     }  
  259.     printf("successful insertsort\n");  
  260. }  
  261. void SelectSort(pSeqList pSeq)  
  262. {  
  263.     assert(pSeq);  
  264.     if (0 == pSeq->size)  
  265.     {  
  266.         printf("seqlist is empty.\n");  
  267.         exit(EXIT_FAILURE);  
  268.     }  
  269.     int i = 0;  
  270.     int j = 0;  
  271.     int min = 0;//the index of min number  
  272.     int tmp = 0;  
  273.     for (i = 0;i < pSeq->size;i++)  
  274.     {  
  275.         min = i;  
  276.         for (j = i;j < pSeq->size;j++)  
  277.         {  
  278.             if (pSeq->data[j] < pSeq->data[min])  
  279.             {  
  280.                 min = j;  
  281.             }  
  282.         }  
  283.         if (min != i)  
  284.         {  
  285.             tmp = pSeq->data[i];  
  286.             pSeq->data[i] = pSeq->data[min];  
  287.             pSeq->data[min] = tmp;  
  288.         }  
  289.     }  
  290.     printf("successful selectsort\n");  
  291. }  
  292. void BinarySearch(pSeqList pSeq, DataType x)  
  293. {  
  294.     assert(pSeq);  
  295.     if (0 == pSeq->size)  
  296.     {  
  297.         printf("seqlist is empty.\n");  
  298.         exit(EXIT_FAILURE);  
  299.     }  
  300.     SelectSort(pSeq);//before searching,we must sort seqlist  
  301.     int left = 0;  
  302.     int right = pSeq->size - 1;  
  303.     int middle = 0;  
  304.     while (left < right)  
  305.     {  
  306.         middle = (left + right) / 2;  
  307.         if (x == pSeq->data[middle])  
  308.         {  
  309.             printf("have found\n");  
  310.             return;  
  311.         }  
  312.         else if (x < pSeq->data[middle])  
  313.         {  
  314.             right = middle - 1;  
  315.         }  
  316.         else  
  317.         {  
  318.             left = middle + 1;  
  319.         }  
  320.     }  
  321.     printf("not found\n");  
  322. }  
  323. void Erase(pSeqList pSeq, int pos)  
  324. {  
  325.     assert(pSeq);  
  326.     if (0 == pSeq->size)  
  327.     {  
  328.         printf("seqlist is empty.\n");  
  329.         exit(EXIT_FAILURE);  
  330.     }  
  331.     pSeq->data[pos] = pSeq->data[pSeq->size - 1];  
  332.     pSeq->size--;  
  333.     printf("successful delete designated position\n");  
  334. }  
  335. void PrintSeqList(pSeqList pSeq)  
  336. {  
  337.     assert(pSeq);  
  338.     int i = 0;  
  339.     printf("动态顺序表中的元素为:\n");  
  340.     for (i = 0;i < pSeq->size;i++)  
  341.     {  
  342.         printf("%d ",pSeq->data[i]);  
  343.     }  
  344.     printf("\n");  
  345. }  
  346. //test.c  
  347. #include"SeqList.h"  
  348. void menu()  
  349. {  
  350.     printf("-------动态顺序表管理系统-------\n");  
  351.     printf("-------0.退出-------------------\n");  
  352.     printf("-------1.显示-------------------\n");  
  353.     printf("-------2.尾插-------------------\n");  
  354.     printf("-------3.尾删-------------------\n");  
  355.     printf("-------4.头插-------------------\n");  
  356.     printf("-------5.头删-------------------\n");  
  357.     printf("-------6.删除指定---------------\n");  
  358.     printf("-------7.删除指定全部-----------\n");  
  359.     printf("-------8.冒泡排序---------------\n");  
  360.     printf("-------9.插入排序---------------\n");  
  361.     printf("-------10.选择排序--------------\n");  
  362.     printf("-------11.折半查找---------------\n");  
  363.     printf("-------12.删除指定位置-----------\n");  
  364. }  
  365. void test()  
  366. {  
  367.     SeqList seq;  
  368.     pSeqList pSeq = &seq;  
  369.     DataType x = 0;  
  370.     int input = 1;  
  371.     int pos = 0;  
  372.     InitSeqList(pSeq);  
  373.     while (input)  
  374.     {  
  375.         menu();  
  376.         printf("请选择:>");  
  377.         scanf("%d",&input);  
  378.         switch (input)  
  379.         {  
  380.         case EXIT:  
  381.             DestroySeqList(pSeq);  
  382.             break;  
  383.         case PRINT:  
  384.             PrintSeqList(pSeq);  
  385.             break;  
  386.         case PUSHBACK:  
  387.             printf("请输入要插入的元素:\n");  
  388.             scanf("%d",&x);  
  389.             PushBack(pSeq,x);  
  390.             break;  
  391.         case POPBACK:  
  392.             PopBack(pSeq);  
  393.             break;  
  394.         case PUSHFRONT:  
  395.             printf("请输入要插入的元素:\n");  
  396.             scanf("%d", &x);  
  397.             PushFront(pSeq,x);  
  398.             break;  
  399.         case POPFRONT:  
  400.             PopFront(pSeq);  
  401.             break;  
  402.         case BOTTLESORT:  
  403.             BubbleSort(pSeq);  
  404.             break;  
  405.         case SELECTSORT:  
  406.             SelectSort(pSeq);  
  407.             break;  
  408.         case INSERTSORT:  
  409.             InsertSort(pSeq);  
  410.             break;  
  411.         case ERASE:  
  412.             printf("请输入要删除元素的位置:\n");  
  413.             scanf("%d", &pos);  
  414.             Erase(pSeq, pos);  
  415.             break;  
  416.         case BINARYSEARCH:  
  417.             printf("请输入要查找的元素:\n");  
  418.             scanf("%d", &x);  
  419.             BinarySearch(pSeq, x);  
  420.             break;  
  421.         case REMOVE:  
  422.             printf("请输入要删除的元素:\n");  
  423.             scanf("%d", &x);  
  424.             Remove(pSeq, x);  
  425.             break;  
  426.         case REMOVEALL:  
  427.             printf("请输入要删除的元素:\n");  
  428.             scanf("%d", &x);  
  429.             RemoveAll(pSeq, x);  
  430.             break;  
  431.         }  
  432.     }  
  433. }  
  434. int main()  
  435. {  
  436.     test();  
  437.     system("pause");  
  438.     return 0;  
  439. }  

动态顺序表的注意事项:

(1)插入之前对顺序表的大小与顺序表容量之间的关系进行判断,如果相等,扩容。

(2)删除、排序等操作之前需要对顺序表是否为空进行判断。

在最近经常碰到以下错误:(如图)


读取位置发生冲突,这是因为没有对某些数据进行初始化导致错误,以上错误就是未调

用初始化函数导致。

有时,程序运行着运行着,就会突然崩,弹出一个窗口,某处触发了一个断点,这是因

为这个地方的函数参数搞错导致吧。

0 0
原创粉丝点击