• 数据结构—顺序表


    目录

    一、线性表

    二、顺序表概念

     三、实现顺序表

    1、声明结构体

    2、初始化

    3、打印数据 

    4、销毁 

    5、尾插&头插

    尾插

    判断是否扩容 

     头插

    6、尾删&头删

    尾删

    头删

    7、 指定位置插入元素

    8、 删除指定位置元素

    9、 查找指定元素位置

    10、修改指定位置元素

    完整版附上:

    Seqlist.h

    Seqlist.c

     text.c


    一、线性表

    • 线性表是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...
    • 线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,
    • 线性表在物理上存储时,通常以数组和链式结构的形式存储。

    二、顺序表概念

    顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
    顺序表一般可以分为:
    1. 静态顺序表:使用定长数组存储元素。

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

     三、实现顺序表

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

     我们创建三个文件:

    1. Seqlist.h用于存放头文件、结构体和函数的声明
    2. text.c作为主程序进行运行和测试
    3. Seqlist.c存放函数主体

    1、声明结构体

    1. #include
    2. typedef int SLDatatype;
    3. typedef struct SeqList
    4. {
    5. SLDatatype* a;
    6. int size;
    7. int capacity;
    8. }SL;
    • 在头文件中声明结构体struct SeqList,由于名字太长,我们用typedef自定义结构体名称的别名为SL。
    • 将顺序表的数据类型用SLDatatype这个别名代替int,以后程序中使用到元素数据类型时都替换成SLDatatype,方便日后修改顺序表数据类型。
    • 定义size存储当前元素个数,capacity存储顺序表容量。

    2、初始化

    1. void SLInit(SL* psl)
    2. {
    3. assert(psl);
    4. psl->a = (SLDatatype*)malloc(sizeof(SLDatatype) * 4);
    5. if (psl->a == NULL) {
    6. perror("malloc fail");
    7. return;
    8. }
    9. psl->size = 0;
    10. psl->capacity = 4;
    11. }
    • 需要改变结构体变量,则将其地址传入函数。
    • assert检测传参顺序表指针是否合法,如果传入参数为空则报错(具体哪行出错)。
    • 然后为结构体成员a分配4个成员类型空间,顺便检查是否分配成功。
    • 对成员size和capacity分别复制为0(当前元素个数)和4(顺序表容量)。

    3、打印数据 

    1. void SLPrint(SL* psl)
    2. {
    3. assert(psl);
    4. for (int i = 0; i < psl->size; i++) {
    5. printf("%d ", psl->a[i]);
    6. }
    7. }
    • assert检测传参顺序表指针是否合法,然后循环遍历输出。

    4、销毁 

    1. void SLDestroy(SL* psl)
    2. {
    3. assert(psl);
    4. free(psl->a);
    5. psl->a = NULL;
    6. psl->size = 0;
    7. psl->capacity = 0;
    8. }
    • assert检测传参顺序表指针是否合法。

    • 释放动态开辟a的空间,同时赋值为空,不置空可能会造成野指针的访问。

    • size和capacity分别赋值为0。

    5、尾插&头插

    尾插

    1. void SLPushBack(SL* psl, SLDatatype x)
    2. {
    3. assert(psl);
    4. SLCheckCapacity(psl);
    5. psl->a[psl->size] = x;
    6. psl->size++;
    7. }
    • assert检测传参顺序表指针是否合法。

    • 判断数组是否已经装满,如果装满则进行扩容,这些操作我们在函数SLCheckCapacity内进行。

    • 将 x 赋值给 psl->a 数组中下一个可用的位置,即 psl->size 索引处。

    • 递增 psl->size,以便记录新元素的加入。

     接下来我们来讲解函数SLCheckCapacity:

    判断是否扩容 

    1. void SLCheckCapacity(SL* psl)
    2. {
    3. if (psl->size == psl->capacity) {
    4. SLDatatype* tmp = (SLDatatype*)realloc(psl->a, sizeof(SLDatatype) * psl->capacity * 2);
    5. if (tmp == NULL) {
    6. perror("realloc fail");
    7. return;
    8. }
    9. psl->a = tmp;
    10. psl->capacity *= 2;
    11. }
    12. }
    •  判断当前元素个数size与顺序表容量capacity是否相等,相等则对结构体成员指针a进行扩容。

    • 通过realloc函数对a的内存进行扩容,每次增加一倍容量,并将reallocd的返回值新空间的起始地址赋值给SLDatatype类型指针 tmp,这样避免直接对a开辟空间时发生错误丢失数据。

    • 检测是否成功扩容。

    • 最后将存放新空间地址的tmp赋值给a。

    • 顺序表的容量也随之扩大为原来的两倍。

    我们还可以优化一下尾插函数,具体如下: 

    1. void SLPushBack(SL* psl, SLDatatype x)//尾插
    2. {
    3. SLCheckCapacity(psl);
    4. psl->a[psl->size++] = x;
    5. }

     头插

    1. void SLPushFront(SL* psl, SLDatatype x)
    2. {
    3. assert(psl);
    4. SLCheckCapacity(psl);
    5. int end = psl->size - 1;
    6. while (end >= 0) {
    7. psl->a[end + 1] = psl->a[end];
    8. end--;
    9. }
    10. psl->a[0] = x;
    11. psl->size++;
    12. }
    • assert检测传参顺序表指针是否合法。

    • 判断数组是否已经装满,如果装满则进行扩容,这些操作在函数SLCheckCapacity内进行。

    • 定义end表示当前顺序表中最后一个元素的下标。

    • while循环用于从后向前将顺序表中的元素依次向后移动一个位置,为新元素 x 腾出空间

    • 将新元素 x 插入到顺序表的开头

    • 顺序表元素个数 size 增加1

    6、尾删&头删

    尾删

    1. void SLPopBack(SL* psl)
    2. {
    3. assert(psl);
    4. //暴力判断
    5. assert(psl->size == 0);
    6. //常规判断
    7. //if (psl->size == 0)
    8. // return;
    9. psl->a[psl->size - 1] = 0;
    10. psl->size--;
    11. }
    • 第一个assert检测传参顺序表指针是否合法。

    • 第二个assert检测顺序表是否为空,为空没有元素则不需要删除,程序报错。

    • 我们还可以通过顺序表元素个数判断,if语句判断size为0时,函数停止运行。

    • 顺序表大小不为空,则对最后一个元素进行删除,赋值为0。

    • 顺序表大小size减1.

    头删

    1. void SLPopFront(SL* psl)
    2. {
    3. assert(psl);
    4. assert(psl->size > 0);
    5. int start = 0;
    6. while (start < psl->size) {
    7. psl->a[start] = psl->a[start + 1];
    8. start++;
    9. }
    10. psl->size--;
    11. }
    • 第一个assert检测传参顺序表指针是否合法。

    • 第二个assert检测顺序表是否为空,为空没有元素则不需要删除,程序报错。

    • 定义变量start为首元素下标.

    • 头删不用赋值为0,直接从第一个元素开始后项赋值给前项。

    7、指定位置插入元素

    1. void SLInsert(SL* psl, int pos, SLDatatype x)
    2. {
    3. assert(psl);
    4. assert(0 <= pos && pos <= psl->size);
    5. SLCheckCapacity(psl);
    6. int end = psl->size - 1;
    7. while (end >= pos) {
    8. psl->a[end + 1] = psl->a[end];
    9. end--;
    10. }
    11. psl->size++;
    12. psl->a[pos] = x;
    13. }
    • 参数pos为要在第几个元素后插入。

    • 第一个assert检测传参顺序表指针是否合法。

    • 第二个assert检测传参pos的位置是否合法。

    • 判断数组是否已经装满,如果装满则进行扩容,这些操作在函数SLCheckCapacity内进行。

    • 定义end表示当前顺序表中最后一个元素的下标。

    • while循环用于从后向前将顺序表中的元素依次向后移动一个位置,为新元素 x 腾出空间

    • 顺序表元素个数 size 增加1。

    • 将新元素 x 插入到顺序表指定元素位置之后。

     SLInsert这个函数可以代替头插尾插函数进行顺序表元素的增加。

    8、删除指定位置元素

    1. void SLErase(SL* psl, int pos)
    2. {
    3. assert(psl);
    4. assert(0 <= pos && pos <= psl->size);
    5. int start = pos + 1;
    6. while (start < psl->size) {
    7. psl->a[start - 1] = psl->a[start];
    8. start++;
    9. }
    10. psl->size--;
    11. }
    • 第一个assert检测传参顺序表指针是否合法。

    • 第二个assert检测传参pos的位置是否合法。

    • 定义变量start存储pos位置后一项的下标。

    • 将删除元素后一项位置的值开始从pos+1向后依次前移一位,顶替pos位置。

    • 顺序表元素个数减一。

    这个函数就可以完全代替头删尾删了。

    9、查找指定元素位置

    1. int SLFind(SL* psl, SLDatatype x)
    2. {
    3. assert(psl);
    4. for (int i = 0; i < psl->size; i++) {
    5. if (psl->a[i] == x)
    6. return i+1;
    7. }
    8. return -1;
    9. }
    •  assert检测传参顺序表指针是否合法。

    • 循环遍历顺序表找出于x相等的元素,返回其下标。

    • 找不到返回-1。

    10、修改指定位置元素

    1. void SLModify(SL* psl, int pos, SLDatatype x)
    2. {
    3. assert(psl);
    4. assert(0 <= pos && pos <= psl->size);
    5. psl->a[pos] = x;
    6. }
    • 第一个assert检测传参顺序表指针是否合法。

    • 第二个assert检测传参pos的位置是否合法。

    • 将pos位置的值修改为x。

    完整版附上:

    Seqlist.h

    1. #include
    2. #include
    3. #include
    4. typedef int SLDatatype;
    5. typedef struct SeqList
    6. {
    7. SLDatatype* a;
    8. int size;
    9. int capacity;
    10. }SL;
    11. void SLInit(SL* psl);
    12. void SLDestroy(SL* psl);
    13. void SLPrint(SL* psl);
    14. void SLPushBack(SL* psl, SLDatatype x);
    15. void SLPushFront(SL* psl, SLDatatype x);
    16. void SLPopBack(SL* psl);
    17. void SLPopFront(SL* psl);
    18. void SLInsert(SL* psl, int pos, SLDatatype x);
    19. void SLErase(SL* psl, int pos);
    20. int SLFind(SL* psl, SLDatatype x);
    21. void SLModify(SL* psl, int pos, SLDatatype x);

    Seqlist.c

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include "Seqlist.h"
    3. void SLInit(SL* psl)//初始化
    4. {
    5. assert(psl);
    6. psl->a = (SLDatatype*)malloc(sizeof(SLDatatype) * 4);
    7. if (psl->a == NULL) {
    8. perror("malloc fail");
    9. return;
    10. }
    11. psl->size = 0;
    12. psl->capacity = 4;//每次开辟的容量为四个
    13. }
    14. void SLPrint(SL* psl)//打印数据
    15. {
    16. assert(psl);
    17. for (int i = 0; i < psl->size; i++) {
    18. printf("%d ", psl->a[i]);
    19. }
    20. }
    21. void SLDestroy(SL* psl)//结束使用需要销毁
    22. {
    23. assert(psl);
    24. free(psl->a);
    25. psl->a = NULL;
    26. psl->size = 0;
    27. psl->capacity = 0;
    28. }
    29. void SLCheckCapacity(SL* psl)
    30. {
    31. if (psl->size == psl->capacity) {
    32. //增加一倍容量
    33. SLDatatype* tmp = (SLDatatype*)realloc(psl->a, sizeof(SLDatatype) * psl->capacity * 2);
    34. if (tmp == NULL) {
    35. perror("realloc fail");
    36. return;
    37. }
    38. psl->a = tmp;
    39. psl->capacity *= 2;
    40. }
    41. }
    42. void SLPushBack(SL* psl, SLDatatype x)//尾插
    43. {
    44. assert(psl);
    45. //psl->a[psl->size] = x;
    46. //psl->size++;
    47. //插入需要判断a是否已满,已满需要扩容
    48. SLCheckCapacity(psl);
    49. //或者写成一句
    50. psl->a[psl->size++] = x;//后置自增
    51. }
    52. void SLPushFront(SL* psl, SLDatatype x)//头插
    53. {
    54. assert(psl);
    55. SLCheckCapacity(psl);
    56. int end = psl->size - 1;
    57. while (end >= 0) {
    58. psl->a[end + 1] = psl->a[end];
    59. end--;
    60. }
    61. psl->a[0] = x;
    62. psl->size++;
    63. }
    64. void SLPopBack(SL* psl)
    65. {
    66. assert(psl);//尾删
    67. //暴力判断
    68. //assert(psl->size == 0);
    69. //常规判断
    70. if (psl->size == 0)
    71. return;
    72. psl->a[psl->size - 1] = 0;
    73. psl->size--;
    74. }
    75. void SLPopFront(SL* psl)//头删
    76. {
    77. assert(psl);
    78. assert(psl->size > 0);
    79. int start = 0;
    80. while (start < psl->size) {
    81. psl->a[start] = psl->a[start + 1];
    82. start++;
    83. }
    84. psl->size--;
    85. }
    86. void SLInsert(SL* psl, int pos, SLDatatype x)//指定位置插入元素,可代替头插尾插
    87. {
    88. assert(psl);
    89. assert(0 <= pos && pos <= psl->size);//判读插入位置是否合法
    90. SLCheckCapacity(psl);
    91. int end = psl->size - 1;
    92. while (end >= pos) {
    93. psl->a[end + 1] = psl->a[end];
    94. end--;
    95. }
    96. psl->size++;
    97. psl->a[pos] = x;
    98. }
    99. void SLErase(SL* psl, int pos)//删除指定位置元素,代替头删尾删
    100. {
    101. assert(psl);
    102. assert(0 <= pos && pos <= psl->size);
    103. int start = pos + 1;
    104. while (start < psl->size) {
    105. psl->a[start - 1] = psl->a[start];
    106. start++;
    107. }
    108. psl->size--;
    109. }
    110. int SLFind(SL* psl, SLDatatype x)//查找指定元素位置
    111. {
    112. assert(psl);
    113. for (int i = 0; i < psl->size; i++) {
    114. if (psl->a[i] == x)
    115. return i+1;
    116. }
    117. return -1;//找不到返回-1
    118. }
    119. void SLModify(SL* psl, int pos, SLDatatype x)//修改指定位置元素
    120. {
    121. assert(psl);
    122. assert(0 <= pos && pos <= psl->size);
    123. psl->a[pos] = x;
    124. }

     text.c

    这里大家可以自行发挥!

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include "Seqlist.h"
    3. void test1()
    4. {
    5. SL s;
    6. SLInit(&s);
    7. SLPushBack(&s, 5);
    8. SLPushBack(&s, 9);
    9. SLPushBack(&s, 50);
    10. SLPushFront(&s, 1);
    11. SLPushFront(&s, 15);
    12. SLPushFront(&s, 0);
    13. SLPopBack(&s, 50);
    14. SLPopFront(&s, 0);
    15. SLInsert(&s, 2, 555);
    16. SLErase(&s, 4);
    17. SLPrint(&s);
    18. int a=SLFind(&s, 15);
    19. printf("\n%d\n", a);
    20. if (a != -1)
    21. SLErase(&s, a);
    22. SLPrint(&s);
    23. SLDestroy(&s);
    24. }
    25. int main()
    26. {
    27. test1();
    28. return 0;
    29. }

  • 相关阅读:
    【JAVA】Scanner的next()、nextInt()、nextLine()读取机制
    Mysql安装
    TOGAF架构内容—架构工件
    【Arduino26】88点阵显示液晶对比度实验
    《uni-app》表单组件-Button按钮
    在云栖深处见未来
    常用的深度学习自动标注软件
    03MyBatis-Plus中的常用注解
    socket.error: [Errno 10049]错误
    学信息系统项目管理师第4版系列34_10大管理49过程ITTO
  • 原文地址:https://blog.csdn.net/m0_73800602/article/details/133796428