深圳幻海软件技术有限公司 欢迎您!

顺序表——“数据结构与算法”

2023-03-19

各位CSDN的uu们你们好呀,今天小雅兰的内容是数据结构与算法里面的顺序表啦,在我看来,数据结构总体上是一个抽象的东西,关键还是要多写代码,下面,就让我们进入顺序表的世界吧线性表顺序表线性表线性表(linearlist)是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,

各位CSDN的uu们你们好呀,今天小雅兰的内容是数据结构与算法里面的顺序表啦,在我看来,数据结构总体上是一个抽象的东西,关键还是要多写代码,下面,就让我们进入顺序表的世界吧


线性表

顺序表


线性表

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

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


 顺序表

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

顺序表就是数组,但是在数组的基础上,它还要求数据是从头开始连续存储的,不能跳跃间隔 

顺序表一般可以分为:

 静态顺序表:使用定长数组存储元素。

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #define N 7
  3. #include"SeqList.h"
  4. typedef int SLDateType;
  5. typedef struct SeqList
  6. {
  7. SLDateType arr[N];//定长数组
  8. size_t size;//表示数组中存储了多少数据
  9. }SL;

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

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #define N 7
  3. #include"SeqList.h"
  4. typedef int SLDateType;
  5. typedef struct SeqList
  6. {
  7. SLDateType* arr;//指向动态开辟的数组
  8. size_t size;//表示数组中存储了多少数据
  9. size_t capicity;//数组实际能存储数据的空间容量是多大
  10. }SL;

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

在这里,所有函数命名风格都是跟着STL走的,方便小雅兰日后的学习!!! 


顺序表的初始化:

  1. void SeqListInit(SL* ps)//顺序表的初始化
  2. {
  3. ps->arr = NULL;
  4. ps->size = ps->capacity = 0;
  5. }

注意:在这里,不可以使用传值调用的方式,只能使用传址调用的方式

  1. void SeqListInit(SL ps)//顺序表的初始化
  2. {
  3. ps.arr = NULL;
  4. ps.size = ps.capacity = 0;
  5. }

万万不能使用这样的方式初始化,因为:在函数传参的时候,形参是实参的一份临时拷贝,对形参的修改并不会影响实参

 顺序表的打印:

  1. void SeqListPrint(SL* ps)//顺序表的打印
  2. {
  3. int i = 0;
  4. for (i = 0; i < ps->size; i++)
  5. {
  6. printf("%d ", ps->arr[i]);
  7. }
  8. printf("\n");
  9. }

扩容:

  1. void SeqListCheckCapacity(SL* ps)//检查空间,如果满了,进行扩容
  2. {
  3. //如果没有空间或者空间不足,那么我们就扩容
  4. if (ps->size == ps->capacity)
  5. {
  6. int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
  7. SLDateType* tmp = (SLDateType*)realloc(ps->arr, newcapacity * sizeof(SLDateType));
  8. if (tmp == NULL)
  9. {
  10. printf("realloc fail!\n");
  11. exit(-1);
  12. }
  13. ps->arr = tmp;
  14. ps->capacity = newcapacity;
  15. }
  16. }

顺序表销毁:

  1. void SeqListDestroy(SL* ps)//顺序表销毁
  2. {
  3. free(ps->arr);
  4. ps->arr = NULL;
  5. ps->capacity = ps->size = 0;
  6. }

尾插:

  1. void SeqListPushBack(SL* ps, SLDateType x)//尾插
  2. {
  3. SeqListCheckCapacity(ps);
  4. ps->arr[ps->size] = x;
  5. ps->size++;
  6. }

尾删:

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

 头插:

  1. void SeqListPushFront(SL* ps, SLDateType x)//头插
  2. {
  3. SeqListCheckCapacity(ps);
  4. //挪动数据
  5. int end = ps->size - 1;
  6. while (end >= 0)
  7. {
  8. ps->arr[end + 1] = ps->arr[end];
  9. end--;
  10. }
  11. ps->arr[0] = x;
  12. ps->size++;
  13. }

头删:

  1. void SeqListPopFront(SL* ps)//头删
  2. {
  3. assert(ps->size > 0);
  4. //挪动数据
  5. int begin = 0;
  6. while (begin < ps->size-1)
  7. {
  8. ps->arr[begin] = ps->arr[begin+1];
  9. ++begin;
  10. }
  11. ps->size--;
  12. }
另一种写法:
  1. void SeqListPopFront(SL* ps)//头删
  2. {
  3. assert(ps->size > 0);
  4. //挪动数据
  5. int begin = 1;
  6. while (begin < ps->size)
  7. {
  8. ps->arr[begin - 1] = ps->arr[begin];
  9. ++begin;
  10. }
  11. ps->size--;
  12. }

 顺序表查找:

  1. //顺序表查找
  2. //找到了返回x位置的下标,没有找到返回-1
  3. int SeqListFind(SL* ps, SLDateType x)
  4. {
  5. int i = 0;
  6. for (i = 0; i < ps->size; i++)
  7. {
  8. if (ps->arr[i] == x)
  9. {
  10. return i;
  11. }
  12. }
  13. return -1;
  14. }

 指定pos下标位置插入:

  1. // 顺序表指定pos下标位置插入x
  2. void SeqListInsert(SL* ps, size_t pos, SLDateType x)
  3. {
  4. 温柔的写法
  5. //if (pos > ps->size || pos < 0)
  6. //{
  7. //printf("pos invalid!\n");
  8. //}
  9. //粗暴的写法
  10. assert(pos <= ps->size || pos >= 0);
  11. SeqListCheckCapacity(ps);
  12. //挪动数据
  13. int end = ps->size - 1;
  14. while (end >= pos)
  15. {
  16. ps->arr[end + 1] = ps->arr[end];
  17. end--;
  18. }
  19. ps->arr[pos] = x;
  20. ps->size++;
  21. }

 然后,写出了这个函数的功能之后,我们发现:可以对之前写的头插和尾插搞一些事情,下面,我们开始搞事情!!!

头插和尾插:

  1. void SeqListPushBack(SL* ps, SLDateType x)//尾插
  2. {
  3. SeqListInsert(ps, ps->size, x);
  4. }
  5. void SeqListPushFront(SL* ps, SLDateType x)//头插
  6. {
  7. SeqListInsert(ps, 0, x);
  8. }

 删除pos位置的数据:

  1. //顺序表删除pos位置的数据
  2. void SeqListErase(SL* ps, size_t pos)
  3. {
  4. assert(pos < ps->size || pos >= 0);
  5. int begin = pos + 1;
  6. while (begin < ps->size)
  7. {
  8. ps->arr[begin - 1] = ps->arr[begin];
  9. ++begin;
  10. }
  11. ps->size--;
  12. }

写出这个函数的功能之后,我们又可以搞事情啦!!!

头删和尾删:

  1. void SeqListPopBack(SL* ps)//尾删
  2. {
  3. SeqListErase(ps, 0);
  4. }
  5. void SeqListPopFront(SL* ps)//头删
  6. {
  7. SeqListErase(ps, ps->size - 1);
  8. }

菜单:

  1. void menu()
  2. {
  3. printf("#############################################\n");
  4. printf("\n");
  5. printf("###################1.头插#####################\n");
  6. printf("\n");
  7. printf("###################2.头删#####################\n");
  8. printf("\n");
  9. printf("###################3.尾插######################\n");
  10. printf("\n");
  11. printf("###################4.尾删######################\n");
  12. printf("\n");
  13. printf("###################5.打印#####################\n");
  14. printf("\n");
  15. printf("###################0.exit#####################\n");
  16. printf("\n");
  17. printf("##############################################\n");
  18. }

其实,最好不要一上来就写菜单,先写单元测试,菜单不方便调试

 测试菜单函数:

  1. void MenuTest()
  2. {
  3. SL s1;
  4. SeqListInit(&s1);
  5. int input = 0;
  6. int x;
  7. while (input != -1)
  8. {
  9. menu();
  10. scanf("%d", &input);
  11. switch (input)
  12. {
  13. case 1:
  14. printf("请输入你要头插的数据,以-1结束:");
  15. while (x != -1)
  16. {
  17. SeqListPushFront(&s1, x);
  18. scanf("%d", &x);
  19. }
  20. break;
  21. case 2:
  22. SeqListPopFront(&s1);
  23. break;
  24. case 3:
  25. printf("请输入你要尾插的数据,以-1结束:");
  26. while (x != -1)
  27. {
  28. SeqListPushBack(&s1, x);
  29. scanf("%d", &x);
  30. }
  31. break;
  32. case 4:
  33. SeqListPopBack(&s1);
  34. break;
  35. case 5:
  36. SeqListPrint(&s1);
  37. break;
  38. case 0:
  39. printf("退出程序\n");
  40. break;
  41. default:
  42. printf("输入错误,请重新输入\n");
  43. }
  44. }
  45. }

源代码如下:

SeqList.h的内容:

  1. #pragma once
  2. #include<stdio.h>
  3. #define _CRT_SECURE_NO_WARNINGS 1
  4. #include<stdlib.h>
  5. #include<assert.h>
  6. #define N 7
  7. typedef int SLDateType;
  8. //顺序表的动态存储
  9. typedef struct SeqList
  10. {
  11. SLDateType* arr;//指向动态开辟的数组
  12. size_t size;//表示数组中存储了多少数据
  13. size_t capacity;//数组实际能存储数据的空间容量是多大
  14. }SL;
  15. //基本增删查改接口
  16. void SeqListInit(SL* ps);//顺序表的初始化
  17. void SeqListPrint(SL* ps);//顺序表的打印
  18. void SeqListCheckCapacity(SL* ps);//检查空间,如果满了,进行扩容
  19. void SeqListDestroy(SL* ps);//顺序表销毁
  20. void SeqListPushBack(SL* ps, SLDateType x);//尾插
  21. void SeqListPopBack(SL* ps);//尾删
  22. void SeqListPushFront(SL* ps, SLDateType x);//头插
  23. void SeqListPopFront(SL* ps);//头删
  24. //顺序表查找
  25. //找到了返回x位置的下标,没有找到返回-1
  26. int SeqListFind(SL* ps, SLDateType x);
  27. //顺序表指定pos下标位置插入x
  28. void SeqListInsert(SL* ps, size_t pos, SLDateType x);
  29. //顺序表删除pos位置的数据
  30. void SeqListErase(SL* ps, size_t pos);

SeqList.c的内容:

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

好啦,小雅兰今天的内容就到这里啦,数据结构的的确确是一个麻烦的东西,还要加油呀!!!

 

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览41000 人正在系统学习中