当前位置:   article > 正文

数据结构 - C/C++ - 栈

数据结构 - C/C++ - 栈

目录

结构特性

结构实现

结构容器

结构设计

顺序栈

链式栈


结构特性

  • 栈(stack)是线性表的一种形式,限定仅在表的一端进行插入或者删除的操作。

  • 栈顶 - 表中允许插入、删除的一端称为栈顶(top),栈顶位置是可以发生变化的。

    • 插入 - 进栈、入栈、压栈。

    • 删除 - 出栈。

  • 栈底 - 表中不允许插入、删除的一端称为栈底(bottom),栈底位置通常是固定不变的。

  • 空栈 - 表中不存在任何元素。

  • LIFO - last in first out - 先进后出、后进先出。

结构实现

  • 顺序栈
  1. int Arr[5] = {0};
  2. [栈顶] - Arr[4]
  3. [元素] - Arr[x]
  4. [元素] - Arr[x]
  5. [元素] - Arr[x]
  6. [栈底] - Arr[0]
  • 顺序栈使用连续的内存空间来存储元素,通常使用数组来实现。

  • 栈底指向数组起始地址(下标为0的元素)。

  • 栈顶指向当前栈中最后一个压入的元素位置。

  • 链式栈

[栈顶元素 | 指针] -> [下一个元素 | 指针] -> ... -> [栈底元素 | 空指针]

结构容器

  1. #include <iostream>
  2. #include <stack>
  3. int main()
  4. {
  5. std::stack<int> myStack;
  6. std::cout << myStack.size() << std::endl;
  7. std::cout << myStack.empty() << std::endl;
  8. //入栈
  9. myStack.push(1);
  10. myStack.push(2);
  11. myStack.push(3);
  12. std::cout << myStack.size() << std::endl;
  13. std::cout << myStack.empty() << std::endl;
  14. //获取栈顶元素
  15. std::cout << myStack.top() << std::endl;
  16. //出栈
  17. myStack.pop();
  18. std::cout << myStack.top() << std::endl;
  19. return 0;
  20. }

结构设计

顺序栈

  1. #include <iostream>
  2. class Stack
  3. {
  4. public:
  5. int* data; //栈的数组
  6. int topIndex; //栈顶索引
  7. int capacity; //栈的容量
  8. public:
  9. Stack(); //默认构造函数
  10. Stack(int Size); //有参构造函数
  11. Stack(const Stack& other); //拷贝构造函数
  12. ~Stack(); //默认析构函数
  13. public:
  14. void Push(int value); //入栈函数
  15. void Pop(); //出栈函数
  16. int Top(); //栈顶元素
  17. public:
  18. bool IsEmpty(); //是否为空
  19. int Size(); //元素个数
  20. };
  21. Stack::Stack() : data(nullptr), topIndex(-1), capacity(0)
  22. {
  23. }
  24. Stack::Stack(int Size) : topIndex(-1), capacity(Size)
  25. {
  26. this->data = new int[capacity] {};
  27. }
  28. Stack::Stack(const Stack& other) : data(new int[other.capacity]), topIndex(other.topIndex), capacity(other.capacity)
  29. {
  30. for (size_t i = 0; i < capacity; i++)
  31. {
  32. this->data[i] = other.data[i];
  33. }
  34. }
  35. Stack::~Stack()
  36. {
  37. if (data != NULL)
  38. {
  39. delete[] data;
  40. data = nullptr;
  41. }
  42. }
  43. void Stack::Push(int value)
  44. {
  45. if (Size() == capacity)
  46. {
  47. //默认容量
  48. int size = capacity;
  49. //动态扩容
  50. capacity = (capacity == 0) ? 5 : capacity * 2;
  51. //申请内存
  52. int* newData = new int[capacity] {};
  53. //数据拷贝
  54. memcpy(newData, this->data, size * sizeof(int));
  55. //释放数据
  56. if (this->data != NULL)
  57. {
  58. delete[] this->data;
  59. }
  60. //修正指向
  61. this->data = newData;
  62. }
  63. data[++topIndex] = value;
  64. }
  65. void Stack::Pop()
  66. {
  67. if (IsEmpty())
  68. {
  69. return;
  70. }
  71. --topIndex;
  72. }
  73. int Stack::Top()
  74. {
  75. return this->data[topIndex];
  76. }
  77. bool Stack::IsEmpty()
  78. {
  79. return this->topIndex == -1 ? true : false;
  80. }
  81. int Stack::Size()
  82. {
  83. return topIndex + 1;
  84. }

链式栈

  1. class Node
  2. {
  3. public:
  4. int value;
  5. Node* Next;
  6. Node(int Num) : value(Num), Next(nullptr) {}
  7. };
  8. class Stack
  9. {
  10. public:
  11. Node* Head;
  12. public:
  13. Stack() : Head(nullptr)
  14. {
  15. }
  16. Stack(const Stack& other)
  17. {
  18. if (other.Head == nullptr)
  19. {
  20. Head = nullptr;
  21. }
  22. else
  23. {
  24. Head = new Node(other.Head->value);
  25. Node* head = Head;
  26. Node* node = other.Head->Next;
  27. while (node != nullptr)
  28. {
  29. head->Next = new Node(node->value);
  30. head = head->Next;
  31. node = node->Next;
  32. }
  33. }
  34. }
  35. ~Stack()
  36. {
  37. Clear();
  38. }
  39. public:
  40. void Push(int value)
  41. {
  42. Node* node = new Node(value);
  43. node->Next = Head;
  44. Head = node;
  45. }
  46. void Pop()
  47. {
  48. if (!IsEmpty())
  49. {
  50. Node* temp = Head;
  51. Head = Head->Next;
  52. delete temp;
  53. }
  54. }
  55. int Top()
  56. {
  57. if (!IsEmpty())
  58. {
  59. return Head->value;
  60. }
  61. }
  62. public:
  63. bool IsEmpty()
  64. {
  65. return Head == nullptr;
  66. }
  67. void Clear()
  68. {
  69. while (!IsEmpty())
  70. {
  71. Pop();
  72. }
  73. }
  74. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/798798
推荐阅读
相关标签
  

闽ICP备14008679号