顺序栈-链式栈的实现

「爱情、让人受尽委屈。」 2024-04-06 13:57 168阅读 0赞
  1. /*
  2. * 顺序栈的实现
  3. */
  4. #include<iostream>
  5. using namespace std;
  6. const int Maxsize = 10;
  7. //template<class T>
  8. //struct Node {
  9. // T data[Maxsize];
  10. // Node<T>* next;
  11. //};
  12. template<class T>
  13. class SeqStack {
  14. private:
  15. T date[Maxsize];
  16. int top;
  17. public:
  18. SeqStack();
  19. ~SeqStack();
  20. void Push(T x);
  21. T Pop();
  22. T GetTop();
  23. int empty();
  24. };
  25. //初始化栈
  26. template<class T>
  27. SeqStack<T>::SeqStack() {
  28. top = -1;
  29. }
  30. template<class T>
  31. SeqStack<T>::~SeqStack() {}
  32. //判断栈是否为空
  33. template<class T>
  34. int SeqStack<T>:: empty() {
  35. if (top == -1)
  36. throw"空栈!";
  37. return 1;
  38. }
  39. //入栈
  40. template<class T>
  41. void SeqStack<T>::Push(T x) {
  42. if (top == Maxsize - 1)
  43. throw"上溢!";
  44. top+=1;
  45. data[top] = x;
  46. /*data[++top] = x;*/
  47. }
  48. //出栈
  49. template<class T>
  50. T SeqStack<T>::Pop() {
  51. T x;
  52. if (top == -1)
  53. throw"下溢!";
  54. //x = data[top--];
  55. return x;
  56. }
  57. //取栈顶元素
  58. template<class T>
  59. T SeqStack<T>::GetTop() {
  60. return data[top];
  61. }
  62. int mian()
  63. {
  64. int x;
  65. SeqStack<int> s;
  66. cout << "对15和20进行入栈操作:" << endl;
  67. s.Push(15);
  68. cout << "当前的栈顶元素为:" << s.GetTop() << endl;
  69. s.Push(20);
  70. cout << "当前的栈顶元素为:" << s.GetTop()<< endl;
  71. try {
  72. x=s.Pop();
  73. cout << "执行一次出栈,删除元素:" << x << endl;
  74. }
  75. catch (char* str) { cout << str << endl; }
  76. try {
  77. cout << "请输入一个要入栈的数:";
  78. cin >> x;
  79. s.Push(x);
  80. cout << "执行一次入栈,栈顶元素:" << s.GetTop()<< endl;
  81. }
  82. catch (char* str) { cout << str << endl; }
  83. return 0;
  84. }

链式栈

  1. #include<iostream>
  2. using namespace std;
  3. struct Node {
  4. int data;
  5. Node* next;
  6. };
  7. class LinkStack {
  8. private:
  9. Node* top;
  10. public:
  11. LinkStack();
  12. ~LinkStack();
  13. void Push(int x);
  14. int Pop();
  15. int GetTop();
  16. int Empty();
  17. };
  18. LinkStack::LinkStack() {
  19. top = new Node;
  20. top->next = nullptr;
  21. }
  22. LinkStack::~LinkStack() {
  23. Node* p = top;
  24. while (top != nullptr) {
  25. top = top->next;
  26. delete p;
  27. p = top;
  28. }
  29. }
  30. void LinkStack::Push(int x) {
  31. Node* s = nullptr;
  32. s = new Node;
  33. s->data = x;
  34. s->next = top;
  35. top = s;//将结点s插在栈顶
  36. }
  37. int LinkStack::Pop() {
  38. Node* p = nullptr;
  39. int x;
  40. if (top == nullptr) {
  41. cout << "下溢!" << endl;
  42. return 0;
  43. }
  44. x = top->data; p = top;
  45. top = top->next;//将栈顶结点摘链
  46. delete p;
  47. return x;
  48. }
  49. int LinkStack::GetTop() {
  50. return top->data;
  51. }
  52. int LinkStack::Empty() {
  53. if (top == nullptr) {
  54. return 0;
  55. }
  56. }
  57. int mian() {
  58. int x;
  59. LinkStack s{};
  60. cout << "入栈:" << endl;
  61. s.Push(20);
  62. s.GetTop();
  63. try {
  64. x = s.Pop();
  65. cout << "出栈一个元素:" << x;
  66. cout << endl;
  67. }
  68. catch (char* str) { cout << str; }
  69. try {
  70. cout << "请输入插入的元素:";
  71. cin >> x;
  72. s.Push(x);
  73. }
  74. catch (char* str) { cout << str; }
  75. if (s.Empty() == 0) {
  76. cout << "空栈!" << endl;
  77. }
  78. else {
  79. cout << "非空!" << endl;
  80. }
  81. return 0;
  82. }

发表评论

表情:
评论列表 (有 0 条评论,168人围观)

还没有评论,来说两句吧...

相关阅读