当前位置:   article > 正文

数据结构2-顺序表_顺序表最大容量和默认最大长度的区别

顺序表最大容量和默认最大长度的区别

                                           数据结构2-顺序表

数据结构2-顺序表

顺序表就是一段有序的数组。 
静态顺序表:就是顺序表的最大长度是有确定值的。 
动态顺序表:相对于静态顺序表,最大长度命名为容量,容量可以在初始化顺序表时候给出,容量可以根据后续需求通过realloc函数进行动态分配改变。


举个栗子——–动态顺序表

  1. //Seqlist.h
  2. #ifndef Seqlist_h_
  3. #define Seqlist_h_
  4. //实现动态顺序表
  5. #include<stdio.h>
  6. #include<windows.h>
  7. #include<assert.h>
  8. typedef int DataType;
  9. typedef struct SeqList
  10. {
  11. DataType* a;
  12. size_t size; // 有效数据个数,相当于数组中的下标。
  13. size_t capacity; // 容量
  14. }SeqList;
  15. void SeqPrint(SeqList* pSeq);
  16. void SeqInit(SeqList* pSeq,size_t capacity);//初始化顺序表
  17. void SeqDestory(SeqList* pSeq);//销毁顺序表
  18. void checkcapa(SeqList*pSeq);//动态特征,防止越界访问。
  19. void SeqPushBack(SeqList* pSeq, DataType x);//尾插
  20. void SeqPopBack(SeqList* pSeq);//尾删
  21. void SeqPushFront(SeqList* pSeq, DataType x);//头插
  22. void SeqPopFront(SeqList* pSeq);//头删
  23. void SeqInsert(SeqList* pSeq, size_t pos, DataType x);//pos位置插入x
  24. void SeqErase(SeqList* pSeq, size_t pos);//
  25. int SeqFind(SeqList* pSeq, DataType x);//查找x
  26. void SeqAt(SeqList* pSeq, size_t pos, DataType x);//修改posw位置的数据
  27. void BubbleSort(SeqList* pSeq);//冒泡排序
  28. void SelectSort(SeqList* pSeq);//选择排序
  29. int BinarySearch(SeqList* pSeq,DataType x);//二分查找
  30. void test();
  31. #endif
  32. //源.c
  33. #include"Seqlist.h"
  34. void SeqPrint(SeqList*pSeq)
  35. {
  36. printf("输出的顺序表有%d个元素\n", pSeq->size);
  37. for (size_t i = 0; i<pSeq->size; i++)
  38. {
  39. printf("%d ", pSeq->a[i]);
  40. }
  41. printf("\n");
  42. }
  43. void SeqInit(SeqList* pSeq,size_t capacity)
  44. {
  45. assert(pSeq&&capacity>0);
  46. pSeq->a = (DataType*)malloc(capacity*sizeof(DataType));
  47. assert(pSeq->a);
  48. pSeq->capacity = capacity;
  49. pSeq->size = 0;
  50. }
  51. void checkcapa(SeqList*pSeq)
  52. {
  53. if (pSeq->size >= pSeq->capacity)
  54. {
  55. DataType*a = (DataType*)realloc(pSeq, sizeof(DataType)* 2 * pSeq->capacity);
  56. assert(a);
  57. pSeq->a = a;
  58. pSeq->capacity *= 2;
  59. }
  60. }
  61. void SeqPushBack(SeqList* pSeq, DataType x)
  62. {
  63. assert(pSeq);
  64. checkcapa(pSeq);
  65. pSeq->a[pSeq->size] = x;
  66. pSeq->size++;
  67. }
  68. void SeqPopBack(SeqList* pSeq)
  69. {
  70. assert(pSeq);
  71. pSeq->a[pSeq->size - 1] = 0;
  72. pSeq->size--;
  73. }
  74. void SeqPushFront(SeqList* pSeq, DataType x)
  75. {
  76. assert(pSeq);
  77. checkcapa(pSeq);
  78. for (size_t i =pSeq->size; i >0; i--)
  79. {
  80. pSeq->a[i] = pSeq->a[i-1];
  81. }
  82. pSeq->a[0] = x;
  83. pSeq->size++;
  84. }
  85. void SeqPopFront(SeqList* pSeq)
  86. {
  87. size_t tmp=1;
  88. assert(pSeq);
  89. while (tmp<pSeq->size)
  90. {
  91. pSeq->a[tmp -1] = pSeq->a[tmp];
  92. tmp++;
  93. }
  94. pSeq->size--;
  95. }
  96. void SeqDestory(SeqList* pSeq)
  97. {
  98. assert(pSeq);
  99. free(pSeq->a);
  100. pSeq->a = NULL;
  101. pSeq->size = pSeq->capacity = 0;
  102. }
  103. void SeqInsert(SeqList* pSeq, size_t pos, DataType x)//pos后插入x
  104. {
  105. assert(pSeq);
  106. assert(pos < pSeq->size);
  107. checkcapa(pSeq);
  108. for (size_t i = pSeq->size - 1; i>pos; i--)
  109. {
  110. pSeq->a[i + 1] = pSeq->a[i];
  111. }
  112. pSeq->a[pos + 1] = x;
  113. pSeq->size++;
  114. }
  115. void SeqErase(SeqList* pSeq, size_t pos)//删除pos位置的元素
  116. {
  117. assert(pSeq&&pos<pSeq->size);
  118. for (size_t i = pos; i < pSeq->size; i++)
  119. {
  120. pSeq->a[i - 1] = pSeq->a[i];
  121. }
  122. pSeq->size--;
  123. }
  124. int SeqFind(SeqList* pSeq, DataType x)//在顺序表中找x
  125. {
  126. size_t i = 0;
  127. assert(pSeq);
  128. while (i < pSeq->size)
  129. {
  130. if (pSeq->a[i] == x)
  131. return x;
  132. i++;
  133. }
  134. return -1;
  135. }
  136. void SeqAt(SeqList* pSeq, size_t pos, DataType x)//修改pos位置的数据
  137. {
  138. assert(pSeq&&pos<pSeq->size);
  139. pSeq->a[pos] = x;
  140. }
  141. void Swap(DataType* a, DataType* b)
  142. {
  143. *a ^=* b;
  144. *b ^= *a;
  145. *a ^= *b;
  146. }
  147. void BubbleSort(SeqList* pSeq)//冒泡排序
  148. {
  149. assert(pSeq);
  150. for (size_t i = 0; i < pSeq->size - 1; i++)
  151. {
  152. int flag = 0;
  153. for (size_t j = 0; j < pSeq->size - i - 1; j++)
  154. {
  155. if (pSeq->a[j]>pSeq->a[j + 1])
  156. {
  157. Swap(&pSeq->a[j], &pSeq->a[j + 1]);
  158. flag = 1;
  159. }
  160. }
  161. if (flag == 0)
  162. break;
  163. }
  164. }
  165. void SelectSort(SeqList* pSeq)
  166. {
  167. size_t left = 0, right = pSeq->size-1;
  168. while (left < right)
  169. {
  170. size_t max=left, min=left;
  171. size_t i = left;
  172. while (i <= right)
  173. {
  174. if (pSeq->a[i] > pSeq->a[max])
  175. max = i;
  176. if (pSeq->a[i] < pSeq->a[min])
  177. min = i;
  178. ++i;
  179. }
  180. Swap(&pSeq->a[left], &pSeq->a[min]);
  181. if (left == max)
  182. max = min;
  183. Swap(&pSeq->a[right], &pSeq->a[max]);
  184. if (right==min)
  185. min=max;
  186. ++left;
  187. --right;
  188. }
  189. }
  190. int BinarySearch(SeqList* pSeq,DataType x)//二分查找
  191. {
  192. assert(pSeq);
  193. size_t head = 0, tail = pSeq->size - 1;
  194. if (x == pSeq->a[head])
  195. return pSeq->a[head];
  196. else if (x == pSeq->a[tail])
  197. return pSeq->a[tail];
  198. else
  199. {
  200. size_t mid = head + ((tail - head) >> 1);
  201. if (x < pSeq->a[mid])
  202. tail = mid - 1;
  203. else if (x>pSeq->a[mid])
  204. head = mid + 1;
  205. else
  206. return pSeq->a[mid];
  207. }
  208. return -1;
  209. }
  210. void test1()
  211. {
  212. SeqList s;
  213. SeqInit(&s, 20);
  214. SeqPushBack(&s, 0);
  215. SeqPushBack(&s, 1);
  216. SeqPushBack(&s, 2);
  217. SeqPushBack(&s, 3);
  218. SeqPushBack(&s, 4);
  219. SeqErase(&s, 0);
  220. SeqPopBack(&s);
  221. SeqPopBack(&s);
  222. SeqInsert(&s, 0, 9);
  223. SeqPrint(&s);
  224. }
  225. void test2()
  226. {
  227. SeqList s;
  228. SeqInit(&s, 20);
  229. SeqPushBack(&s, 0);
  230. SeqPushBack(&s, 1);
  231. SeqPushBack(&s, 2);
  232. SeqPushFront(&s, 10);
  233. SeqPushFront(&s, 20);
  234. SeqPushFront(&s, 30);
  235. SeqAt(&s,0,50);
  236. SeqPrint(&s);
  237. }
  238. void test3()
  239. {
  240. SeqList s;
  241. SeqInit(&s, 20);
  242. SeqPushBack(&s, 0);
  243. SeqPushBack(&s, 1);
  244. SeqPushBack(&s, 2);
  245. SeqPushBack(&s, 3);
  246. SeqPopFront(&s);
  247. SeqPopFront(&s);
  248. SeqPrint(&s);
  249. }
  250. void test4()
  251. {
  252. SeqList s;
  253. SeqInit(&s, 20);
  254. SeqPushBack(&s, 0);
  255. SeqPushBack(&s, 1);
  256. printf("%d\n", SeqFind(&s, 0));
  257. printf("%d\n", SeqFind(&s, 1));
  258. printf("%d\n", SeqFind(&s, 2));
  259. }
  260. void test5()
  261. {
  262. SeqList s;
  263. SeqInit(&s, 20);
  264. SeqPushBack(&s, 10);
  265. SeqPushBack(&s, 1);
  266. SeqPushBack(&s, 5);
  267. SeqPushBack(&s, 3);
  268. SeqPushBack(&s, 99);
  269. SeqPrint(&s);
  270. printf("%d\n", BinarySearch(&s, 5));
  271. printf("%d\n", BinarySearch(&s, 2));
  272. SeqPrint(&s);
  273. //BubbleSort(&s);
  274. SelectSort(&s);
  275. SeqPrint(&s);
  276. }
  277. int main()
  278. {
  279. //test1();
  280. //test2();
  281. //test3();
  282. //test4();
  283. test5();
  284. system("pause");
  285. return 0;
  286. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/474692
推荐阅读
相关标签
  

闽ICP备14008679号