• 数据结构:顺序表(C++实现)


     1 头文件 SeqList.h

    1. //SeqList.h
    2. #pragma once
    3. #define _CRT_SECURE_NO_WARNINGS
    4. #include
    5. #include
    6. using namespace std;
    7. class SeqList
    8. {
    9. public:
    10. //初始化
    11. SeqList();
    12. //销毁
    13. ~SeqList();
    14. //头插
    15. void PushFront(int data);
    16. //头删
    17. void PopFront();
    18. //尾插
    19. void PushBack(int data);
    20. //尾删
    21. void PopBack();
    22. //打印
    23. void Print();
    24. //寻找
    25. int* Find(int data);
    26. //插入
    27. void Insert(int pos, int data);
    28. //清除
    29. void Erase(int data);
    30. //检查容量
    31. void Check();
    32. private:
    33. //动态数组
    34. int* ptr;
    35. //数组索引
    36. int index;
    37. //数组容量
    38. int capacity;
    39. };

    2 源文件

    2.1 SeqList.cpp(函数)

    1. //SeqList.cpp
    2. #include "SeqList.h"
    3. //练习顺序表
    4. //初始化
    5. SeqList::SeqList():ptr(nullptr) //创建变量时即给指针赋值为空指针
    6. {
    7. index = 0;
    8. capacity = 0;
    9. }
    10. //销毁
    11. SeqList::~SeqList()
    12. {
    13. if (ptr!=nullptr)
    14. {
    15. delete[]ptr;
    16. ptr = nullptr;
    17. capacity = 0;
    18. index = 0;
    19. cout << "~SeqList Destroyed" << endl;
    20. }
    21. }
    22. //头插
    23. void SeqList::PushFront(int data)
    24. {
    25. Check();
    26. for (int i = index - 1; i >= 0; i--)
    27. {
    28. ptr[i + 1] = ptr[i];
    29. }
    30. ptr[0] = data;
    31. index++;
    32. }
    33. //头删
    34. void SeqList::PopFront()
    35. {
    36. for (int i = 1; i < index; i++)
    37. {
    38. ptr[i - 1] = ptr[i];
    39. }
    40. index--;
    41. }
    42. //尾插
    43. void SeqList::PushBack(int data)
    44. {
    45. Check();
    46. assert(ptr);
    47. ptr[index++] = data;
    48. }
    49. //尾删
    50. void SeqList::PopBack()
    51. {
    52. assert(index >= 0);
    53. index--;
    54. }
    55. //打印
    56. void SeqList::Print()
    57. {
    58. for (int i = 0; i < index; i++)
    59. {
    60. cout << ptr[i] << " ";
    61. }
    62. cout << endl;
    63. }
    64. //寻找
    65. int* SeqList::Find(int data)
    66. {
    67. for (int i = 0; i < index; i++)
    68. {
    69. if (ptr[i] == data)
    70. {
    71. return ptr + i;
    72. }
    73. }
    74. return nullptr;
    75. }
    76. //插入
    77. void SeqList::Insert(int pos, int data)
    78. {
    79. assert(pos > 0);
    80. Check();
    81. for (int i = index - 1; i >= pos - 1; i--)
    82. {
    83. ptr[i + 1] = ptr[i];
    84. }
    85. ptr[pos-1] = data;
    86. index++;
    87. }
    88. //清除
    89. void SeqList::Erase(int data)
    90. {
    91. int flag = 1;
    92. for (int i = 0; i < index; i++)
    93. {
    94. if (ptr[i] == data)
    95. {
    96. for (int j = i; j
    97. {
    98. ptr[j] = ptr[j + 1];
    99. }
    100. index--;
    101. flag = 0;
    102. }
    103. }
    104. if (flag)
    105. {
    106. cout << "该元素不存在" << endl;
    107. }
    108. }
    109. //检查容量
    110. void SeqList::Check()
    111. {
    112. if (ptr == nullptr || index == capacity)
    113. {
    114. int new_capcaity = (ptr == nullptr ? 4 : capacity * 2);
    115. //转移数据
    116. if (capacity != new_capcaity && capacity != 0)
    117. {
    118. int* temp = new int[new_capcaity];
    119. for (int i = 0; i < index; i++)
    120. {
    121. temp[i] = ptr[i];
    122. }
    123. delete[] ptr;
    124. ptr = temp;
    125. assert(ptr);
    126. capacity = new_capcaity;
    127. return;
    128. }
    129. ptr = new int[new_capcaity];
    130. capacity = new_capcaity;
    131. assert(ptr);
    132. }
    133. }

    2.2 test.cpp(测试,入口)

    1. //test.cpp
    2. #include"SeqList.h"
    3. int main()
    4. {
    5. SeqList a;
    6. a.PushBack(1);
    7. a.PushBack(2);
    8. a.PushBack(3);
    9. a.PushBack(4);
    10. a.PushBack(5);
    11. a.PushBack(6);
    12. a.PushBack(1);
    13. a.Print();
    14. a.PopBack();
    15. a.PopBack();
    16. a.PopBack();
    17. a.PopBack();
    18. a.Print();
    19. a.PushFront(1);
    20. a.PushFront(2);
    21. a.PushFront(9);
    22. a.Print();
    23. a.PopFront();
    24. a.Print();
    25. a.Insert(3, 7);
    26. cout<Find(2)<
    27. cout<Find(8)<
    28. a.Print();
    29. a.Insert(1, 8);
    30. a.Print();
    31. a.Erase(1);
    32. a.Print();
    33. a.Erase(1);
    34. return 0;
    35. }

  • 相关阅读:
    Proteus仿真--1602LCD显示仿手机键盘按键字符(仿真文件+程序)
    【Go语言】(一)环境搭建与了解VScode工具
    语音信号处理-基础(一):声学基础知识
    五分钟掌握NineData:新手任务速成指南
    ArcgisPro3.0.1中文安装包下载及安装教程
    ApiFox衔接前后端开发人员,提升沟通效率实践
    eclipse配置maven,安装lombok,导入和创建springboot项目
    【Vue】了解这些类和样式绑定就够了!
    数据指标显示,目前还没到牛市顶点 2021-04-24
    【题解】洛谷 P9658 Laser Trap
  • 原文地址:https://blog.csdn.net/Crazy_Rabbits/article/details/136607074