当前位置:   article > 正文

【数据结构】顺序表(线性表)的实现_1. 完成线性表的顺序存储及基本操作的实现(顺序表初始化、插入、删除功能); 2. 实

1. 完成线性表的顺序存储及基本操作的实现(顺序表初始化、插入、删除功能); 2. 实

目录

         一、什么是顺序表?

         二、顺序表的动态实现

               1、顺序表初始化

               2、顺序表打印

               3、顺序表检查空间

               4、顺序表尾插

               5、顺序表尾删

               6、顺序表头插

               7、顺序表头删

               8、顺序表指定位置插入

               9、顺序表指定位置删除

               10、顺序表查找

               11、顺序表销毁

         三、源代码

               1、SeqList.h

               2、SeqList.c

               3、test.c

 


 

一、什么是顺序表

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

顺序表的结构如下:

 

顺序表一般分为两种:

1、静态顺序表:使用定长数组存储元素。

  1. #define Max 10
  2. typedef int SLDataType;
  3. typedef struct SeqList
  4. {
  5. SLDataType data[Max]; //定长数组
  6. size_t size; //有效数据的个数
  7. }SeqList;

2、动态顺序表:使用动态开辟的数组存储。

  1. typedef int SLDataType;
  2. typedef struct SeqList
  3. {
  4. SLDataType* a; // 指向动态开辟数组的指针
  5. size_t size; // 有效数据个数
  6. size_t capicity; // 容量空间的大小
  7. }SeqList;

本文主要使用的是动态顺序表,因为静态顺序表是事先已经设置好数组空间的大小,万一需要的空间大小比事先设置的大,此时就会出现溢出的情况,使用不方便,所以在这里使用动态顺序表,用多少开辟多少的数组空间。

二、顺序表的动态实现

  1、顺序表初始化

  1. //初始化
  2. void SLInit(SL* ps)
  3. {
  4. assert(ps);
  5. ps->a = NULL;
  6. ps->size = 0;
  7. ps->capacity = 0;
  8. }

初始化的时候可以给顺序表开辟空间,当然也可以直接将指针a给设置为NULL,size和capacity同样设置为0。

  2、顺序表打印

  1. //打印
  2. void SLPrint(SL* ps)
  3. {
  4. assert(ps);
  5. for (int i = 0; i < ps->size; i++)
  6. {
  7. printf("%d ", ps->a[i]);
  8. }
  9. printf("\n");
  10. }

  3、顺序表检查空间

在顺序表插入和删除的时候会涉及到顺序表空间大小的问题,在插入的时候首先要判断一下,空间是否已满,满的情况要继续开辟空间,才能够插入。

  1. void SLCheckCapacity(SL* ps)
  2. {
  3. assert(ps);
  4. //扩容
  5. if (ps->size == ps->capacity)
  6. {
  7. int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
  8. SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));
  9. if (tmp == NULL)
  10. {
  11. perror("realloc fail");
  12. exit(-1);
  13. }
  14. ps->a = tmp;
  15. ps->capacity = newCapacity;
  16. }
  17. }

在这里之所以将检查封装为一个检查空间的函数,就是为了后面在插入的时候方便使用。

这个顺序表在刚创建的时候,其容量是0,此时我们就给它开辟4个元素大小的空间,当容量满的时候,我们就直接将容量翻倍。当我们在realloc函数中传入的是一个空指针的时候,此时的realloc函数就相当于一个malloc函数,所以在这里直接使用realloc函数也是可以的。

  4、顺序表尾插

  1. //尾插 O(1)
  2. void SLPushBack(SL* ps, SLDataType x)
  3. {
  4. assert(ps);
  5. SLCheckCapacity(ps);
  6. ps->a[ps->size] = x;
  7. ps->size++;
  8. }

首先我们要检查一下数组空间的大小,如果容量不满可以直接插入,如果容量已满就需要先扩容,在进行插入。 SLCheckCapacity()这个函数就是要检查容量是否已满,满的情况就会立即扩容。

  

  5、顺序表尾删

  1. //尾删
  2. void SLPopBack(SL* ps)
  3. {
  4. assert(ps);
  5. ///暴力检查
  6. assert(ps->size > 0);
  7. ps->size--;
  8. }

尾删其实只用size--就行,但是要注意一个问题就是在size = 0 的时候,顺序表本身就为空,那么就不能再减了,防止越界访问,因此在这里面加了两个断言就可以防止这种情况的发生。

  6、顺序表头插

  1. //头插 O(N)
  2. void SLPushFront(SL* ps, SLDataType x)
  3. {
  4. assert(ps);
  5. //检查容量够不够
  6. SLCheckCapacity(ps);
  7. //挪动数据
  8. int end = ps->size - 1;
  9. while (end >= 0)
  10. {
  11. ps->a[end + 1] = ps->a[end];
  12. end--;
  13. }
  14. ps->a[0] = x;
  15. ps->size++;
  16. }

头插首先肯定也要检查容量的大小, 保证容量大小充足,然后再将存入的数据整体向后移动一位,将第一个位置给空出来,最后插入新的元素。

  7、顺序表头删

  1. //头删
  2. void SLPopFront(SL* ps)
  3. {
  4. assert(ps);
  5. assert(ps->size > 0);
  6. //挪动数据
  7. int begin = 1;
  8. while (begin < ps->size)
  9. {
  10. ps->a[begin - 1] = ps->a[begin];
  11. begin++;
  12. }
  13. //暴力检查
  14. ps->size--;
  15. }

头删和尾删的大体逻辑是相同的,我们首先需要检查一下size是否为0。接下来的操作我们只需要让后面的数据覆盖前面的数据,最终就完成了头删的效果。

  8、顺序表指定位置插入 

任意位置插入就像是头插和尾插的升级版,只不过多了一个参数pos,如果pos在开始位置,相当于头插,如果pos在最后面,就相当于尾插。

  1. //在pos的位置插入数据
  2. void SLInsert(SL* ps, int pos, SLDataType x)
  3. {
  4. assert(ps);
  5. assert(pos >= 0);
  6. assert(pos <= ps->size);
  7. //检查容量够不够
  8. SLCheckCapacity(ps);
  9. int end = ps->size - 1;
  10. while (end >= pos)
  11. {
  12. ps->a[end + 1] = ps->a[end];
  13. end--;
  14. }
  15. ps->a[pos] = x;
  16. ps->size++;
  17. }

假设插入位置的下标是pos,那么我们只需要将pos位置到最后的所有数据整体向后移动,然后把pos的位置空出来,再在pos的位置插入一个新的元素,最后size++。

注意:assert(pos >= 0);assert(pos <= ps->size);这两句代码就是限制插入的元素是在0到size之间,而不是在其他地方,保证元素与元素之间是连续存储的。

 

  9、顺序表指定位置删除 

  1. //在pos的位置删除数据
  2. void SLErase(SL* ps, int pos)
  3. {
  4. assert(ps);
  5. assert(pos >= 0);
  6. assert(pos < ps->size);
  7. //挪动数据覆盖
  8. int begin = pos + 1;
  9. while (begin < ps->size)
  10. {
  11. ps->a[begin - 1] = ps->a[begin];
  12. begin++;
  13. }
  14. ps->size--;
  15. }

首先需要断言一下size是否为0。然后将pos后面的数据整体向前移动,覆盖原来pos位置所在的数据,最终size–。

  10、顺序表查找

  1. int SLFind(SL* ps, SLDataType x, int begin)
  2. {
  3. assert(ps);
  4. for (int i = begin; i < ps->size; ++i)
  5. {
  6. if (ps->a[i] == x)
  7. {
  8. return i;
  9. }
  10. }
  11. return -1;
  12. }

这个函数接口就很简单了,只需遍历一遍数组就行,找到就返回元素的下标,找不到返回-1。

  11、顺序表销毁

  1. //销毁
  2. void SLDestory(SL* ps)
  3. {
  4. assert(ps);
  5. //if (ps->a != NULL)
  6. if (ps->a)
  7. {
  8. free(ps->a);
  9. ps->a = NULL;
  10. ps->capacity = 0;
  11. ps->size = 0;
  12. }
  13. }

我们开始的时候动态开辟的空间是在堆区申请的,我们用完之后一定要将它释放,防止内存泄漏。

三、源代码

  1、SeqList.h

  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. //动态的顺序表 - 按需扩空间
  6. #define N 10
  7. typedef int SLDataType;
  8. typedef struct SeqList
  9. {
  10. SLDataType* a;
  11. int size;//记录存储多少个有效数据
  12. int capacity;//空间容量大小
  13. }SL;
  14. void SLPrint(SL* ps);//打印
  15. void SLInit(SL* ps);//初始化
  16. void SLDestory(SL* ps);//销毁
  17. void SLCheckCapacity(SL* ps);//检查空间大小
  18. //尾插
  19. void SLPushBack(SL* ps, SLDataType x);
  20. //尾删
  21. void SLPopBack(SL* ps);
  22. //头插
  23. void SLPushFront(SL* ps, SLDataType x);
  24. //头删
  25. void SLPopFront(SL* ps);
  26. //中间的插入
  27. //在pos位置插入数据
  28. void SLInsert(SL* ps, int pos, SLDataType x);
  29. //中间的删除
  30. //删除pos位置的数据
  31. void SLErase(SL* ps, int pos);
  32. //查找
  33. int SLFind(SL* ps, SLDataType x);

  2、SeqList.c

  1. #include "SeqList.h"
  2. //打印
  3. void SLPrint(SL* ps)
  4. {
  5. assert(ps);
  6. for (int i = 0; i < ps->size; i++)
  7. {
  8. printf("%d ", ps->a[i]);
  9. }
  10. printf("\n");
  11. }
  12. //初始化
  13. void SLInit(SL* ps)
  14. {
  15. assert(ps);
  16. ps->a = NULL;
  17. ps->size = 0;
  18. ps->capacity = 0;
  19. }
  20. //销毁
  21. void SLDestory(SL* ps)
  22. {
  23. assert(ps);
  24. //if (ps->a != NULL)
  25. if (ps->a)
  26. {
  27. free(ps->a);
  28. ps->a = NULL;
  29. ps->capacity = 0;
  30. ps->size = 0;
  31. }
  32. }
  33. void SLCheckCapacity(SL* ps)
  34. {
  35. assert(ps);
  36. //扩容
  37. if (ps->size == ps->capacity)
  38. {
  39. int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
  40. SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));
  41. if (tmp == NULL)
  42. {
  43. perror("realloc fail");
  44. exit(-1);
  45. }
  46. ps->a = tmp;
  47. ps->capacity = newCapacity;
  48. }
  49. }
  50. //尾插 O(1)
  51. void SLPushBack(SL* ps, SLDataType x)
  52. {
  53. assert(ps);
  54. SLCheckCapacity(ps);
  55. ps->a[ps->size] = x;
  56. ps->size++;
  57. }
  58. //尾删
  59. void SLPopBack(SL* ps)
  60. {
  61. assert(ps);
  62. //暴力检查
  63. assert(ps->size > 0);
  64. ps->size--;
  65. }
  66. //头插 O(N)
  67. void SLPushFront(SL* ps, SLDataType x)
  68. {
  69. assert(ps);
  70. //检查容量够不够
  71. SLCheckCapacity(ps);
  72. //挪动数据
  73. int end = ps->size - 1;
  74. while (end >= 0)
  75. {
  76. ps->a[end + 1] = ps->a[end];
  77. end--;
  78. }
  79. ps->a[0] = x;
  80. ps->size++;
  81. }
  82. //头删
  83. void SLPopFront(SL* ps)
  84. {
  85. assert(ps);
  86. assert(ps->size > 0);
  87. //挪动数据
  88. int begin = 1;
  89. while (begin < ps->size)
  90. {
  91. ps->a[begin - 1] = ps->a[begin];
  92. begin++;
  93. }
  94. //暴力检查
  95. ps->size--;
  96. }
  97. //在pos的位置插入数据
  98. void SLInsert(SL* ps, int pos, SLDataType x)
  99. {
  100. assert(ps);
  101. assert(pos >= 0);
  102. assert(pos <= ps->size);
  103. //检查容量够不够
  104. SLCheckCapacity(ps);
  105. int end = ps->size - 1;
  106. while (end >= pos)
  107. {
  108. ps->a[end + 1] = ps->a[end];
  109. end--;
  110. }
  111. ps->a[pos] = x;
  112. ps->size++;
  113. }
  114. //在pos的位置删除数据
  115. void SLErase(SL* ps, int pos)
  116. {
  117. assert(ps);
  118. assert(pos >= 0);
  119. assert(pos < ps->size);
  120. //挪动数据覆盖
  121. int begin = pos + 1;
  122. while (begin < ps->size)
  123. {
  124. ps->a[begin - 1] = ps->a[begin];
  125. begin++;
  126. }
  127. ps->size--;
  128. }
  129. //查找
  130. int SLFind(SL* ps, SLDataType x)
  131. {
  132. assert(ps);
  133. for (int i = 0; i < ps->size; ++i)
  134. {
  135. if (ps->a[i] == x)
  136. {
  137. //找到
  138. return i;
  139. }
  140. }
  141. //没找到
  142. return -1;
  143. }

  3、test.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include"SeqList.h"
  3. void menu()
  4. {
  5. printf("*******************************\n");
  6. printf("****** 0.退出 1.打印 ******\n");
  7. printf("****** 2.尾插 3.尾删 ******\n");
  8. printf("****** 4.头插 5.头删 ******\n");
  9. printf("****** 6.任意插 7.任意删******\n");
  10. printf("****** 8.在pos位置插入 ******\n");
  11. printf("****** 9.删除pos元素值 ******\n");
  12. printf("*******************************\n");
  13. }
  14. int main()
  15. {
  16. int input = 1;
  17. SL s;
  18. SLInit(&s); //创建一个顺序表
  19. while (input)
  20. {
  21. int pos = 0;
  22. SLDatatype x = 0;
  23. SLDatatype y = 0;
  24. menu();
  25. printf("请选择:>");
  26. scanf("%d", &input);
  27. switch (input)
  28. {
  29. case 0:
  30. printf("退出顺序表!\n");
  31. SLDestroy(&s);
  32. break;
  33. case 1:
  34. SLPrint(&s);
  35. break;
  36. case 2:
  37. printf("请输入一个值:>");
  38. scanf("%d", &x);
  39. SLPushBack(&s, x);
  40. break;
  41. case 3:
  42. SLPopBack(&s);
  43. break;
  44. case 4:
  45. printf("请输入一个值:>");
  46. scanf("%d", &x);
  47. SLPushFront(&s, x);
  48. break;
  49. case 5:
  50. SLPopFront(&s);
  51. break;
  52. case 6:
  53. printf("请输入下标和目标值:>");
  54. scanf("%d %d", &pos, &x);
  55. SLInsert(&s, pos, x);
  56. break;
  57. case 7:
  58. printf("请输入下标:>");
  59. scanf("%d", &pos);
  60. SLErase(&s, pos);
  61. break;
  62. case 8:
  63. printf("请输入要插入元素值和目标值:>");
  64. scanf("%d %d", &y, &x);
  65. SLInsert(&s, SeqListFind(&s, y), x);
  66. break;
  67. case 9:
  68. printf("请输入要删除元素值:>");
  69. scanf("%d", &y);
  70. SLErase(&s, SeqListFind(&s, y));
  71. break;
  72. default:
  73. printf("选择错误,请重新选择!\n");
  74. break;
  75. }
  76. }
  77. return 0;
  78. }


 本文要是有不足的地方,欢迎大家在下面评论,我会在第一时间更正。

  老铁们,记着点赞加关注哦!!!

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

闽ICP备14008679号