• Day1:数据结构&算法之顺序表


    目录

    一、基本概念以及相关思路、注意点

            1.注意细节:

            2.核心模块:增 删 改 查

                    ①增加(两种方法):

                    ②删除(伪删除)

                    ③查找:由data获取并返回data的index

    二、C语言实现顺序表

    sqlist.h

     sqlist.c

     Main顺序表.c

    三、C++实现顺序表 

    sqlist.h

    sqlist.cpp

    Main顺序表.cpp 


    一、基本概念以及相关思路、注意点

            1.注意细节:

                    顺序表:取值时,优先判断是否为NULL,增加时要看是否满了(链表就无需担心)

                            null 、curSize、MaxSize

     

            2.核心模块:增 删 改 查

                    ①增加(两种方法):

                            法一:直接把数据丢到末尾,然后依次swap至index指定位置

                            法二:现平移出位置,然后对空位进行赋值

                    优化:提供push_front()和push_back()两种接口

                    对于有序表

                                    step1:先将data插入到表的最后面,

                                    step2:再依次和前面进行比较,找到比其小的位置(注意边界问题:不能越界&&记得不管是增还是删除,及时更新curSize!!!)

                    ②删除(向前移动覆盖->伪删除)

                                    ->真正的删除在curSize--

                     优化:提供pop_front()和pop_back()两种接口

                    ③查找:由data获取并返回data的index

    二、C语言实现顺序表

    sqlist.h

    1. #pragma once
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <assert.h>
    5. #define MAX 10 //最大元素个数
    6. typedef int DataType;
    7. //结构体抽象---> 抽象长相
    8. enum INFO {OK=1,ERROR=-1};
    9. typedef struct
    10. {
    11. DataType* dataMemory; //一级指针动态内存申请变成一位数组
    12. int maxSize; //最大元素个数-->容量
    13. int curSize; //当前元素个数
    14. }Sqlist,*LPSqlist;
    15. LPSqlist createSqlist(int maxSize);
    16. //万金油函数-->所有数据结构都可以有的
    17. int size(LPSqlist sqlist);
    18. int empty(LPSqlist sqlist);
    19. //增删改查
    20. DataType getData(LPSqlist sqlist, int index);
    21. int insertSqlist(LPSqlist sqlist, int index, DataType data);
    22. void printSqlist(LPSqlist sqlist);
    23. int deleteSqlist(LPSqlist sqlist, int index);
    24. void push_front(LPSqlist sqlist,DataType data); //头部插入
    25. void pop_front(LPSqlist sqlist);
    26. void push_back(LPSqlist sqlist, DataType data);
    27. void pop_back(LPSqlist sqlist);
    28. int searchSqlist(LPSqlist sqlist, DataType data);
    29. void insert_sort(LPSqlist sqlist, DataType data);
    30. void destroySqlist(LPSqlist* sqlist);

     sqlist.c

    1. #include "sqlist.h"
    2. LPSqlist createSqlist(int maxSize)
    3. {
    4. //创建过程,就是初始化数据
    5. LPSqlist sqlist = (LPSqlist)malloc(sizeof(Sqlist));
    6. assert(sqlist);
    7. sqlist->curSize = 0;
    8. sqlist->maxSize = maxSize;
    9. //指针变成一个数组
    10. sqlist->dataMemory = (DataType*)malloc(sizeof(DataType)*maxSize);
    11. assert(sqlist->dataMemory);
    12. return sqlist;
    13. }
    14. int size(LPSqlist sqlist)
    15. {
    16. if (sqlist == NULL)
    17. return ERROR;
    18. return sqlist->curSize;
    19. }
    20. int empty(LPSqlist sqlist)
    21. {
    22. if (sqlist == NULL)
    23. return ERROR;
    24. return sqlist->curSize==0;
    25. }
    26. DataType getData(LPSqlist sqlist, int index)
    27. {
    28. if (sqlist == NULL||sqlist->curSize==0)
    29. {
    30. printf("sqlist is empty,unable to get......\n");
    31. return ERROR;
    32. }
    33. if ( sqlist->curSize < index)
    34. {
    35. //invalid 无效
    36. printf("index is invalid,unable to get......\n");
    37. return ERROR;
    38. }
    39. //index是第几个元素
    40. return sqlist->dataMemory[index-1];
    41. }
    42. int insertSqlist(LPSqlist sqlist, int index, DataType data)
    43. {
    44. //数组实现的数据结构:放进去需要考虑满
    45. if (sqlist == NULL)
    46. {
    47. printf("sqlist is empty,unable to insert......\n");
    48. return ERROR;
    49. }
    50. if (sqlist->curSize == sqlist->maxSize)
    51. {
    52. printf("sqlist is full,unbale to insert......\n");
    53. return ERROR;
    54. }
    55. if (index == 0)
    56. {
    57. sqlist->dataMemory[sqlist->curSize++] = data;
    58. return OK;
    59. }
    60. if (sqlist->curSize < 1 || sqlist->curSize < index)
    61. {
    62. printf("index is invalid,unable to insert......\n");
    63. return ERROR;
    64. }
    65. #if 0
    66. int pos = sqlist->curSize;
    67. sqlist->dataMemory[pos] = data; //插在最后面
    68. while (pos != index)
    69. {
    70. DataType temp = sqlist->dataMemory[pos];
    71. sqlist->dataMemory[pos] = sqlist->dataMemory[pos - 1];
    72. sqlist->dataMemory[pos - 1] = temp;
    73. pos--;
    74. }
    75. sqlist->curSize++;
    76. #endif
    77. for (int k = sqlist->curSize; k >= index; k--)
    78. {
    79. sqlist->dataMemory[k] = sqlist->dataMemory[k - 1];
    80. }
    81. sqlist->dataMemory[index - 1] = data;
    82. sqlist->curSize++;
    83. return OK;
    84. }
    85. void printSqlist(LPSqlist sqlist)
    86. {
    87. if (sqlist == NULL||sqlist->curSize==0)
    88. {
    89. printf("sqlist is empty,unable to print......\n");
    90. return;
    91. }
    92. for (int i = 0; i < sqlist->curSize; i++)
    93. {
    94. printf("%d\t", sqlist->dataMemory[i]);
    95. }
    96. printf("\n");
    97. }
    98. int deleteSqlist(LPSqlist sqlist, int index)
    99. {
    100. if (sqlist == NULL || sqlist->curSize == 0)
    101. {
    102. printf("sqlist is empty,unable to delete......\n");
    103. return ERROR;
    104. }
    105. if (sqlist->curSize < index)
    106. {
    107. printf("index is invalid,unbale to delete......\n");
    108. return ERROR;
    109. }
    110. for (int k = index - 1; k < sqlist->curSize; k++)
    111. {
    112. sqlist->dataMemory[k] = sqlist->dataMemory[k + 1];
    113. }
    114. sqlist->curSize--; //伪删除
    115. return OK;
    116. }
    117. void push_front(LPSqlist sqlist, DataType data)
    118. {
    119. insertSqlist(sqlist, 1, data);
    120. }
    121. void pop_front(LPSqlist sqlist)
    122. {
    123. deleteSqlist(sqlist, 1);
    124. }
    125. void push_back(LPSqlist sqlist, DataType data)
    126. {
    127. if (sqlist == NULL)
    128. {
    129. printf("sqlist is empty,unable to pushback");
    130. return;
    131. }
    132. if (sqlist->curSize == sqlist->maxSize)
    133. {
    134. printf("sqlist is full,unable to pushback");
    135. return;
    136. }
    137. sqlist->dataMemory[sqlist->curSize++] = data;
    138. }
    139. void pop_back(LPSqlist sqlist)
    140. {
    141. deleteSqlist(sqlist, sqlist->curSize);
    142. }
    143. int searchSqlist(LPSqlist sqlist, DataType data)
    144. {
    145. if (sqlist == NULL)
    146. {
    147. printf("sqlist is empty,unable to search......\n");
    148. return ERROR;
    149. }
    150. for (int i = 0; i < sqlist->curSize; i++)
    151. {
    152. if (sqlist->dataMemory[i] == data)
    153. {
    154. return i;
    155. }
    156. }
    157. return ERROR;
    158. }
    159. void insert_sort(LPSqlist sqlist, DataType data)
    160. {
    161. if (sqlist == NULL)
    162. {
    163. printf("sqlist is empty,unable to insert......\n");
    164. return;
    165. }
    166. if (sqlist->curSize == sqlist->maxSize)
    167. {
    168. printf("sqlist is full,unable to insert......\n");
    169. return;
    170. }
    171. if (sqlist->curSize == 0)
    172. {
    173. sqlist->dataMemory[sqlist->curSize++] = data;
    174. }
    175. sqlist->dataMemory[sqlist->curSize] = data;
    176. int pos = sqlist->curSize;
    177. for (int k = sqlist->curSize;k>0; k--)
    178. {
    179. if (sqlist->dataMemory[k] < sqlist->dataMemory[k - 1])
    180. {
    181. DataType temp = sqlist->dataMemory[k];
    182. sqlist->dataMemory[k] = sqlist->dataMemory[k - 1];
    183. sqlist->dataMemory[k - 1] = temp;
    184. }
    185. else
    186. {
    187. break;
    188. }
    189. }
    190. sqlist->curSize++;
    191. }
    192. void destroySqlist(LPSqlist* sqlist)
    193. {
    194. if (*sqlist == NULL)
    195. {
    196. printf("sqlist is empty, unable to destory......\n");
    197. return;
    198. }
    199. free((*sqlist)->dataMemory);
    200. free(*sqlist);
    201. *sqlist = NULL;
    202. }

     Main顺序表.c

    1. #include "sqlist.h"
    2. int main()
    3. {
    4. LPSqlist sqlist = createSqlist(MAX);
    5. printf("test insert:\n");
    6. insertSqlist(sqlist, 0, 0); //0
    7. insertSqlist(sqlist, 1, 1); //1 0
    8. insertSqlist(sqlist, 1, 2); //2 1 0
    9. insertSqlist(sqlist, 1, 3); //3 2 1 0
    10. insertSqlist(sqlist, 4, 444);
    11. printSqlist(sqlist);
    12. printf("test delete:\n");
    13. deleteSqlist(sqlist, 4);
    14. printSqlist(sqlist);
    15. printf("test push and pop:\n");
    16. push_front(sqlist, 666);
    17. push_back(sqlist, 999);
    18. printSqlist(sqlist);
    19. pop_front(sqlist);
    20. pop_back(sqlist);
    21. printSqlist(sqlist);
    22. int pos = searchSqlist(sqlist, 3);
    23. printf("pos:%d\n", pos);
    24. destroySqlist(&sqlist);
    25. LPSqlist sortSqlist = createSqlist(MAX);
    26. insert_sort(sortSqlist, 1);
    27. insert_sort(sortSqlist, 100);
    28. insert_sort(sortSqlist, 77);
    29. insert_sort(sortSqlist, -1);
    30. printSqlist(sortSqlist);
    31. destroySqlist(&sortSqlist);
    32. return 0;
    33. }

     输出:

    test insert:
    3       2       1       444     0
    test delete:
    3       2       1       0
    test push and pop:
    666     3       2       1       0       999
    3       2       1       0
    pos:0
    -1      1       1       77      100

    三、C++实现顺序表 

    sqlist.h

    1. #pragma once
    2. #pragma once
    3. #include <iostream>
    4. using namespace std;
    5. #define MAX 10 //最大元素个数
    6. typedef int DataType;
    7. //结构体抽象---> 抽象长相
    8. enum INFO { OK = 1, ERROR = -1 };
    9. class Sqlist
    10. {
    11. public:
    12. Sqlist(int MaxSize);
    13. int size() const;
    14. int empty()const;
    15. DataType getData(int index);
    16. int insertSqlist(int index, DataType data);
    17. void printSqlist();
    18. int deleteSqlist( int index);
    19. void push_front( DataType data); //头部插入
    20. void pop_front();
    21. void push_back(DataType data);
    22. void pop_back();
    23. int searchSqlist( DataType data);
    24. void insert_sort(DataType data);
    25. void destroySqlist();
    26. protected:
    27. DataType* dataMemory;
    28. int maxSize;
    29. int curSize;
    30. };

    sqlist.cpp

    1. #include "sqlist.h"
    2. Sqlist::Sqlist(int MaxSize)
    3. {
    4. //创建过程,就是初始化数据
    5. curSize = 0;
    6. maxSize = MaxSize;
    7. //指针变成一个数组
    8. dataMemory =new DataType[maxSize];
    9. }
    10. int Sqlist::size() const
    11. {
    12. if (dataMemory == NULL)
    13. return ERROR;
    14. return curSize;
    15. }
    16. int Sqlist::empty() const
    17. {
    18. if (dataMemory == NULL)
    19. return ERROR;
    20. return curSize==0;
    21. }
    22. DataType Sqlist::getData(int index)
    23. {
    24. if (curSize == 0)
    25. {
    26. cout<<"sqlist is empty,unable to get......"<<endl;
    27. return ERROR;
    28. }
    29. if (curSize < index)
    30. {
    31. //invalid 无效
    32. cout << "index is invalid,unable to get......" << endl;;
    33. return ERROR;
    34. }
    35. //index是第几个元素
    36. return dataMemory[index - 1];
    37. }
    38. int Sqlist::insertSqlist(int index, DataType data)
    39. {
    40. //数组实现的数据结构:放进去需要考虑满
    41. if (curSize == maxSize)
    42. {
    43. printf("sqlist is full,unbale to insert......\n");
    44. return ERROR;
    45. }
    46. if (index == 0)
    47. {
    48. dataMemory[curSize++] = data;
    49. return OK;
    50. }
    51. if (curSize < 1 || curSize < index)
    52. {
    53. printf("index is invalid,unable to insert......\n");
    54. return ERROR;
    55. }
    56. #if 0
    57. int pos = sqlist->curSize;
    58. dataMemory[pos] = data; //插在最后面
    59. while (pos != index)
    60. {
    61. DataType temp = dataMemory[pos];
    62. dataMemory[pos] = dataMemory[pos - 1];
    63. dataMemory[pos - 1] = temp;
    64. pos--;
    65. }
    66. curSize++;
    67. #endif
    68. for (int k = curSize; k >= index; k--)
    69. {
    70. dataMemory[k] = dataMemory[k - 1];
    71. }
    72. dataMemory[index - 1] = data;
    73. curSize++;
    74. return OK;
    75. }
    76. void Sqlist::printSqlist()
    77. {
    78. if (curSize == 0)
    79. {
    80. cout << "sqlist is empty,unable to print......\n" << endl;
    81. return;
    82. }
    83. for (int i = 0; i < curSize; i++)
    84. {
    85. printf("%d\t", dataMemory[i]);
    86. }
    87. printf("\n");
    88. }
    89. int Sqlist::deleteSqlist(int index)
    90. {
    91. if (curSize == 0)
    92. {
    93. cout<<"sqlist is empty,unable to delete......\n";
    94. return ERROR;
    95. }
    96. if (curSize < index)
    97. {
    98. cout<<"index is invalid,unbale to delete......\n";
    99. return ERROR;
    100. }
    101. for (int k = index - 1; k < curSize; k++)
    102. {
    103. dataMemory[k] = dataMemory[k + 1];
    104. }
    105. curSize--; //伪删除
    106. return OK;
    107. }
    108. void Sqlist::push_front(DataType data)
    109. {
    110. insertSqlist(1, data);
    111. }
    112. void Sqlist::pop_front()
    113. {
    114. deleteSqlist(1);
    115. }
    116. void Sqlist::push_back(DataType data)
    117. {
    118. if (curSize == maxSize)
    119. {
    120. printf("sqlist is full,unable to pushback");
    121. return;
    122. }
    123. dataMemory[curSize++] = data;
    124. }
    125. void Sqlist::pop_back()
    126. {
    127. deleteSqlist(curSize);
    128. }
    129. int Sqlist::searchSqlist(DataType data)
    130. {
    131. for (int i = 0; i < curSize; i++)
    132. {
    133. if (dataMemory[i] == data)
    134. {
    135. return i;
    136. }
    137. }
    138. return ERROR;
    139. }
    140. void Sqlist::insert_sort(DataType data)
    141. {
    142. if (curSize ==maxSize)
    143. {
    144. cout<<"sqlist is full,unable to insert......\n";
    145. return;
    146. }
    147. if (curSize == 0)
    148. {
    149. dataMemory[curSize++] = data;
    150. }
    151. dataMemory[curSize] = data;
    152. int pos = curSize;
    153. for (int k = curSize; k > 0; k--)
    154. {
    155. if (dataMemory[k] < dataMemory[k - 1])
    156. {
    157. DataType temp = dataMemory[k];
    158. dataMemory[k] = dataMemory[k - 1];
    159. dataMemory[k - 1] = temp;
    160. }
    161. else
    162. {
    163. break;
    164. }
    165. }
    166. curSize++;
    167. }
    168. void Sqlist::destroySqlist()
    169. {
    170. delete [] dataMemory;
    171. dataMemory = nullptr;
    172. }

    Main顺序表.cpp 

    1. #include "sqlist.h"
    2. int main()
    3. {
    4. Sqlist *pSqlist=new Sqlist(MAX);
    5. printf("test insert:\n");
    6. pSqlist->insertSqlist( 0, 0); //0
    7. pSqlist->insertSqlist( 1, 1); //1 0
    8. pSqlist->insertSqlist( 1, 2); //2 1 0
    9. pSqlist->insertSqlist( 1, 3); //3 2 1 0
    10. pSqlist->insertSqlist( 4, 444);
    11. pSqlist->printSqlist();
    12. printf("test delete:\n");
    13. pSqlist->deleteSqlist( 4);
    14. pSqlist->printSqlist();
    15. printf("test push and pop:\n");
    16. pSqlist->push_front(666);
    17. pSqlist->push_back(999);
    18. pSqlist->printSqlist();
    19. pSqlist->pop_front();
    20. pSqlist->pop_back();
    21. pSqlist->printSqlist();
    22. int pos = pSqlist->searchSqlist(3);
    23. printf("pos:%d\n", pos);
    24. pSqlist->destroySqlist();
    25. delete pSqlist;
    26. pSqlist = nullptr;
    27. Sqlist sortSqlist(10);
    28. sortSqlist.insert_sort(1);
    29. sortSqlist.insert_sort(100);
    30. sortSqlist.insert_sort(77);
    31. sortSqlist.insert_sort(-1);
    32. sortSqlist.printSqlist();
    33. sortSqlist.destroySqlist();
    34. return 0;
    35. }

  • 相关阅读:
    巨好看的登录注册界面源码
    K8S精进之路-控制器Deployment-(1)
    Vue中的$attrs和inheritAttrs
    Intel汇编-Linux系统调用
    网络安全(黑客)—自学
    算法刷题2(C++)链表遍历算法
    java计算机毕业设计医院远程诊断系统源代码+系统+数据库+lw文档
    【Nano Framework ESP32 篇】刷入 nanoCLR 固件以及相关问题
    案例4-1.4 堆中的路径 + 基础实验4-2.5 关于堆的判断
    设计两个异常类,用于检查身高
  • 原文地址:https://blog.csdn.net/zjjaibc/article/details/125630084