• 数据结构---顺序表


    线性表

    什么是线性表

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

    线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的(地址不一定是连续的,但是可以通过之间存在的逻辑关系找到下一个数据)线性表在物理上存储时,通常以数组和链式结构的形式存储。

    顺序表

    什么是顺序表

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

    静态顺序表

    静态顺序表指的是使用定长数组存储元素的顺序表,由于在使用之前,顺序表的个数就已经确定,对于需要大量增删查改的项目来说很鸡肋,所以静态顺序表只作为顺序表一个小的知识点一带而过。

    静态顺序表的初始化

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

    动态顺序表

    动态顺序表指的是使用动态开辟的数组存储,顺序表的有效数据个数根据所需要进行修改。

    动态顺序表的初始化

    1. //动态顺序表
    2. typedef int SLDateType;
    3. typedef struct SeqList
    4. {
    5. SLDateType* array; //指向动态开辟的数组
    6. size_t size; //数据中存储的数据
    7. size_t capacity; //数组的容量
    8. }SeqList;
    1. 由于动态顺序表不像静态顺序表那样直接给出存储有效数据的个数,所以没有办法直接写出数组,这是就需要指针进行操作,因为指针就是数据的地址,所以要通过定义指针去找到动态开辟的空间,通过指针的加减去对应相应的数据的增删查改。
    2. 动态开辟要有初始条件,只有满足才能扩容,所以加入数组的容量(capacity),当有效数据个数与数组容量相等时,说明顺序表已满,需要扩容。

    接口实现

    使用数据结构的目的就是为了对数据进行增删查改,那么对于动态顺序表的增删查改,我们使用接口的方式去详细的描述这一过程。

    顺序表的初始化

    对于顺序表的初始化,主要就是针对顺序表的初始地址,有效数据个数以及最大容量进行设定,只有初始化成功才能开始正常的工作。

    1. //顺序表初始化
    2. void SeqListInit(SeqList* psl) //psl p指针,sl顺序表
    3. {
    4. assert(psl);
    5. psl->array = (SLDataType*)malloc(sizeof(SLDataType) * INIT_CAPACITY);
    6. if (psl->array == NULL)
    7. {
    8. perror("fail in malloc");
    9. return;
    10. }
    11. psl->size = 0;
    12. psl->capacity = INIT_CAPACITY;
    13. }
    1. 起始地址可以置为NULL;但是以上代码是使用malloc函数开辟一块空间,目的是为了使得顺序表的最大容量不为0;当然,如果起始地址是NULL;最大容量就要设置为0;这两种方法都可以。
    2. INIT_CAPACITY是在头文件中定义的大小为4的数据;
    3. 如果对malloc函数和realloc函数不是很清楚,可参考动态内存管理icon-default.png?t=N7T8https://blog.csdn.net/Senyu_nuanshu/article/details/131934727

    顺序表的扩容

    动态顺序表要时刻检查最大容量是否和有效数据个数相等,如果相等就说明要进行扩容操作

    1. //检查空间,如果满了,进行增容
    2. void SeqListCheckCapacity(SeqList* psl)
    3. {
    4. assert(psl);
    5. if (psl->size == psl->capacity)
    6. {
    7. SLDataType* newnode = (SLDataType*)realloc(psl->array, sizeof(SLDataType)* 2 * psl->capacity);
    8. if (newnode == NULL)
    9. {
    10. perror("fail in realloc");
    11. return;
    12. }
    13. psl->array = newnode;
    14. psl->capacity *= 2;
    15. }
    16. }
    1. realloc函数分为原地扩容或者异地扩容,这两种扩容方式是根据电脑不同时刻内存的情况选择的,处理扩容问题最正确的办法就是先设定一个新的地址去保存扩容出来的内存,再将新的地址赋值给原来的地址。
    2. 我们要扩容到最大容量的2倍,很多同学会写成   sizeof(SLDataType)* 2   ,这个是4*2,8个字节的大小,但是最大容量是4*4字节,也就是16字节,2倍就是32个字节;所以这么写完就会报错,报错的原因就是扩容扩少了,导致新的元素没有地方存放,所以正确的写法是 sizeof(SLDataType)* 2 * psl->capacity
    3. 扩容成功之后,除了地址要改变以外,顺序表的最大容量也要*2,这样才是正确的扩容。

    顺序表的打印

    打印是一个很好检查自己代码错误以及输出的方法,一定要先写打印,然后用其测试头插头删,尾插尾删。

    1. //打印顺序表
    2. void PrintSL(SeqList* psl)
    3. {
    4. assert(psl);
    5. for (int i = 0; i < psl->size; i++)
    6. {
    7. printf("%d ", psl->array[i]);
    8. }
    9. printf("\n ");
    10. }
    1. assert断言函数是为了防止顺序表为空而加入的,各位同学在写的时候一定要注意,仔细思考一下顺序表为空能不能打印,能不能进行增删查改,答案肯定是不能,顺序表都已经空了,你打印什么,增删查改谁?
    2. 还有就是size-1才是array数组的最后一个下标

    顺序表的尾插

    从图中就可以明白,尾插其实就是赋值给array[size],前提是要检查一下存储空间够不够用

    同时尾插之后要注意,数组存储的有效数据个数已经增加了

    1. //顺序表尾插
    2. void SeqListPushBack(SeqList* psl, SLDataType x)
    3. {
    4. assert(psl);
    5. SeqListCheckCapacity(psl);
    6. psl->array[psl->size] = x;//size是顺序表的最后一个位置
    7. psl->size++;
    8. }

    顺序表的尾删

    对于顺序表的删除,一定要注意,要保证有效数据个数,也就是size>0;如果没有数据你删什么。

    从图中可以看出来,顺序表的尾删是什么,就是把尾巴的数据直接扔掉就行了。

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

     很多小伙伴会质疑,realloc函数开辟出来的空间,不用的时候应该使用free函数将空间的使用权还给操作系统啊,但是请注意,动态内存函数开辟出来的空间,只能一起还给操作系统,不能一个一个free,我们可以在最后使用free函数将所有开辟出来的空间全部还给操作系统。

    顺序表的头插

    1. //顺序表头插
    2. void SeqListPushFront(SeqList* psl, SLDataType x)
    3. {
    4. assert(psl);
    5. SeqListCheckCapacity(psl);
    6. for (int i = psl->size; i > 0; i--)
    7. {
    8. psl->array[i] = psl->array[i - 1];
    9. }
    10. psl->array[0] = x;
    11. psl->size++;
    12. }

    顺序表的头删

    1. //顺序表头删
    2. void SeqListPopFront(SeqList* psl)
    3. {
    4. assert(psl);
    5. assert(psl->size > 0);
    6. for (int i = 0; i < psl->size-1; i++)
    7. {
    8. psl->array[i] = psl->array[i + 1];
    9. }
    10. psl->size--;
    11. }

     顺序表的查找

    1. int SeqListFind(SeqList* psl, SLDataType x)
    2. {
    3. assert(psl);
    4. for (int i = 0; i < psl->size; i++)
    5. {
    6. if (psl->array[i] == x)
    7. {
    8. return i;
    9. }
    10. }
    11. return -1;
    12. }

    顺序表在pos位置插入x

    pos指的是数组的下标,在寻找的时候注意头和尾,pos=size 相当于尾插,

    1. //顺序表在pos位置插入x
    2. void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
    3. {
    4. assert(psl);
    5. assert(pos>=0 && pos <= psl->size);
    6. SeqListCheckCapacity(psl);
    7. for (int i = psl->size; i > pos; i--)
    8. {
    9. psl->array[i] = psl->array[i - 1];
    10. }
    11. psl->array[pos] = x;
    12. psl->size++;
    13. }

    上述代码可以规避pos=0的情况,如果pos=0;i可以在0的位置停下来。相当于是头插; 如果是这样的代码会出现什么情况呢

    1. void SeqListInsert(SeqList* psl, size_t pos, SLDateType x)
    2. {
    3. assert(psl);
    4. assert(pos<=psl->size);
    5. for (int i = psl->size-1; i >= pos; i--)
    6. {
    7. psl->array[i + 1] = psl->array[i];
    8. }
    9. }

    当在下标为0的位置上插入一个数据时,i从psl->size-1到0,i进行i--操作,此时i=-1,再执行i>=pos操作,此时会停止循环吗?答案是不会,大家可以去调试,确实不会让循环停下。

    那为什么呢?因为pos为 size_t 的类型,size_t 为无符号整形-,当int(有符号整形)和size_t(无符号整形)比较大小时,int型的数据会发生算数转换,转换成unsigned int型,此时为负数的 i 就变成了很大的数字,自然而然比0大,因此会进入死循环。


    顺序表删除pos位置的值

    删除pos位置的值其实就是另一种的头删,采用覆盖pos位置的值的方法

    1. void SeqListErase(SeqList* psl, size_t pos)
    2. {
    3. assert(psl);
    4. assert(pos >= 0 && pos < psl->size);
    5. for (int i = pos; i < psl->size - 1; i++)
    6. {
    7. psl->array[i] = psl->array[i + 1];
    8. }
    9. psl->size--;
    10. }

    顺序表的销毁

    1. void SeqListDestory(SeqList* psl)
    2. {
    3. assert(psl);
    4. free(psl->array);
    5. psl->array = NULL;
    6. psl->capacity = psl->size = 0;
    7. }

    总代码

    头文件--seqlist.h

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #define INIT_CAPACITY 4
    6. typedef int SLDataType;
    7. typedef struct Seqlist
    8. {
    9. SLDataType* array;
    10. size_t size;
    11. size_t capacity;
    12. }SeqList;
    13. //顺序表初始化
    14. void SeqListInit(SeqList* psl); //psl p指针,sl顺序表
    15. //检查空间,如果满了,进行增容
    16. void SeqListCheckCapacity(SeqList* psl);
    17. //顺序表尾插
    18. void SeqListPushBack(SeqList* psl, SLDataType x);
    19. //顺序表尾删
    20. void SeqListPopBack(SeqList* psl);
    21. //顺序表头插
    22. void SeqListPushFront(SeqList* psl, SLDataType x);
    23. //顺序表头删
    24. void SeqListPopFront(SeqList* psl);
    25. //顺序表查找
    26. int SeqListFind(SeqList* psl, SLDataType x);
    27. //顺序表在pos位置插入x
    28. void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
    29. //顺序表删除pos位置的值
    30. void SeqListErase(SeqList* psl, size_t pos);
    31. //顺序表销毁
    32. void SeqListDestory(SeqList* psl);
    33. //打印顺序表
    34. void PrintSL(SeqList* psl);

    函数文件--seqlist.c

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include"seqlist.h"
    3. //打印顺序表
    4. void PrintSL(SeqList* psl)
    5. {
    6. assert(psl);
    7. for (int i = 0; i < psl->size; i++)
    8. {
    9. printf("%d ", psl->array[i]);
    10. }
    11. printf("\n ");
    12. }
    13. //顺序表初始化
    14. void SeqListInit(SeqList* psl) //psl p指针,sl顺序表
    15. {
    16. assert(psl);
    17. psl->array = (SLDataType*)malloc(sizeof(SLDataType) * INIT_CAPACITY);
    18. if (psl->array == NULL)
    19. {
    20. perror("fail in malloc");
    21. return;
    22. }
    23. psl->size = 0;
    24. psl->capacity = INIT_CAPACITY;
    25. }
    26. //检查空间,如果满了,进行增容
    27. void SeqListCheckCapacity(SeqList* psl)
    28. {
    29. assert(psl);
    30. if (psl->size == psl->capacity)
    31. {
    32. SLDataType* newnode = (SLDataType*)realloc(psl->array, sizeof(SLDataType)* 2 * psl->capacity);
    33. if (newnode == NULL)
    34. {
    35. perror("fail in realloc");
    36. return;
    37. }
    38. psl->array = newnode;
    39. psl->capacity *= 2;
    40. }
    41. }
    42. //顺序表销毁
    43. void SeqListDestory(SeqList* psl)
    44. {
    45. assert(psl);
    46. free(psl->array);
    47. psl->array = NULL;
    48. psl->capacity = psl->size = 0;
    49. }
    50. //顺序表尾插
    51. void SeqListPushBack(SeqList* psl, SLDataType x)
    52. {
    53. assert(psl);
    54. SeqListCheckCapacity(psl);
    55. psl->array[psl->size] = x;//size是顺序表的最后一个位置
    56. psl->size++;
    57. }
    58. //顺序表尾删
    59. void SeqListPopBack(SeqList* psl)
    60. {
    61. assert(psl);
    62. assert(psl->size > 0);
    63. psl->size--;
    64. //不能单独free,申请的空间要不然就一起释放掉,不能单独释放某一段空间
    65. }
    66. //顺序表头插
    67. void SeqListPushFront(SeqList* psl, SLDataType x)
    68. {
    69. assert(psl);
    70. SeqListCheckCapacity(psl);
    71. for (int i = psl->size; i > 0; i--)
    72. {
    73. psl->array[i] = psl->array[i - 1];
    74. }
    75. psl->array[0] = x;
    76. psl->size++;
    77. }
    78. //顺序表头删
    79. void SeqListPopFront(SeqList* psl)
    80. {
    81. assert(psl);
    82. assert(psl->size > 0);
    83. for (int i = 0; i < psl->size-1; i++)
    84. {
    85. psl->array[i] = psl->array[i + 1];
    86. }
    87. psl->size--;
    88. }
    89. //顺序表查找
    90. int SeqListFind(SeqList* psl, SLDataType x)
    91. {
    92. assert(psl);
    93. for (int i = 0; i < psl->size; i++)
    94. {
    95. if (psl->array[i] == x)
    96. {
    97. return i;
    98. }
    99. }
    100. return -1;
    101. }
    102. //顺序表在pos位置插入x
    103. void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
    104. {
    105. assert(psl);
    106. assert(pos>=0 && pos <= psl->size);
    107. SeqListCheckCapacity(psl);
    108. for (int i = psl->size; i > pos; i--)
    109. {
    110. psl->array[i] = psl->array[i - 1];
    111. }
    112. psl->array[pos] = x;
    113. psl->size++;
    114. }
    115. //顺序表删除pos位置的值
    116. void SeqListErase(SeqList* psl, size_t pos)
    117. {
    118. assert(psl);
    119. assert(pos >= 0 && pos < psl->size);
    120. for (int i = pos; i < psl->size - 1; i++)
    121. {
    122. psl->array[i] = psl->array[i + 1];
    123. }
    124. psl->size--;
    125. }

    测试文件--test.c

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include"seqlist.h"
    3. void Test()
    4. {
    5. SeqList seq;
    6. SeqListInit(&seq);
    7. SeqListPushBack(&seq, 1);
    8. SeqListPushBack(&seq, 1);
    9. SeqListPushBack(&seq, 1);
    10. SeqListPushBack(&seq, 1);
    11. SeqListPushBack(&seq, 1);
    12. SeqListPushBack(&seq, 1);
    13. SeqListPushBack(&seq, 1);
    14. SeqListPushFront(&seq, 4);
    15. SeqListPopFront(&seq);
    16. SeqListInsert(&seq, 3, 6);
    17. SeqListInsert(&seq, 3, 6);
    18. SeqListInsert(&seq, 3, 6);
    19. SeqListInsert(&seq, 3, 6);
    20. SeqListInsert(&seq, 3, 6);
    21. SeqListInsert(&seq, 3, 6);
    22. SeqListErase(&seq, 3);
    23. PrintSL(&seq);
    24. SeqListDestory(&seq);
    25. }
    26. int main()
    27. {
    28. Test();
    29. return 0;
    30. }

  • 相关阅读:
    数说睿见连锁药店城市开店空间模型举例,详解渠道经营方法论
    华为HG532系列路由器命令注入漏洞复现 CVE-2017-17215
    c语言基础:L1-059 敲笨钟
    后端实现跨域的几种方案
    [1157]dbeaverr的ssh远程连接
    模型效果优化,试一下多种交叉验证的方法(系统实操)
    VBA技术资料MF144:将PDF首页作为对象插入工作表
    零极点分析
    如何设计存储架构
    ARM Developer Keil MDK 5.X Crack
  • 原文地址:https://blog.csdn.net/Senyu_nuanshu/article/details/134033254