• 【数据结构】——顺序表(增删查改)


     

    目录

     前言:

    顺序表: 

    1、概念及分类 

    1.1顺序表分类

    静态顺序表

    动态顺序表

     2、接口实现

    2.1功能要求

    2.2功能实现 

    💡初始化顺序表

     💡销毁顺序表

     💡顺序表尾插入

    💡检查是否扩容 

    💡打印顺序表 

     💡顺序表头插入

    💡顺序表头删除

    💡顺序表尾删除

    💡顺序表pos位置插入值

     💡顺序表删除pos的位置

     总代码

    test.c

     SeqList.c

     SeqList.h


     前言:

    数据结构是计算机存储、组织数据的方式。 

    数据结构是指相互之间存在⼀种或多种特定关系的数据元素的集合。数据结构反映数据的内部构成,即数据由那部分构成,以什么方式构成,以及数据元素之间呈现的结构。

    能够存储数据(如顺序表、链表等结构)
    存储的数据能够方便查找

    顺序表: 

    数据结构分为:线性表非线性表

            顺序表就是线性表中的一个小类。

    何为线性表:线性表是指n个具有相同性质的数据元素的有限序列,

            常见的线性表有:顺序表、链表、栈、队列、字符串等等

    注:线性表的物理结构不一定是线性的,它在逻辑结构上一定是线性的(这个很好理解,等我们学完顺序表和单链表这对黄金搭档,就明白这句话的含义了。(逻辑结构类似于想象的,比如箭头这种东西在内存中是存在的))

    顺序表是线性表,顺序表在逻辑结构和物理结构上都是线性的。 

     

    1、概念及分类 

    顺序表(SeqList):顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构连续存储数据,不能跳跃)。 

    类似于数组,但是顺序表是连续的(只能从头开始连续),数组不需要连续;

    1.1顺序表分类

    静态顺序表

    概念:使用定长数组存储元素

    缺陷:空间给少了不够⽤,给多了造成空间浪费

    1. //静态顺序表
    2. typedef int SLDataType;
    3. #define N 7
    4. struct SeqList
    5. {
    6. SLDataType arr[N]; //定长数组
    7. int size; //有效数据个数
    8. }SL;

    动态顺序表

    概念:使用动态开辟的数组存储。 

    1. //动态顺序表
    2. typedef int SLDateType;
    3. typedef struct SeqList
    4. {
    5. SLDateType* array; //指向动态开辟的数组
    6. size_t size; //数据中存储的数据
    7. size_t capacity; //数组的容量
    8. }SeqList;

     

    建议用动态顺序表,比起静态顺序表,动态的更加好调整顺序表的大小。接下来,我也会以动态顺序表为例,介绍如何实现动态顺序表的增删查改。 

     2、接口实现

    2.1功能要求

    1. #pragma once
    2. #include
    3. #include
    4. //对顺序表的初始化
    5. void SeqListInit(SL* ps);
    6. //对顺序表的销毁
    7. void SeqListDestroy(SL* ps);
    8. //对顺序表的打印
    9. void SeqListPrint(SL* ps);
    10. //对顺序表的尾插入
    11. void SeqListPushBack(SL* ps, SLDataType x);
    12. //对顺序表的头插入
    13. void SeqListPushFront(SL* ps, SLDataType x);
    14. //对顺序表头删除
    15. void SeqListPopFront(SL* ps);
    16. //对顺序表的尾删除
    17. void SeqListPopBack(SL* ps);
    18. // 顺序表查找
    19. //int SeqListFind(SeqList* ps, SLDateType x);
    20. // 顺序表在pos位置插入x
    21. void SeqListInsert(SL* ps, int pos, SLDataType x);
    22. // 顺序表删除pos位置的值
    23. void SeqListErase(SL* ps, int pos);

    2.2功能实现 

    💡初始化顺序表

    没啥好说的,在【C语言】系列都讲过了,3 2 1 上链接

    1. //初始化
    2. void SeqListInit(SL* ps)
    3. {
    4. assert(ps);
    5. ps->arry = NULL;
    6. ps->size = 0;
    7. ps->capacity = 0;
    8. }
     💡销毁顺序表
    1. //销毁
    2. void SeqListDestroy(SL* ps)
    3. {
    4. assert(ps);
    5. if (ps->arry != NULL)
    6. {
    7. free(ps->arry);
    8. ps->arry = NULL;
    9. ps->size = 0;
    10. ps->capacity = 0;
    11. }
    12. }
     💡顺序表尾插入

    这里需要检查是否需要扩容,我们还需要提前封装一个函数

    1. //对顺序表的尾插入
    2. void SeqListPushBack(SL* ps, SLDataType x)
    3. {
    4. assert(ps);
    5. //检查是否需要扩容
    6. CheckCapacity(ps);
    7. ps->arry[ps->size] = x;
    8. ps->size++;
    9. }
    💡检查是否扩容 

    在这里需要注意的就是,我们初始化的时候,capacity是0,如果用常规*2方式扩容,0*2还是0;

    所以这里可以用上一个三目操作符来避免;realloc可以对空指针进行开辟空间,相当于malloc

    1. //检查是否需要扩容
    2. void CheckCapacity(SL* ps)
    3. {
    4. assert(ps);
    5. if (ps->size == ps->capacity) //需要扩容
    6. {
    7. int new =ps->capacity == 0 ? 4 :ps->capacity * 2;
    8. SLDataType* tmp = (SLDataType*)realloc(ps->arry, sizeof(SLDataType)*new);
    9. //检查是否扩容成功
    10. if (tmp == NULL)
    11. {
    12. perror("realloc");
    13. return;
    14. }
    15. ps->arry = tmp;
    16. ps->capacity = new;
    17. printf("扩容成功\n");
    18. }
    19. }
    💡打印顺序表 
    1. //打印顺序表
    2. void SeqListPrint(SL* ps)
    3. {
    4. assert(ps);
    5. int i = 0;
    6. for (i = 0; i < ps->size; i++)
    7. {
    8. printf("%d ", ps->arry[i]);
    9. }
    10. printf("\n");
    11. }
     💡顺序表头插入

    不管是头插入还是尾插入以及后面的任意位置插入,都需要检查是否需要扩容,

    头插入就相当于先将数据往右边移动,头位置空出来,然后将新数据插入即可

    1. //头插入
    2. void SeqListPushFront(SL* ps, SLDataType x)
    3. {
    4. assert(ps);
    5. CheckCapacity(ps);
    6. int begin = ps->size;
    7. while (begin>0)
    8. {
    9. ps->arry[begin] = ps->arry[begin - 1];
    10. begin--;
    11. }
    12. ps->arry[0] = x;
    13. ps->size++;
    14. }
    💡顺序表头删除

     因为顺序表的逻辑结构和物理结构一致,数据前后紧密相连的,所以可以直接将数据往前覆盖

    1. //头删除
    2. void SeqListPopFront(SL* ps)
    3. {
    4. assert(ps);
    5. int begin = 1;
    6. while (begin > 0 && begin < ps->size )
    7. {
    8. ps->arry[begin - 1] = ps->arry[begin];
    9. begin++;
    10. }
    11. ps->size--;
    12. }
    💡顺序表尾删除

    需要注意的就说size跑到负数去,我们采取“七匹狼式警告”,直接“竹条炒肉”

    1. //尾删除
    2. void SeqListPopBack(SL* ps)
    3. {
    4. assert(ps);
    5. //size检查 防止越界
    6. assert(ps->size > 0);
    7. ps->size--;
    8. }
    💡顺序表pos位置插入值

    在容量内任意位置插入,将该位置后的数据往后移,将新元素赋值

    1. // 顺序表在pos位置插入x
    2. void SeqListInsert(SL* ps, int pos, SLDataType x)
    3. {
    4. assert(ps);
    5. assert(pos >= 0 && pos <= ps->size);
    6. CheckCapacity(ps);
    7. int begin = ps->size;
    8. while (begin > pos)
    9. {
    10. ps->arry[begin] = ps->arry[begin - 1];
    11. begin--;
    12. }
    13. ps->arry[pos] = x;
    14. ps->size++;
    15. }
     💡顺序表删除pos的位置
    1. // 顺序表删除pos位置的值
    2. void SeqListErase(SL* ps, int pos)
    3. {
    4. assert(ps);
    5. assert(pos >= 0 && pos <= ps->size);
    6. int begin = pos;
    7. while (begin < ps->size)
    8. {
    9. ps->arry[begin] = ps->arry[begin + 1];
    10. begin++;
    11. }
    12. ps->size--;
    13. }

     总代码

    注意,我们完成每个功能实现,最好都单独测试一下,不要留到最后,不然就会这样

    test.c

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include"SeqList.h"
    3. //11-4ݽṹ
    4. #include<stdio.h>
    5. void test1()
    6. {
    7. SL s1;
    8. SeqListInit(&s1);
    9. SeqListPushBack(&s1,1);
    10. SeqListPrint(&s1);
    11. SeqListDestroy(&s1);
    12. }
    13. void test2()
    14. {
    15. SL s2;
    16. SeqListInit(&s2);
    17. SeqListPushBack(&s2, 1);
    18. SeqListPushBack(&s2, 2);
    19. SeqListPushBack(&s2, 3);
    20. SeqListPushBack(&s2, 4);
    21. SeqListPushBack(&s2, 5);
    22. SeqListPushFront(&s2,10);
    23. SeqListPrint(&s2);
    24. }
    25. //void test3()
    26. //{
    27. // SL s3;
    28. // SeqListInit(&s3);
    29. // SeqListPushBack(&s3, 1);
    30. // SeqListPushBack(&s3, 2);
    31. // SeqListPushBack(&s3, 3);
    32. // SeqListPushBack(&s3, 4);
    33. // SeqListPushBack(&s3, 5);
    34. // SeqListPrint(&s3);
    35. //
    36. // SeqListPopFront(&s3);
    37. // SeqListPrint(&s3);
    38. //}
    39. void test4()
    40. {
    41. SL s4;
    42. SeqListInit(&s4);
    43. SeqListPushBack(&s4, 1);
    44. SeqListPushBack(&s4, 2);
    45. SeqListPushBack(&s4, 3);
    46. SeqListPushBack(&s4, 4);
    47. SeqListPushBack(&s4, 5);
    48. SeqListPrint(&s4);
    49. SeqListInsert(&s4, 2, 66);
    50. SeqListPrint(&s4);
    51. }
    52. void test5()
    53. {
    54. SL s5;
    55. SeqListInit(&s5);
    56. SeqListPushBack(&s5, 1);
    57. SeqListPushBack(&s5, 2);
    58. SeqListPushBack(&s5, 3);
    59. SeqListPushBack(&s5, 4);
    60. SeqListPushBack(&s5, 5);
    61. SeqListPrint(&s5);
    62. SeqListErase(&s5, 2);
    63. SeqListPrint(&s5);
    64. }
    65. int main()
    66. {
    67. //test1();
    68. //test2();
    69. //test3();
    70. //test4();
    71. test5();
    72. return 0;
    73. }

     SeqList.c

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

     SeqList.h

    1. #pragma once
    2. #include
    3. #include
    4. typedef int SLDataType;
    5. typedef struct SeqList
    6. {
    7. SLDataType* arry; //动态开辟的数组
    8. int size; //记录个数
    9. int capacity; //记录容量
    10. }SL;
    11. //对顺序表的初始化
    12. void SeqListInit(SL* ps);
    13. //对顺序表的销毁
    14. void SeqListDestroy(SL* ps);
    15. //对顺序表的打印
    16. void SeqListPrint(SL* ps);
    17. //对顺序表的尾插入
    18. void SeqListPushBack(SL* ps, SLDataType x);
    19. //对顺序表的头插入
    20. void SeqListPushFront(SL* ps, SLDataType x);
    21. //对顺序表头删除
    22. void SeqListPopFront(SL* ps);
    23. //对顺序表的尾删除
    24. void SeqListPopBack(SL* ps);
    25. // 顺序表查找
    26. //int SeqListFind(SeqList* ps, SLDateType x);
    27. // 顺序表在pos位置插入x
    28. void SeqListInsert(SL* ps, int pos, SLDataType x);
    29. // 顺序表删除pos位置的值
    30. void SeqListErase(SL* ps, int pos);

     

  • 相关阅读:
    电子信息工程专业课复习知识点总结:(四)信号与系统、数字信号处理
    9月12日OpenCV学习笔记——基于 Dlib 库的人脸检测
    cx3588 Recovery HDMI 没显示
    Android 9 固定以太网MAC地址(cmdline)
    ROS 学习 Gazebo仿真
    [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945
    功率放大器低功率射频放大器导轨式0-10V转4-20mADC24V隔离变送器
    java毕业生设计爱心公益网站设计与制作计算机源码+系统+mysql+调试部署+lw
    18.2.1 创建分区表
    k8s-NFS系统配置
  • 原文地址:https://blog.csdn.net/m0_67367079/article/details/134252556