• C语言描述数据结构 —— 顺序表


    1. 线性表

    线性表是 n 个具有相同特征的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串……

    线性表在逻辑上是线性结构,也就是说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

    2. 顺序表

    2.1 概念及结构

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

    顺序表一般可分为: 

    • 静态顺序表:使用定长数组存储元素。
    • 动态顺序表:使用动态开辟的数组存储。

    需要注意,顺序表的元素的连续存放的。

    2.2 顺序表各接口

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

    那么我们将要实现的接口如下:

    1. // 基本增删查改接口
    2. // 顺序表初始化
    3. void SeqListInit(SeqList * psl);
    4. // 检查空间,如果满了,进行增容
    5. void CheckCapacity(SeqList* psl);
    6. // 顺序表尾插
    7. void SeqListPushBack(SeqList* psl, SLDataType x);
    8. // 顺序表尾删
    9. void SeqListPopBack(SeqList* psl);
    10. // 顺序表头插
    11. void SeqListPushFront(SeqList* psl, SLDataType x);
    12. // 顺序表头删
    13. void SeqListPopFront(SeqList* psl);
    14. // 顺序表查找
    15. int SeqListFind(SeqList* psl, SLDataType x);
    16. // 顺序表在pos位置插入x
    17. void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
    18. // 顺序表删除pos位置的值
    19. void SeqListErase(SeqList* psl, size_t pos);
    20. // 顺序表销毁
    21. void SeqListDestory(SeqList* psl);
    22. // 顺序表打印
    23. void SeqListPrint(SeqList* psl);


    2.3 顺序表各接口实现

    注意,以下所有的代码都是建立在多文件操作上,头文件的定义、声明以及源文件具体写什么,这里就不做介绍了。相信各位朋友有分辨代码写在哪个文件下的基本能力。

    动态顺序表的声明:

    1. //静态顺序表
    2. //#define N 10
    3. //typedef int SLDataType;
    4. //typedef struct SeqList
    5. //{
    6. // SLDataType a[N];
    7. // int size;
    8. //}SL;
    9. //动态顺序表
    10. typedef int SLDataType;
    11. typedef struct SeqList
    12. {
    13. SLDataType* a;
    14. int size;//元素个数
    15. int capacity;//容量
    16. }SL;

    我们注意到一条语句,typedef int SLDataType 。这是为了通用多种类型数据而设计的,我们只需要在此语句上修改类型,那么顺序表的元素就不止局限于整形了。

    顺序表初始化:

    1. //顺序表初始化
    2. void SeqListInit(SeqList* psl)
    3. {
    4. assert(psl);
    5. psl->a = NULL;
    6. psl->size = 0;
    7. psl->capacity = 0;
    8. }

    需要注意的是,在进行结构体传参时,在函数内部改变外部内容时,都使用传址调用,并断言确保程序安全性。

     顺序表尾插:

    1. //检查容量、增容
    2. static void CheckCapacity(SeqList* psl)
    3. {
    4. assert(psl);
    5. if (psl->size == psl->capacity)
    6. {
    7. int Newcapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
    8. SLDataType* tmp = (SLDataType*)realloc(psl->a, Newcapacity*sizeof(SLDataType));
    9. if (tmp == NULL)
    10. {
    11. perror("realloc");
    12. exit(-1);
    13. }
    14. psl->a = tmp;
    15. psl->capacity = Newcapacity;
    16. }
    17. }
    18. //顺序表尾插
    19. void SeqListPushBack(SeqList* psl, SLDataType x)
    20. {
    21. assert(psl);
    22. CheckCapacity(psl);
    23. psl->a[psl->size] = x;
    24. psl->size++;
    25. }

    注意,检查容量、增容函数不需要被其他文件使用,所以用 stacit 修饰。另外,对于增容为什么要增容原来的 2 倍,这个就是效率空间利用率的问题了,大家自行脑补。

    顺序表打印:

    1. //打印
    2. void SeqListPrint(SeqList psl)
    3. {
    4. for (int i = 0; i < psl.size; i++)
    5. {
    6. printf("%d ", psl.a[i]);
    7. }
    8. }

    打印是不需要改变外部内容的,所以使用传值调用即可。

    测试:顺序表尾插和打印 

    1. void Test()
    2. {
    3. SeqList s;
    4. SeqListInit(&s);
    5. SeqListPushBack(&s, 1);
    6. SeqListPushBack(&s, 2);
    7. SeqListPushBack(&s, 3);
    8. SeqListPushBack(&s, 4);
    9. SeqListPrint(s);
    10. }
    11. int main()
    12. {
    13. Test();
    14. return 0;
    15. }

    顺序表的尾插、打印功能正常。 

    顺序表头插: 

    1. //头插
    2. void SeqListPushFront(SeqList* psl, SLDataType x)
    3. {
    4. assert(psl);
    5. CheckCapacity(psl);
    6. int end = psl->size - 1;
    7. while (end >= 0)
    8. {
    9. psl->a[end + 1] = psl->a[end];
    10. end--;
    11. }
    12. psl->a[0] = x;
    13. psl->size++;
    14. }

    注意我们插入数据我们都需要检查容量是否合适。那么头插的原理为:

     

    测试:顺序表头插和打印

    1. void Test()
    2. {
    3. SeqList s;
    4. SeqListInit(&s);
    5. SeqListPushBack(&s, 1);
    6. SeqListPushBack(&s, 2);
    7. SeqListPushBack(&s, 3);
    8. SeqListPushBack(&s, 4);
    9. SeqListPrint(s);
    10. SeqListPushFront(&s, 40);
    11. SeqListPushFront(&s, 30);
    12. SeqListPushFront(&s, 20);
    13. SeqListPushFront(&s, 10);
    14. SeqListPrint(s);
    15. }
    16. int main()
    17. {
    18. Test();
    19. return 0;
    20. }

    顺序表尾删:

    1. //尾删
    2. void SeqListPopBack(SeqList* psl)
    3. {
    4. assert(psl);
    5. assert(psl->size > 0);//保证顺序表有数据
    6. psl->size--;
    7. }

    对于尾删来说,我们只要做到访问不到最后一个元素即可。那么我们能够释放掉某些空间吗?不能,因为在动态开辟内存的章节中,我们分析过动态内存的常见错误,free 不能释放动态开辟的部分空间。但是,我们可以发现 realloc 函数的一个"bug" 。这个"bug" 就是我们追加的空间是以前的 1/2 。这个从某种角度来说,确实是节省空间了,但是我们要考虑 realloc 的特性,它在堆上开辟的空间不一定是在原空间上追加的,也有可能是重新开辟了一块空间,如果是重新开辟了一块空间的话,那么就得进行数据的拷贝的原空间的释放,那么运行的时间就会增多,导致效率下降。所以我们做的处理是:删除数据不进行空间释放和压缩,从而达到以空间换时间的目的。

    测试:顺序表尾删和打印 

    1. void Test()
    2. {
    3. SeqList s;
    4. SeqListInit(&s);
    5. SeqListPushBack(&s, 1);
    6. SeqListPushBack(&s, 2);
    7. SeqListPushBack(&s, 3);
    8. SeqListPushBack(&s, 4);
    9. SeqListPrint(s);
    10. SeqListPushFront(&s, 40);
    11. SeqListPushFront(&s, 30);
    12. SeqListPushFront(&s, 20);
    13. SeqListPushFront(&s, 10);
    14. SeqListPrint(s);
    15. SeqListPopBack(&s);
    16. SeqListPopBack(&s);
    17. SeqListPrint(s);
    18. }
    19. int main()
    20. {
    21. Test();
    22. return 0;
    23. }

    顺序表头删: 

    1. //头删
    2. void SeqListPopFront(SeqList* psl)
    3. {
    4. assert(psl);
    5. assert(psl->size > 0);//保证顺序表有数据
    6. int end = 0;
    7. while (end < psl->size - 1)
    8. {
    9. psl->a[end] = psl->a[end + 1];
    10. end++;
    11. }
    12. psl->size--;
    13. }

    头删的原理也很简单,只需把第一个元素覆盖掉就行了。

    测试:顺序表头删和打印 

    1. void Test()
    2. {
    3. SeqList s;
    4. SeqListInit(&s);
    5. SeqListPushBack(&s, 1);
    6. SeqListPushBack(&s, 2);
    7. SeqListPushBack(&s, 3);
    8. SeqListPushBack(&s, 4);
    9. SeqListPrint(s);
    10. SeqListPushFront(&s, 40);
    11. SeqListPushFront(&s, 30);
    12. SeqListPushFront(&s, 20);
    13. SeqListPushFront(&s, 10);
    14. SeqListPrint(s);
    15. SeqListPopBack(&s);
    16. SeqListPopBack(&s);
    17. SeqListPrint(s);
    18. SeqListPopFront(&s);
    19. SeqListPopFront(&s);
    20. SeqListPrint(s);
    21. }
    22. int main()
    23. {
    24. Test();
    25. return 0;
    26. }

    顺序表查找: 

    1. //查找
    2. int SeqListFind(SeqList* psl, SLDataType x)
    3. {
    4. assert(psl);
    5. assert(psl->size > 0);//保证顺序表有数据
    6. for (int i = 0; i < psl->size; i++)
    7. {
    8. if (psl->a[i] == x)
    9. return i;
    10. }
    11. return -1;
    12. }

    我们的返回值是返回下标,因为顺序表就是一个数组。而且返回值是下标的话可以搭配任意位置插入使用。

    在 pos 位置插入:

    1. //在 pos 位置插入
    2. void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
    3. {
    4. assert(psl);
    5. if (pos == 0)//当要插入的位置是 0 之前时,直接调用头插
    6. {
    7. SeqListPushFront(psl, x);
    8. return;
    9. }
    10. size_t end = psl->size - 1;
    11. while (end >= pos)
    12. {
    13. psl->a[end + 1] = psl->a[end];
    14. end--;
    15. }
    16. psl->a[pos] = x;
    17. psl->size++;
    18. }

    原理如下:

     

    测试:顺序表查找以及在 pos 位置插入

    1. void Test()
    2. {
    3. SeqList s;
    4. SeqListInit(&s);
    5. SeqListPushBack(&s, 1);
    6. SeqListPushBack(&s, 2);
    7. SeqListPushBack(&s, 3);
    8. SeqListPushBack(&s, 4);
    9. SeqListPrint(s);
    10. SeqListPushFront(&s, 40);
    11. SeqListPushFront(&s, 30);
    12. SeqListPushFront(&s, 20);
    13. SeqListPushFront(&s, 10);
    14. SeqListPrint(s);
    15. SeqListPopBack(&s);
    16. SeqListPopBack(&s);
    17. SeqListPrint(s);
    18. SeqListPopFront(&s);
    19. SeqListPopFront(&s);
    20. SeqListPrint(s);
    21. SeqListInsert(&s, SeqListFind(&s, 30), 300);
    22. SeqListInsert(&s, SeqListFind(&s, 1), 100);
    23. SeqListPrint(s);
    24. }
    25. int main()
    26. {
    27. Test();
    28. return 0;
    29. }

    这个程序完成了在 30 前面插入 300,在 1 前面插入 100 的任务。

     

    在 pos 位置删除数据: 

    1. //在 pos 位置删除数据
    2. void SeqListErase(SeqList* psl, size_t pos)
    3. {
    4. assert(psl);
    5. assert(psl->size > 0);//保证顺序表有数据
    6. size_t del = pos;
    7. while (del <= psl->size-2)
    8. {
    9. psl->a[del] = psl->a[del + 1];
    10. del++;
    11. }
    12. psl->size--;
    13. }

    原理如下:

     

    测试:在 pos 位置删除数据 

    1. void Test()
    2. {
    3. SeqList s;
    4. SeqListInit(&s);
    5. SeqListPushBack(&s, 1);
    6. SeqListPushBack(&s, 2);
    7. SeqListPushBack(&s, 3);
    8. SeqListPushBack(&s, 4);
    9. SeqListPrint(s);
    10. SeqListPushFront(&s, 40);
    11. SeqListPushFront(&s, 30);
    12. SeqListPushFront(&s, 20);
    13. SeqListPushFront(&s, 10);
    14. SeqListPrint(s);
    15. SeqListPopBack(&s);
    16. SeqListPopBack(&s);
    17. SeqListPrint(s);
    18. SeqListPopFront(&s);
    19. SeqListPopFront(&s);
    20. SeqListPrint(s);
    21. SeqListInsert(&s, SeqListFind(&s, 30), 300);
    22. SeqListInsert(&s, SeqListFind(&s, 1), 100);
    23. SeqListPrint(s);
    24. SeqListErase(&s, SeqListFind(&s, 300));
    25. SeqListErase(&s, SeqListFind(&s, 100));
    26. SeqListPrint(s);
    27. }
    28. int main()
    29. {
    30. Test();
    31. return 0;
    32. }

     

    顺序表销毁:

    1. //顺序表销毁
    2. void SeqListDestory(SeqList* psl)
    3. {
    4. assert(psl);
    5. free(psl->a);
    6. psl->a = NULL;
    7. psl->size = 0;
    8. psl->capacity = 0;
    9. }

    3. 顺序表完整代码

    3.1 SeqList.h 头文件

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #pragma once
    3. #include
    4. #include
    5. #include
    6. //静态顺序表
    7. //#define N 10
    8. //typedef int SLDataType;
    9. //typedef struct SeqList
    10. //{
    11. // SLDataType a[N];
    12. // int size;
    13. //}SL;
    14. //动态顺序表
    15. typedef int SLDataType;
    16. typedef struct SeqList
    17. {
    18. SLDataType* a;
    19. int size;//元素个数
    20. int capacity;//容量
    21. }SeqList;
    22. // 基本增删查改接口
    23. // 顺序表初始化
    24. void SeqListInit(SeqList * psl);
    25. // 检查空间,如果满了,进行增容
    26. void CheckCapacity(SeqList* psl);
    27. // 顺序表尾插
    28. void SeqListPushBack(SeqList* psl, SLDataType x);
    29. // 顺序表尾删
    30. void SeqListPopBack(SeqList* psl);
    31. // 顺序表头插
    32. void SeqListPushFront(SeqList* psl, SLDataType x);
    33. // 顺序表头删
    34. void SeqListPopFront(SeqList* psl);
    35. // 顺序表查找
    36. int SeqListFind(SeqList* psl, SLDataType x);
    37. // 顺序表在pos位置插入x
    38. void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
    39. // 顺序表删除pos位置的值
    40. void SeqListErase(SeqList* psl, size_t pos);
    41. // 顺序表销毁
    42. void SeqListDestory(SeqList* psl);
    43. // 顺序表打印
    44. void SeqListPrint(SeqList psl);

     3.2 SeqList.c 源文件

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include "SeqList.h"
    3. //顺序表初始化
    4. void SeqListInit(SeqList* psl)
    5. {
    6. assert(psl);
    7. psl->a = NULL;
    8. psl->size = 0;
    9. psl->capacity = 0;
    10. }
    11. //检查容量、增容
    12. static void CheckCapacity(SeqList* psl)
    13. {
    14. assert(psl);
    15. if (psl->size == psl->capacity)
    16. {
    17. int Newcapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
    18. SLDataType* tmp = (SLDataType*)realloc(psl->a, Newcapacity*sizeof(SLDataType));
    19. if (tmp == NULL)
    20. {
    21. perror("realloc");
    22. exit(-1);
    23. }
    24. psl->a = tmp;
    25. psl->capacity = Newcapacity;
    26. }
    27. }
    28. //顺序表尾插
    29. void SeqListPushBack(SeqList* psl, SLDataType x)
    30. {
    31. assert(psl);
    32. CheckCapacity(psl);
    33. psl->a[psl->size] = x;
    34. psl->size++;
    35. }
    36. //打印
    37. void SeqListPrint(SeqList psl)
    38. {
    39. for (int i = 0; i < psl.size; i++)
    40. {
    41. printf("%d ", psl.a[i]);
    42. }
    43. printf("\n");
    44. }
    45. //头插
    46. void SeqListPushFront(SeqList* psl, SLDataType x)
    47. {
    48. assert(psl);
    49. CheckCapacity(psl);
    50. int end = psl->size - 1;
    51. while (end >= 0)
    52. {
    53. psl->a[end + 1] = psl->a[end];
    54. end--;
    55. }
    56. psl->a[0] = x;
    57. psl->size++;
    58. }
    59. //尾删
    60. void SeqListPopBack(SeqList* psl)
    61. {
    62. assert(psl);
    63. assert(psl->size > 0);//保证顺序表有数据
    64. psl->size--;
    65. }
    66. //头删
    67. void SeqListPopFront(SeqList* psl)
    68. {
    69. assert(psl);
    70. assert(psl->size > 0);//保证顺序表有数据
    71. int end = 0;
    72. while (end < psl->size - 1)
    73. {
    74. psl->a[end] = psl->a[end + 1];
    75. end++;
    76. }
    77. psl->size--;
    78. }
    79. //查找
    80. int SeqListFind(SeqList* psl, SLDataType x)
    81. {
    82. assert(psl);
    83. assert(psl->size > 0);//保证顺序表有数据
    84. for (int i = 0; i < psl->size; i++)
    85. {
    86. if (psl->a[i] == x)
    87. return i;
    88. }
    89. return -1;
    90. }
    91. //在 pos 位置插入
    92. void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
    93. {
    94. assert(psl);
    95. if (pos == 0)//当要插入的位置是 0 之前时,直接调用头插
    96. {
    97. SeqListPushFront(psl, x);
    98. return;
    99. }
    100. size_t end = psl->size - 1;
    101. while (end >= pos)
    102. {
    103. psl->a[end + 1] = psl->a[end];
    104. end--;
    105. }
    106. psl->a[pos] = x;
    107. psl->size++;
    108. }
    109. //在 pos 位置删除数据
    110. void SeqListErase(SeqList* psl, size_t pos)
    111. {
    112. assert(psl);
    113. assert(psl->size > 0);//保证顺序表有数据
    114. size_t del = pos;
    115. while (del <= psl->size-2)
    116. {
    117. psl->a[del] = psl->a[del + 1];
    118. del++;
    119. }
    120. psl->size--;
    121. }
    122. //顺序表销毁
    123. void SeqListDestory(SeqList* psl)
    124. {
    125. assert(psl);
    126. free(psl->a);
    127. psl->a = NULL;
    128. psl->size = 0;
    129. psl->capacity = 0;
    130. }

    3.3 test.c 源文件

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include "SeqList.h"
    3. void Test()
    4. {
    5. SeqList s;
    6. SeqListInit(&s);
    7. SeqListPushBack(&s, 1);
    8. SeqListPushBack(&s, 2);
    9. SeqListPushBack(&s, 3);
    10. SeqListPushBack(&s, 4);
    11. SeqListPrint(s);
    12. SeqListPushFront(&s, 40);
    13. SeqListPushFront(&s, 30);
    14. SeqListPushFront(&s, 20);
    15. SeqListPushFront(&s, 10);
    16. SeqListPrint(s);
    17. SeqListPopBack(&s);
    18. SeqListPopBack(&s);
    19. SeqListPrint(s);
    20. SeqListPopFront(&s);
    21. SeqListPopFront(&s);
    22. SeqListPrint(s);
    23. SeqListInsert(&s, SeqListFind(&s, 30), 300);
    24. SeqListInsert(&s, SeqListFind(&s, 1), 100);
    25. SeqListPrint(s);
    26. SeqListErase(&s, SeqListFind(&s, 300));
    27. SeqListErase(&s, SeqListFind(&s, 100));
    28. SeqListPrint(s);
    29. }
    30. int main()
    31. {
    32. Test();
    33. return 0;
    34. }

  • 相关阅读:
    三维量子成像雷达
    俄罗斯套娃 (Matryoshka) 嵌入模型概述
    用户的生命周期
    阿里云全站加速 DCDN 重磅发布!打造新一代加速引擎
    Feign 实现 GET 方法传递 POJO
    【测试工具】UnixBench 测试
    php实战案例记录(1)删除语句之TRUNCATE TABLE和DELETE FROM
    重装系统后电脑耳机插前面没有声音输出怎么办?
    1979. 找出数组的最大公约数
    前端面试--贡献给刚毕业的你们
  • 原文地址:https://blog.csdn.net/weixin_59913110/article/details/126056490