当前位置:   article > 正文

【C语言数据结构】顺序表详解(动态版本)_详解动态顺序表

详解动态顺序表
作者:热爱编程的小y
专栏:C语言数据结构
格言:能打败你的只能是明天的你

一、概念

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

顺序表是线性表的一种,他的孪生兄弟是链表。

二、结构

顺序表一般可以分为:

  1. 静态顺序表

使用定长数组存储元素。

  1. 动态顺序表

使用动态开辟的数组存储。

三、接口实现

静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态地分配空间大小,所以下面我们实现动态顺序表。

(一)准备工作

1.创立文件

我们需要创立三个文件,分别是SeqList.cSeqList.hTest.c

SeqList.h 内包含引用的头文件,函数的声明,结构体的声明,宏的声明与定义。

SeqList.c 内包含函数的主体,增删查改的具体过程在这里实现。

Test.c 负责对各项功能的测试与具体运用。

2.函数与结构体的定义

我们需要定义一个结构体类型用于数据的存放,结构体内需要包含三个参数,分别是一个指针,一个表示当前已存放的数据个数的整形,一个表示当前最多存放的数据个数的整形。因为我们不仅需要把数据存放进去,还要知道它的存放位置便于后续的更改,第三个参数则是确保开辟的空间大小在一个合理的范围内。具体如下。

  1. typedef struct SeqList
  2. {
  3. SLDateType* a;//存放数据的地址
  4. int size;//当前已存放的数据个数
  5. int capacity;//当前最多存放的数据个数
  6. }SeqList;

因为我们并不能确定存放数据的类型是什么,所以我们需要重新命名一个类型,就像这样。

  1. typedef int SLDateType;//想修改顺序表内数据的类型可以直接在这修改

之后不论是想存放什么类型的数据,只要在这里修改一遍就可以了。

我们还需要定义各种函数来分别实现各种功能,功能就不一一介绍了,具体如下。

  1. // 对数据的管理:增删查改
  2. //初始化
  3. void SeqListInit(SeqList* ps);
  4. //销毁
  5. void SeqListDestroy(SeqList* ps);
  6. //显示(打印)
  7. void SeqListPrint(SeqList* ps);
  8. //尾插
  9. void SeqListPushBack(SeqList* ps, SLDateType x);
  10. //头插
  11. void SeqListPushFront(SeqList* ps, SLDateType x);
  12. //头删
  13. void SeqListPopFront(SeqList* ps);
  14. //尾删
  15. void SeqListPopBack(SeqList* ps);
  16. // 顺序表查找
  17. void SeqListFind(SeqList* ps, SLDateType x);
  18. // 顺序表在pos位置插入x
  19. void SeqListInsert(SeqList* ps, int pos, SLDateType x);
  20. // 顺序表删除pos位置的值
  21. void SeqListErase(SeqList* ps, int pos);

(二)功能实现

  1. 初始化与销毁

我们先给定一个内存大小初始值,按照这个初始值通过malloc或者calloc函数给参数ps->a开辟空间,

#define INIT_CAPACITY 4

我们将初始值定义成一个宏,方便随时修改。

空间有开辟失败的可能性(虽然可能性很小很小就是了),因此还要对其进行排除。

销毁顺序表的时候我们用free函数来释放掉里面的空间,并顺带把原先指向存放数据地址的指针赋为空,避免其成为野指针。

  1. //初始化
  2. void SeqListInit(SeqList* ps)
  3. {
  4. ps->a = (SLDateType*)malloc(INIT_CAPACITY * sizeof(SLDateType));
  5. if (NULL == ps->a)
  6. {
  7. perror("SeqListInit::calloc");
  8. return;
  9. }
  10. ps->size = 0;
  11. ps->capacity = INIT_CAPACITY;
  12. return;
  13. }
  14. //销毁
  15. void SeqListDestroy(SeqList* ps)
  16. {
  17. free(ps->a);
  18. ps->a = NULL;
  19. ps->capacity = ps->size = 0;
  20. return;
  21. }
  1. 头插与尾插

先说明一下,头插就是在顺序表的开头插入数据,尾插则是在顺序的末尾插入数据。

我们进行头插操作时,因为不能直接硬插,而是要先腾出空间才能插入,而且不能打乱后面数据的顺序,所以我们需要把所有数据由末向前依次往后挪一个位置。结束之后要记得让size+1。

相比头插,尾插就方便多了,我们可以直接插入数据,因为size的值不仅是已有数据的个数也是我们插入的下一个数据的地址。

不管是头插还是尾插,我们都需要先进行一个判断是否扩容的操作,判断条件很简单,只需要判断size与capacity就是已有数据的个数与最大空间是否相等了即可。与开辟空间一样,扩容也存在失败的可能性,也需要进行排除。

  1. //尾插
  2. void SeqListPushBack(SeqList* ps, SLDateType x)
  3. {
  4. /*增容*/
  5. if (ps->size == ps->capacity)
  6. {
  7. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType)*ps->capacity * 2);
  8. /*增容失败*/
  9. if (tmp == NULL)
  10. {
  11. perror("SeqListPushFront::realloc");
  12. return;
  13. }
  14. /*增容成功*/
  15. ps->a = tmp;
  16. ps->capacity *= 2;
  17. }
  18. ps->a[ps->size++] = x;
  19. return;
  20. }
  21. //头插
  22. void SeqListPushFront(SeqList* ps, SLDateType x)
  23. {
  24. /*扩容*/
  25. if (ps->size == ps->capacity || ps->size + 1 == ps->capacity)
  26. {
  27. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * ps->capacity * 2);
  28. /*增容失败*/
  29. if (tmp == NULL)
  30. {
  31. perror("SeqListPushFront::realloc");
  32. return;
  33. }
  34. /*增容成功*/
  35. ps->a = tmp;
  36. ps->capacity *= 2;
  37. }
  38. int i = 0;
  39. for (i = ps->size; i>0; i -- )
  40. {
  41. ps->a[i] = ps->a[i - 1];
  42. }
  43. ps->a[0] = x;
  44. ps->size++;
  45. return;
  46. }
  1. 头删与尾删

在进行头删操作的时候,我们就不需要像头插一样考虑空间的问题了,我们只需要对头部数据进行重新赋值,效果就等同于删除了。因此我们需要从第二个数据开始,赋值给它的前一个数据,依次向后推,直到最后一个数据也完成对前一个数据的赋值为之。

尾插则更简单了,只需要让控制顺序表数据个数的size-1即可。

注意在进行上述删除操作之前,都需要对size的值进行一个判断,当它为0时理应终止删除,即让size停止自减操作,否则size有可能减成负数,而导致后续插入操作出现问题。

  1. //尾删
  2. void SeqListPopBack(SeqList* ps)
  3. {
  4. if (0 == ps->size)
  5. return;
  6. ps->size--;
  7. }
  8. //头删
  9. void SeqListPopFront(SeqList* ps)
  10. {
  11. if (0 == ps->size)
  12. return;
  13. int i = 0;
  14. for (i = 0; i < ps->size - 1; i++)
  15. {
  16. ps->a[i] = ps->a[i + 1];
  17. }
  18. ps->size--;
  19. return;
  20. }
  1. 在指定位置插入与删除

不同于尾插尾删,要想在指定位置进行数据的插入与删除,需要考虑后面数据的影响,因此它与头插头删类似,只不过起始地址不同罢了。

类似于头插,在指定位置插入时,也要把后续数据不变顺序地向后挪一个空间,再把新数据插入到空出来的那个空间当中

类似于头删,我们只需要从指定位置的后一个数据开始,向前赋值,依次往后直到最后一个数据。

  1. //在pos位置插入x
  2. void SeqListInsert(SeqList* ps, int pos, SLDateType x)
  3. {
  4. /*扩容*/
  5. if (ps->size == ps->capacity || ps->size + 1 == ps->capacity)
  6. {
  7. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * ps->capacity * 2);
  8. /*增容失败*/
  9. if (tmp == NULL)
  10. {
  11. perror("SeqListPushFront::realloc");
  12. return;
  13. }
  14. /*增容成功*/
  15. ps->a = tmp;
  16. ps->capacity *= 2;
  17. }
  18. int i = 0;
  19. for (i = ps->size; i > pos; i--)
  20. {
  21. ps->a[i] = ps->a[i - 1];
  22. }
  23. ps->a[pos] = x;
  24. ps->size++;
  25. return;
  26. }
  27. //删除pos位置的值
  28. void SeqListErase(SeqList* ps, int pos)
  29. {
  30. if (0 == ps->size)
  31. return;
  32. if (pos > ps->size-1)
  33. return;
  34. int i = 0;
  35. for (i = pos; i < ps->size-1; i++)
  36. {
  37. ps->a[i] = ps->a[i + 1];
  38. }
  39. ps->size--;
  40. return 0;
  41. }
  1. 数据的显示与查找

显示与查找操作比较简单,都只用直接打印出来即可,就不过多介绍了,直接看代码。

  1. //显示(打印)
  2. void SeqListPrint(SeqList* ps)
  3. {
  4. for (int i = 0; i < ps->size; i++)
  5. {
  6. printf("%d ", ps->a[i]);
  7. }
  8. printf("\n");
  9. return;
  10. }
  11. //查找
  12. void SeqListFind(SeqList* ps, SLDateType x)
  13. {
  14. int i = 0, flag = 0;
  15. for (i = 0; i < ps->size; i++)
  16. {
  17. if (x == ps->a[i])
  18. {
  19. printf("找到了,下标是:%d\n", i);
  20. flag++;
  21. }
  22. }
  23. if (flag == 0)
  24. printf("没找到\n");
  25. return;
  26. }

四、代码呈现

最后附上完整代码:

  1. SeqList.h部分
  2. #pragma once
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #define INIT_CAPACITY 4
  7. typedef int SLDateType;//想修改顺序表内数据的类型可以直接在这修改
  8. typedef struct SeqList
  9. {
  10. SLDateType* a;//存放数据的地址
  11. int size;//当前已存放的数据个数
  12. int capacity;//当前最多存放的数据个数
  13. }SeqList;
  14. // 对数据的管理:增删查改
  15. //初始化
  16. void SeqListInit(SeqList* ps);
  17. //销毁
  18. void SeqListDestroy(SeqList* ps);
  19. //显示(打印)
  20. void SeqListPrint(SeqList* ps);
  21. //尾插
  22. void SeqListPushBack(SeqList* ps, SLDateType x);
  23. //头插
  24. void SeqListPushFront(SeqList* ps, SLDateType x);
  25. //头删
  26. void SeqListPopFront(SeqList* ps);
  27. //尾删
  28. void SeqListPopBack(SeqList* ps);
  29. // 顺序表查找
  30. void SeqListFind(SeqList* ps, SLDateType x);
  31. // 顺序表在pos位置插入x
  32. void SeqListInsert(SeqList* ps, int pos, SLDateType x);
  33. // 顺序表删除pos位置的值
  34. void SeqListErase(SeqList* ps, int pos);
  1. SeqList.c部分
  2. #pragma once
  3. #include"SeqList.h"
  4. //初始化
  5. void SeqListInit(SeqList* ps)
  6. {
  7. ps->a = (SLDateType*)malloc(INIT_CAPACITY * sizeof(SLDateType));
  8. if (NULL == ps->a)
  9. {
  10. perror("SeqListInit::calloc");
  11. return;
  12. }
  13. ps->size = 0;
  14. ps->capacity = INIT_CAPACITY;
  15. return;
  16. }
  17. //销毁
  18. void SeqListDestroy(SeqList* ps)
  19. {
  20. free(ps->a);
  21. ps->a = NULL;
  22. ps->capacity = ps->size = 0;
  23. return;
  24. }
  25. //显示(打印)
  26. void SeqListPrint(SeqList* ps)
  27. {
  28. for (int i = 0; i < ps->size; i++)
  29. {
  30. printf("%d ", ps->a[i]);
  31. }
  32. printf("\n");
  33. return;
  34. }
  35. //尾插
  36. void SeqListPushBack(SeqList* ps, SLDateType x)
  37. {
  38. /*增容*/
  39. if (ps->size == ps->capacity)
  40. {
  41. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType)*ps->capacity * 2);
  42. /*增容失败*/
  43. if (tmp == NULL)
  44. {
  45. perror("SeqListPushFront::realloc");
  46. return;
  47. }
  48. /*增容成功*/
  49. ps->a = tmp;
  50. ps->capacity *= 2;
  51. }
  52. ps->a[ps->size++] = x;
  53. return;
  54. }
  55. //尾删
  56. void SeqListPopBack(SeqList* ps)
  57. {
  58. if (0 == ps->size)
  59. return;
  60. ps->size--;
  61. }
  62. //头插
  63. void SeqListPushFront(SeqList* ps, SLDateType x)
  64. {
  65. /*扩容*/
  66. if (ps->size == ps->capacity || ps->size + 1 == ps->capacity)
  67. {
  68. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * ps->capacity * 2);
  69. /*增容失败*/
  70. if (tmp == NULL)
  71. {
  72. perror("SeqListPushFront::realloc");
  73. return;
  74. }
  75. /*增容成功*/
  76. ps->a = tmp;
  77. ps->capacity *= 2;
  78. }
  79. int i = 0;
  80. for (i = ps->size; i>0; i -- )
  81. {
  82. ps->a[i] = ps->a[i - 1];
  83. }
  84. ps->a[0] = x;
  85. ps->size++;
  86. return;
  87. }
  88. //头删
  89. void SeqListPopFront(SeqList* ps)
  90. {
  91. if (0 == ps->size)
  92. return;
  93. int i = 0;
  94. for (i = 0; i < ps->size - 1; i++)
  95. {
  96. ps->a[i] = ps->a[i + 1];
  97. }
  98. ps->size--;
  99. return;
  100. }
  101. //查找
  102. void SeqListFind(SeqList* ps, SLDateType x)
  103. {
  104. int i = 0, flag = 0;
  105. for (i = 0; i < ps->size; i++)
  106. {
  107. if (x == ps->a[i])
  108. {
  109. printf("找到了,下标是:%d\n", i);
  110. flag++;
  111. }
  112. }
  113. if (flag == 0)
  114. printf("没找到\n");
  115. return;
  116. }
  117. //在pos位置插入x
  118. void SeqListInsert(SeqList* ps, int pos, SLDateType x)
  119. {
  120. /*扩容*/
  121. if (ps->size == ps->capacity || ps->size + 1 == ps->capacity)
  122. {
  123. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * ps->capacity * 2);
  124. /*增容失败*/
  125. if (tmp == NULL)
  126. {
  127. perror("SeqListPushFront::realloc");
  128. return;
  129. }
  130. /*增容成功*/
  131. ps->a = tmp;
  132. ps->capacity *= 2;
  133. }
  134. int i = 0;
  135. for (i = ps->size; i > pos; i--)
  136. {
  137. ps->a[i] = ps->a[i - 1];
  138. }
  139. ps->a[pos] = x;
  140. ps->size++;
  141. return;
  142. }
  143. //删除pos位置的值
  144. void SeqListErase(SeqList* ps, int pos)
  145. {
  146. if (0 == ps->size)
  147. return;
  148. if (pos > ps->size-1)
  149. return;
  150. int i = 0;
  151. for (i = pos; i < ps->size-1; i++)
  152. {
  153. ps->a[i] = ps->a[i + 1];
  154. }
  155. ps->size--;
  156. return 0;
  157. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/500881
推荐阅读
相关标签
  

闽ICP备14008679号