当前位置:   article > 正文

手搓顺序表(C语言)

手搓顺序表(C语言)

目录

SeqList.h 

SeqList.c 

 头插尾插复用任意位置插入

头删尾删复用任意位置删除

SLtest.c 

测试示例

 顺序表优劣分析


SeqList.h 

  1. //SeqList.h
  2. #pragma once
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #define IN_CY 3
  7. typedef int SLdataType;
  8. typedef struct SeqList
  9. {
  10. SLdataType* a;
  11. int size;
  12. int capacity;
  13. }SeqList;
  14. //初始化函数
  15. void SeqListInit(SeqList* ps);
  16. //销毁函数
  17. void SeqListDestory(SeqList* ps);
  18. //尾插
  19. void SeqListPushBack(SeqList* ps, SLdataType x);
  20. //尾删
  21. void SeqListPopBack(SeqList* ps);
  22. //打印函数
  23. void print(SeqList* ps);
  24. //头插
  25. void SeqListPushFront(SeqList* ps, SLdataType x);
  26. //头删
  27. void SeqListPopFront(SeqList* ps);
  28. // 顺序表在pos位置插入x
  29. void SeqListInsert(SeqList* ps, int pos, SLdataType x);
  30. // 顺序表删除pos位置的值
  31. void SeqListErase(SeqList* ps, int pos);

SeqList.c 

  1. //SeqList.C
  2. #include "SeqList.h"
  3. //初始化顺序表
  4. void SeqListInit(SeqList* ps)
  5. {
  6. assert(ps);
  7. ps->size = 0;
  8. ps->capacity = 3;
  9. ps->a = (SLdataType*)malloc(sizeof(SLdataType) * ps->capacity);
  10. //申请空间失败
  11. if (ps->a == NULL)
  12. {
  13. perror("SeqListInit");
  14. exit(-1);
  15. }
  16. }
  17. //销毁顺序表
  18. void SeqListDestory(SeqList* ps)
  19. {
  20. assert(ps);
  21. ps->size = 0;
  22. ps->capacity = 0;
  23. free(ps->a);
  24. ps->a = NULL;
  25. }
  26. //容量检查
  27. void SLCheckCapacity(SeqList* ps)
  28. {
  29. assert(ps);
  30. if (ps->size == ps->capacity)
  31. {
  32. ps->capacity += IN_CY;
  33. SLdataType* tmp = (SLdataType*)realloc(ps->a, sizeof(SLdataType) * ps->capacity);
  34. //申请空间失败
  35. if (tmp == NULL)
  36. {
  37. perror("SLCheckCapacity");
  38. exit(-1);
  39. }
  40. ps->a = tmp;
  41. }
  42. }
  43. //尾插
  44. void SeqListPushBack(SeqList* ps, SLdataType x)
  45. {
  46. assert(ps);
  47. SLCheckCapacity(ps);
  48. ps->a[ps->size++] = x;
  49. }
  50. //尾删
  51. void SeqListPopBack(SeqList* ps)
  52. {
  53. assert(ps);
  54. assert(ps->size);
  55. ps->size--;
  56. }
  57. //打印顺序表
  58. void print(SeqList* ps)
  59. {
  60. assert(ps);
  61. int i;
  62. for (i = 0; i < ps->size; i++)
  63. {
  64. printf("%d ", ps->a[i]);
  65. }
  66. printf("\n");
  67. }
  68. //头插
  69. void SeqListPushFront(SeqList* ps, SLdataType x)
  70. {
  71. assert(ps);
  72. SLCheckCapacity(ps);
  73. int end = ps->size;
  74. while (end--)
  75. {
  76. ps->a[end + 1] = ps->a[end];
  77. }
  78. ps->size++;
  79. ps->a[0] = x;
  80. }
  81. //头删
  82. void SeqListPopFront(SeqList* ps)
  83. {
  84. assert(ps);
  85. assert(ps->size);
  86. int i;
  87. for (i = 0; i < ps->size - 1; i++)
  88. {
  89. ps->a[i] = ps->a[i + 1];
  90. }
  91. ps->size--;
  92. }
  93. // 顺序表在pos位置插入x
  94. void SeqListInsert(SeqList* ps, int pos, SLdataType x)
  95. {
  96. assert(ps);
  97. //下标合法检查
  98. assert(pos>= 0 && pos <= ps->size);
  99. SLCheckCapacity(ps);
  100. int end = ps->size;
  101. while (end >= pos)
  102. {
  103. ps->a[end + 1] = ps->a[end];
  104. end--;
  105. }
  106. ps->size++;
  107. ps->a[pos] = x;
  108. }
  109. // 顺序表删除pos位置的值
  110. void SeqListErase(SeqList* ps, int pos)
  111. {
  112. assert(ps);
  113. //下标合法检查
  114. assert(pos >= 0 && pos < ps->size);
  115. int i;
  116. for (i = pos; i < ps->size - 1; i++)
  117. {
  118. ps->a[i] = ps->a[i + 1];
  119. }
  120. ps->size--;
  121. }

 头插尾插复用任意位置插入

  1. // 顺序表在pos位置插入x
  2. void SeqListInsert(SeqList* ps, int pos, SLdataType x)
  3. {
  4. assert(ps);
  5. //下标合法检查
  6. assert(pos>= 0 && pos <= ps->size);
  7. SLCheckCapacity(ps);
  8. int end = ps->size;
  9. while (end >= pos)
  10. {
  11. ps->a[end + 1] = ps->a[end];
  12. end--;
  13. }
  14. ps->size++;
  15. ps->a[pos] = x;
  16. }
  17. //头插
  18. void SeqListPushFront(SeqList* ps, SLdataType x)
  19. {
  20. SeqListInsert(ps, 0, x);
  21. }
  22. //尾插
  23. void SeqListPushBack(SeqList* ps, SLdataType x)
  24. {
  25. SeqListInsert(ps, ps->size, x);
  26. }

头删尾删复用任意位置删除

  1. // 顺序表删除pos位置的值
  2. void SeqListErase(SeqList* ps, int pos)
  3. {
  4. assert(ps);
  5. //下标合法检查
  6. assert(pos >= 0 && pos < ps->size);
  7. int i;
  8. for (i = pos; i < ps->size - 1; i++)
  9. {
  10. ps->a[i] = ps->a[i + 1];
  11. }
  12. ps->size--;
  13. }
  14. //头删
  15. void SeqListPopFront(SeqList* ps)
  16. {
  17. SeqListErase(ps, 0);
  18. }
  19. //尾删
  20. void SeqListPopBack(SeqList* ps)
  21. {
  22. SeqListErase(ps, ps->size - 1);
  23. }

SLtest.c 

  1. //SLtest.c
  2. #include "SeqList.h"
  3. void SLtest1()
  4. {
  5. SeqList s1;
  6. SeqListInit(&s1);
  7. SeqListPushBack(&s1, 1);
  8. SeqListPushBack(&s1, 2);
  9. SeqListPushBack(&s1, 3);
  10. print(&s1);
  11. SeqListPushBack(&s1, 4);
  12. SeqListPushBack(&s1, 5);
  13. print(&s1);
  14. SeqListPopBack(&s1);
  15. SeqListPopBack(&s1);
  16. SeqListPopBack(&s1);
  17. SeqListPopBack(&s1);
  18. SeqListPopBack(&s1);
  19. print(&s1);
  20. SeqListDestory(&s1);
  21. }
  22. void SLtest2()
  23. {
  24. SeqList s1;
  25. SeqListInit(&s1);
  26. SeqListPushFront(&s1, 1);
  27. SeqListPushFront(&s1, 2);
  28. SeqListPushFront(&s1, 3);
  29. print(&s1);
  30. SeqListPushFront(&s1, 4);
  31. SeqListPushFront(&s1, 5);
  32. print(&s1);
  33. //SeqListPopFront(&s1);
  34. //SeqListPopFront(&s1);
  35. SeqListPopFront(&s1);
  36. print(&s1);
  37. SeqListPopFront(&s1);
  38. print(&s1);
  39. SeqListPopFront(&s1);
  40. print(&s1);
  41. SeqListPopFront(&s1);
  42. print(&s1);
  43. SeqListDestory(&s1);
  44. }
  45. void SLtest3()
  46. {
  47. SeqList s1;
  48. SeqListInit(&s1);
  49. SeqListInsert(&s1, 0, 1);
  50. SeqListInsert(&s1, 0, 2);
  51. SeqListInsert(&s1, 0, 3);
  52. print(&s1);
  53. SeqListInsert(&s1, s1.size, 4);
  54. SeqListInsert(&s1, s1.size, 5);
  55. SeqListInsert(&s1, s1.size, 6);
  56. print(&s1);
  57. SeqListInsert(&s1, 1, 7);
  58. print(&s1);
  59. SeqListErase(&s1, s1.size - 1);
  60. print(&s1);
  61. }
  62. int main()
  63. {
  64. //测试尾插尾删
  65. //SLtest1();
  66. //测试头插头删
  67. //SLtest2();
  68. //测试任意位置插入删除
  69. SLtest3();
  70. return 0;
  71. }

测试示例

尾插:

尾删:

头插:

头删:

任意位置插入:

任意位置删除:

 顺序表优劣分析

        由于顺序表是一块连续的空间,因此可以直接通过下标访问而无需遍历寻找,所以在需要大量访问的程序中具有优势,对缓存的利用率高。而当空间不够扩容时一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。且对于异地扩容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。

        尾插和尾删的时间复杂度为O(1),因此适用于只需要尾插尾删的场景。而头插头删和任意位置插入删除为了保证数据的顺序性需要一个一个挪动数据,时间复杂度为0(n),因此对于需要大量随机存取的程序来说开销较大。

        因此顺序表通常应用于元素高效存储+频繁访问的场景。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/478816
推荐阅读
相关标签
  

闽ICP备14008679号