• 【数据结构初阶】顺序表


    各位读者老爷好,又见面了哈!鼠鼠我呀现在基于C语言浅浅介绍一下数据结构初阶中的顺序表,希望对你有所帮助!

    目录

    1.线性表 

    2.顺序表

    2.1概念即结构

    2.2动态顺序表接口的实现

    2.2.1定义顺序表

    2.2.2初始化

    2.2.3销毁 

    2.2.4打印

    2.2.5尾插

    2.2.6头插 

    2.2.7头删

    2.2.8尾删 

    2.2.9顺序表查找

     2.2.10顺序表在下标pos位置插入数据x

    2.2.11 顺序表删除下标pos位置的值

    3.顺序表使用 

    3.1test.c

    3.2SeqList.h

    3.3SeqList.c

    4.小知识累积(与顺序表无关)

    4.1数组越界一定会报错吗?

    4.2数组的下标为什么不从1开始而要从0开始呢? 

    5.ending

    额额额,好哈!顺序表是线性表的一种哈!那我们看看下面是线性表!

    1.线性表 

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

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

    咱们下面介绍的顺序表是一种在逻辑上和物理结构上是连续的。

    2.顺序表

    2.1概念即结构

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

    与数组不同的是,数组可以在不越界的情况下任意位置存储数据,而顺序表要求数据只能从头开始连续存储。

     顺序表一般可以分为:

    1.静态顺序表:使用定长数组存储数据

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

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

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

    2.2动态顺序表接口的实现

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

    2.2.1定义顺序表
    1. typedef int SLDateType;
    2. typedef struct SeqList
    3. {
    4. SLDateType* a;
    5. int size; //有效数据
    6. int capaticy; //空间容量
    7. }SeqList;

    根据上面定义的顺序表,咱们创建一个顺序表s1,并实现该顺序表一系列接口实现。

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

    咱们使用顺序表之前需要将有效数据size和空间容量capaticy置零,同时不妨将指向动态开辟数组的指针a置为NULL。

    ps:这个函数的参数接收的是上面创建的顺序表s1的地址。

    question:实现这个初始化函数参数为啥是s1的地址而不是s1呢?

    answer:因为形参是实参的一份临时拷贝,形参的改变不会影响实参。就以这个初始化函数为例,如果这个函数参数是"SeqList s2"的话 ,虽然这个函数中将s2的成员a置为NULL、将s2的成员size和capaticy都置为零,但根本影响不了顺序表s1的成员a、size和capaticy。

    就是因为这个原因,下面接口的实现都是采用传递s1的地址。

    2.2.3销毁 
    1. void SeqListDestroy(SeqList*ps) //销毁
    2. {
    3. assert(ps);
    4. if (ps->a != NULL)
    5. {
    6. ps->a = NULL;
    7. ps->capaticy = 0;
    8. ps->size = 0;
    9. }
    10. }

    咱们如果不再使用顺序表的话,因为顺序表的成员a指向动态开辟的数组,所以最好将这块空间free掉,size和capaticy也最好置零。

     ps:这个函数的参数接收的是上面创建的顺序表s1的地址。

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

    必要时可以将存储在顺序表的数据打印出来看看,因为有size个数据,所以循环打印size次即可将数据全部打印出来。

     ps:这个函数的参数接收的是上面创建的顺序表s1的地址。

    2.2.5尾插
    1. void SLCheckcapacity(SeqList* ps) //扩容
    2. {
    3. assert(ps);
    4. if (ps->size == ps->capaticy)
    5. {
    6. int newcapaticy = (ps->capaticy == 0) ? 4 : 2 * (ps->capaticy);
    7. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapaticy);
    8. if (tmp == NULL)
    9. {
    10. perror("calloc fail");
    11. return;
    12. }
    13. ps->a = tmp;
    14. ps->capaticy = newcapaticy;
    15. }
    16. }
    17. void SeqListPushBack(SeqList* ps, SLDateType x) //尾插
    18. {
    19. assert(ps);
    20. SLCheckcapacity(ps);
    21. ps->a[ps->size] = x;
    22. ps->size += 1;
    23. }

    在顺序表的末尾(下标为size处)处插入数据时直接插入,size加一即可。但要考虑顺序表容量空间是否足够,所以要调用扩容函数SLCheckcapacity,扩容函数的实现大致时当顺序表的size和capaticy一样时,调用realloc函数二倍扩容。 

    ps:尾插函数的参数接收的是上面创建的顺序表s1的地址。

    2.2.6头插 
    1. void SLCheckcapacity(SeqList* ps) //扩容
    2. {
    3. assert(ps);
    4. if (ps->size == ps->capaticy)
    5. {
    6. int newcapaticy = (ps->capaticy == 0) ? 4 : 2 * (ps->capaticy);
    7. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapaticy);
    8. if (tmp == NULL)
    9. {
    10. perror("calloc fail");
    11. return;
    12. }
    13. ps->a = tmp;
    14. ps->capaticy = newcapaticy;
    15. }
    16. }
    17. void SeqListPushFront(SeqList* ps, SLDateType x) //头插
    18. {
    19. assert(ps);
    20. SLCheckcapacity(ps);
    21. int end = ps->size - 1;
    22. while (end >= 0)
    23. {
    24. ps->a[end + 1] = ps->a[end];
    25. end--;
    26. }
    27. ps->a[0] = x;
    28. ps->size++;
    29. }

    顺序表头部插入数据就是将第二个及以后的数据均后移,在将所需头插数据x插入到顺序表头部,size加一即可,同样要考虑容量问题。

     ps:头插函数的第一个参数接收的是上面创建的顺序表s1的地址,第二个参数是所需头插数据。

    2.2.7头删
    1. void SeqListPopFront(SeqList* ps) //头删
    2. {
    3. assert(ps);
    4. assert(ps->size > 0);
    5. int begin = 0;
    6. while (beginsize-1)
    7. {
    8. ps->a[begin] = ps->a[begin+1];
    9. begin++;
    10. }
    11. ps->size--;
    12. }

    顺序表删除头部数据也很简单,将第二个及以后的数据均向前覆盖,在将size减一即可。但是需要注意的是,如果size为零的话说明没有数据就不要再删了,用assert断言一下就行。

      ps:这个函数的参数接收的是上面创建的顺序表s1的地址。

    2.2.8尾删 
    1. void SeqListPopBack(SeqList* ps) //尾删
    2. {
    3. assert(ps);
    4. assert(ps->size >0);
    5. ps->size--;
    6. }

    顺序表尾部删除数据最简单,直接将size减一就行,但和头删一样,用assert断言一下防止删空了。

      ps:这个函数的参数接收的是上面创建的顺序表s1的地址。

    2.2.9顺序表查找
    1. int SeqListFind(SeqList* ps, SLDateType x) //顺序表查找
    2. {
    3. assert(ps);
    4. int i = 0;
    5. for (i = 0; i < ps->size; i++)
    6. {
    7. if (x == ps->a[i])
    8. {
    9. return i;//找得到返回下标
    10. }
    11. }
    12. return -1;//找不到返回-1
    13. }

    我们也许需要在顺序表中查找某个数据,所以遍历这个顺序表的数据即可,找到返回该数据下标,找不到返回-1。

     ps:该函数的第一个参数接收的是上面创建的顺序表s1的地址,第二个参数是所需查找的数据。

     2.2.10顺序表在下标pos位置插入数据x
    1. void SLCheckcapacity(SeqList* ps) //扩容
    2. {
    3. assert(ps);
    4. if (ps->size == ps->capaticy)
    5. {
    6. int newcapaticy = (ps->capaticy == 0) ? 4 : 2 * (ps->capaticy);
    7. SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapaticy);
    8. if (tmp == NULL)
    9. {
    10. perror("calloc fail");
    11. return;
    12. }
    13. ps->a = tmp;
    14. ps->capaticy = newcapaticy;
    15. }
    16. }
    17. void SeqListInsert(SeqList* ps, int pos, SLDateType x) //顺序表在下标pos位置插入x
    18. {
    19. assert(ps);
    20. assert(pos >= 0 && pos <= ps->size);
    21. SLCheckcapacity(ps);
    22. int end = ps->size - 1;
    23. while (end >= pos)
    24. {
    25. ps->a[end + 1] = ps->a[end];
    26. end--;
    27. }
    28. ps->a[pos] = x;
    29. ps->size++;
    30. }

    这个接口的实现大致是将下标pos以后的数据都往后挪,在将所需插入的数据x插入到下标pos的位置,在将size加一即可。但有几处细节要注意:1.pos只能在0和size之间(包括0和size), 否则就越界访问了,所以利用assert断言一下;2.防止顺序表容量不足,调用扩容函数。

    ps:该函数的第一个参数接收的是上面创建的顺序表s1的地址,第二个参数是插入位置的下标,第三个参数是需插入的数据。

    2.2.11 顺序表删除下标pos位置的值
    1. void SeqListErase(SeqList* ps, int pos) // 顺序表删除下标pos位置的值
    2. {
    3. assert(ps);
    4. assert(pos >= 0 && pos< ps->size);
    5. int begin = pos + 1;
    6. while (begin <= ps->size - 1)
    7. {
    8. ps->a[begin - 1] = ps->a[begin];
    9. begin++;
    10. }
    11. ps->size--;
    12. }

    这个接口实现大致就是将下标pos以后的数据均向前覆盖,size减一即可,若需删除最后一个(下标为size-1)数据 ,无需覆盖,直接size减一即可。细节:下标pos只能在0到size之间(包括0),否则会越界访问,利用assert断言。

    ps:该函数第一个参数接收的是上面创建的顺序表s1的地址,第二个参数是需删除数据的下标。

    3.顺序表使用 

    咱们顺序表接口就实现完了。可以写一个接口来对接上面的所以接口,以实现顺序表的增删查改及初始化、销毁、打印等功能。鼠鼠我写了一个,可以参考参考哈:

    3.1test.c

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include"SeqList.h"
    3. void menu()
    4. {
    5. printf("**********************\n");
    6. printf("********0.退出********\n");
    7. printf("****1.头插 2.头删****\n");
    8. printf("****3.尾插 4.尾删****\n");
    9. printf("****5.查找 6.打印****\n");
    10. printf("*7.在下标pos位置插入值\n");
    11. printf("*8.删除下标pos位置的值\n");
    12. printf("**********************\n");
    13. }
    14. int main()
    15. {
    16. SeqList s1;
    17. SeqListInit(&s1);
    18. int input;
    19. do
    20. {
    21. menu();
    22. printf("请输入你想操作的数字:->");
    23. scanf("%d", &input);
    24. if (input == 0)
    25. {
    26. SeqListDestroy(&s1);
    27. printf("\n");
    28. break;
    29. }
    30. else if (input == 1)
    31. {
    32. int number = 0;
    33. printf("请输入你要头插数据的个数:->");
    34. scanf("%d", &number);
    35. printf("请输入你要头插的数据:->");
    36. while (number--)
    37. {
    38. SLDateType x = 0;
    39. scanf("%d", &x);
    40. SeqListPushFront(&s1, x);
    41. }
    42. printf("\n");
    43. }
    44. else if (input == 2)
    45. {
    46. SeqListPopFront(&s1);
    47. printf("\n");
    48. }
    49. else if (input == 3)
    50. {
    51. int number = 0;
    52. printf("请输入你要尾插数据的个数:->");
    53. scanf("%d", &number);
    54. printf("请输入你要尾插的数据:->");
    55. int i = 0;
    56. for (i = 0; i < number; i++)
    57. {
    58. SLDateType x = 0;
    59. scanf("%d", &x);
    60. SeqListPushBack(&s1, x);
    61. }
    62. printf("\n");
    63. }
    64. else if (input == 4)
    65. {
    66. SeqListPopBack(&s1);
    67. printf("\n");
    68. }
    69. else if (input == 5)
    70. {
    71. SLDateType x = 0;
    72. printf("请输入你要查找的值:->");
    73. scanf("%d", &x);
    74. int flag=SeqListFind(&s1, x);
    75. if (flag!=-1)
    76. {
    77. printf("你要查找的值下标是%d\n", flag);
    78. }
    79. else
    80. {
    81. printf("找不到!\n");
    82. }
    83. printf("\n");
    84. }
    85. else if (input == 6)
    86. {
    87. SeqListPrint(&s1);
    88. printf("\n");
    89. }
    90. else if (input == 7)
    91. {
    92. SLDateType x = 0; int pos = 0;
    93. printf("请分别输入你要插入的值及插入的下标:->");
    94. scanf("%d %d", &x, &pos);
    95. SeqListInsert(&s1,pos,x);
    96. printf("\n");
    97. }
    98. else if (input == 8)
    99. {
    100. int pos = 0;
    101. printf("请输入你要删除值的下标:->");
    102. scanf("%d", &pos);
    103. SeqListErase(&s1,pos);
    104. printf("\n");
    105. }
    106. else
    107. {
    108. printf("输入错误,请重新输入:->");
    109. }
    110. } while (input);
    111. return 0;
    112. }

    3.2SeqList.h

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. typedef int SLDateType;
    6. typedef struct SeqList
    7. {
    8. SLDateType* a;
    9. int size; //有效数据
    10. int capaticy; //空间容量
    11. }SeqList;
    12. void SeqListInit(SeqList* ps); //初始化
    13. void SeqListDestroy(SeqList* ps); //销毁
    14. void SeqListPrint(SeqList* ps); //打印
    15. void SeqListPushBack(SeqList* ps, SLDateType x); //尾插
    16. void SeqListPushFront(SeqList* ps, SLDateType x); //头插
    17. void SeqListPopFront(SeqList* ps); //头删
    18. void SeqListPopBack(SeqList* ps); //尾删
    19. int SeqListFind(SeqList* ps, SLDateType x); //顺序表查找
    20. void SeqListInsert(SeqList* ps, int pos, SLDateType x); // 顺序表在下标pos位置插入x
    21. void SeqListErase(SeqList* ps, int pos); // 顺序表删除下标pos位置的值

    3.3SeqList.c

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

     各位读者老爷可以将这三个文件放到一个工程下玩玩哦!

    4.小知识累积(与顺序表无关)

    4.1数组越界一定会报错吗?

    answer:越界读基本不会报错。越界写可能会报错。越界的检查是一种抽查行为,就像查酒驾一样,比如数组通常会在数组后面设置一些标记位,一旦标记位的 值被更改就会报错,所以一般在数组末尾附近越界写会报错,但越界太远写就基本不报错了(跳过了标记位)。当然不同的编译器对越界的检查不同。这里只是对越界报错的一种认知。

    4.2数组的下标为什么不从1开始而要从0开始呢? 

    因为通过下标访问数组本质是指针访问,数组下标从0开始是要和指针的设计自恰!

    a[i]等价于*(a+i),只有当下标从0开始时,当i=0时,a[0]=*(a+0)才解释得通。

    5.ending

    鼠鼠我才疏学浅,且时间紧迫,如有不足,恳请斧正,谢谢哈哈哈!

    懂我意思吧?

  • 相关阅读:
    SpringBoot实现定时任务
    20231009比赛总结
    前端周刊第三十三期
    mysql用户管理(sql语句)
    黑马瑞吉外卖之删除分类
    线性方程组求解的迭代方法&Python实现
    “顾客第一”的理念在地方政府仍然适用
    【JavaWeb】【瑞吉外卖】分页操作&数据传输转换
    从零开始带你编写属于自己的 Starter
    【GNN基础学习】图模块基本定义 || 图的邻接矩阵 || GNN中常见任务有哪些? || GNN消息传递方法 || 多层GCN有什么作用?
  • 原文地址:https://blog.csdn.net/X__cheng/article/details/134363152