当前位置:   article > 正文

数据结构:顺序表的实现

数据结构:顺序表的实现

1.SeqList.h  头文件

  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. //定义动态顺序表
  6. typedef int SLDatatype;
  7. typedef struct SeqLlist {
  8. SLDatatype* arr;
  9. int capacity;
  10. int size;
  11. }SL;
  12. //typedef struct SeqList Sl;
  13. //初始化
  14. void SLInit(SL* ps);
  15. //销毁
  16. void SLDestory(SL* ps);
  17. //尾插入数据
  18. void SLPushBack(SL* ps,SLDatatype x);
  19. //打印顺序表
  20. void SLPrint(SL* ps);
  21. //头插
  22. void SLPushFront(SL* ps,SLDatatype x);
  23. //删除
  24. void SLPopBack(SL* ps);
  25. void SLPopFront(SL* ps);
  26. //在指定位置之前插入数据
  27. void SLInsert(SL* ps, SLDatatype x, int pos);
  28. //删除指定位置的数据
  29. void SLErase(SL* ps, int pos);
  30. //查找数据
  31. int SLFind(SL* ps, SLDatatype x);

2.SeqList.c文件

  1. #include "Seqlist.h"
  2. //初始化
  3. //错误1:使用了未初始化的局部变量“s”
  4. //void SLInit(SL s)--值传递
  5. void SLInit(SL* ps)
  6. {
  7. ps->arr = NULL;
  8. ps->capacity = ps->size = 0;
  9. }
  10. //判断空间是否充足
  11. void SLCheckCapacity(SL* ps)
  12. {
  13. if (ps->size == ps->capacity) {
  14. //增容0*2=0
  15. //若capacity为0,给个默认值,否则*2
  16. int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
  17. SLDatatype* tmp = (SLDatatype*)realloc(ps->arr, newCapacity * sizeof(SLDatatype));
  18. if (tmp == NULL)
  19. {
  20. perror("realloc faile!");
  21. exit(1);
  22. }
  23. ps->arr = tmp;
  24. ps->capacity = newCapacity;
  25. }
  26. }
  27. //尾插
  28. void SLPushBack(SL* ps,SLDatatype x) {
  29. //温柔的解决方式
  30. //if (ps == NULL)
  31. //{
  32. // return;
  33. //}
  34. //粗暴的解决方式
  35. assert(ps);
  36. SLCheckCapacity(ps);
  37. ps->arr[ps->size] = x;
  38. ps->size++;
  39. }
  40. //打印操作
  41. void SLPrint(SL* ps)
  42. {
  43. for (int i = 0; i < ps->size; i++)
  44. {
  45. printf("%d ", ps->arr[i]);
  46. }
  47. printf("\n");
  48. }
  49. //头插
  50. void SLPushFront(SL* ps,SLDatatype x) {
  51. assert(ps);
  52. //判断空间是否足够
  53. SLCheckCapacity(ps);
  54. //数据整体后移动1位
  55. for (int i = ps->size; i > 0; i--)
  56. {
  57. ps->arr[i] = ps->arr[i - 1];
  58. }
  59. //下标为0的位置空出来
  60. ps->arr[0] = x;
  61. ps->size++;
  62. }
  63. //销毁
  64. void SLDestory(SL* ps)
  65. {
  66. if (ps->arr != NULL)
  67. {
  68. free(ps->arr);
  69. }
  70. ps->arr = NULL;
  71. ps->capacity = ps->size = 0;
  72. }
  73. //删除
  74. void SLPopBack(SL* ps) {
  75. assert(ps);
  76. assert(ps->size);
  77. ps->arr[ps->size - 1] = 0;
  78. ps->size--;
  79. }
  80. void SLPopFront(SL* ps) {
  81. assert(ps);
  82. assert(ps->size);
  83. //数据整体向前挪动一个位置
  84. for (int i = 0; i < ps->size - 1; i++)
  85. {
  86. ps->arr[i] = ps->arr[i + 1];
  87. }
  88. ps->size--;
  89. }
  90. //在指定位置之前插入数据(空间足够才能插入数据)
  91. void SLInsert(SL* ps, SLDatatype x, int pos)
  92. {
  93. assert(ps);
  94. assert(pos >= 0 && pos <= ps->size);
  95. SLCheckCapacity(ps);
  96. //pos之后的数据向后移动一位
  97. for (int i = ps->size; i > pos; i--)
  98. {
  99. ps->arr[i] = ps->arr[i-1];
  100. }
  101. ps->arr[pos] = x;
  102. ps->size++;
  103. }
  104. //删除指定位置的数据
  105. void SLErase(SL* ps, int pos)
  106. {
  107. assert(ps);
  108. assert(pos >= 0 && pos <= ps->size);
  109. //pos之后的数据整体向前移动1位
  110. for (int i = 0; i < ps->size - 1; i++)
  111. {
  112. ps->arr[i] = ps->arr[i + 1];
  113. }
  114. ps->size--;
  115. }
  116. //查找数据
  117. int SLFind(SL* ps, SLDatatype x)
  118. {
  119. assert(ps);
  120. for (int i = 0; i < ps->size; i++)
  121. {
  122. if (ps->arr[i] = x)
  123. {
  124. return i;
  125. }
  126. }
  127. //没有找到,返回一个无效的下标
  128. return -1;
  129. }

3.test.c文件

  1. #include "Seqlist.h"
  2. void SLtest01()
  3. {
  4. SL s;
  5. SLInit(&s);
  6. //SLPushBack(&s, 1);
  7. //SLPushBack(&s, 2);
  8. //SLPushBack(&s, 3);
  9. //SLPushBack(&s, 4);
  10. //SLPushBack(&s, 5);
  11. //SLPushBack(&s, 6);
  12. //SLPushBack(NULL, 6);
  13. SLPushFront(&s, 1);
  14. SLPushFront(&s, 2);
  15. SLPushFront(&s, 3);
  16. SLPushFront(&s, 4);
  17. //尾删除
  18. //SLPopBack(&s);
  19. //SLPopBack(&s);
  20. //SLPopBack(&s);
  21. //SLPopBack(&s);
  22. //SLPopBack(&s);
  23. //头删除
  24. //SLPopFront(&s);
  25. //为空不能删除
  26. //SLInsert(&s,11,0);
  27. //SLInsert(&s,22,s.size);
  28. //SLErase(&s, 0);
  29. //SLErase(&s, s.size-1);
  30. SLPrint(&s);
  31. //查找数据
  32. int find = SLFind(&s, 2);
  33. if (find >= 0)
  34. {
  35. printf("找到了\n");
  36. }
  37. else
  38. {
  39. printf("没有找到\n");
  40. }
  41. }
  42. int main()
  43. {
  44. SLtest01();
  45. return 0;
  46. }

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

闽ICP备14008679号