当前位置:   article > 正文

【C++】栈和队列(STL)_c++stl栈和队列

c++stl栈和队列

链栈,共享栈,循环队列和链队列的基本实现。

链栈:

  1. #pragma once
  2. //定义节点
  3. template<class T>
  4. struct Node {
  5. T data;
  6. struct Node<T>* next;
  7. };
  8. //链栈模板类
  9. template<class T>
  10. class LinkStack {
  11. public:
  12. LinkStack() { top = NULL; }
  13. ~LinkStack();
  14. void Push(T x);//入栈
  15. T Pop();//出栈
  16. T GetTop();//得到栈顶元素
  17. bool Empty() { return (top == NULL) ? true : false;; }//判断是否为空
  18. private:
  19. struct Node<T>* top;
  20. };
  21. template<class T>
  22. LinkStack<T>::~LinkStack() {//析构函数
  23. while (top) {
  24. struct Node<T>* p = top;//工作指针
  25. top = top->next;
  26. delete[] p;
  27. }
  28. }
  29. template<class T>
  30. void LinkStack<T>::Push(T x) {//入栈操作
  31. struct Node<T>* p = new Node<T>;
  32. p->data = x;
  33. p->next = top;
  34. top = p;
  35. }
  36. template<class T>
  37. T LinkStack<T>::Pop() {//出栈操作
  38. if (Empty())throw"下溢";
  39. T x = top->data;
  40. struct Node<T>* p = top;
  41. top = top->next;
  42. delete[] p;
  43. return x;
  44. }
  45. template<class T>
  46. T LinkStack<T>::GetTop() {//查找栈顶元素
  47. if (Empty())throw"栈空";
  48. return top->data;
  49. }

共享栈:

  1. #pragma once
  2. //共享栈模板类
  3. const int STACKSIZE = 1024;//共享栈的最大空间
  4. template<class T>
  5. class SharedStack{
  6. public:
  7. SharedStack();
  8. void Push1(T x);//栈1入栈
  9. void Push2(T x);//栈2入栈
  10. T Pop1();//栈1出栈
  11. T Pop2();//栈2出栈
  12. T GetTop1();//查找栈顶元素1
  13. T GetTop2();//查找栈顶元素2
  14. bool Empty1();//判断栈1是否为空
  15. bool Empty2();//判断栈2是否为空
  16. private:
  17. int top1;//栈1指针
  18. int top2;//栈2指针
  19. T data[STACKSIZE];//定义栈的大小
  20. };
  21. template<class T>
  22. SharedStack<T>::SharedStack() {//初始化空栈
  23. top1 = -1;
  24. top2 = STACKSIZE;
  25. }
  26. template<class T>
  27. void SharedStack<T>::Push1(T x) {//栈1入栈
  28. if (top1 + 1 >= top2)throw"栈1上溢";
  29. top1++;
  30. data[top1] = x;
  31. }
  32. template<class T>
  33. void SharedStack<T>::Push2(T x) {//栈2入栈
  34. if (top2 <= top1 + 1)throw"栈2上溢";
  35. top2--;
  36. data[top2] = x;
  37. }
  38. template<class T>
  39. T SharedStack<T>::Pop1() {//栈1出栈
  40. if (Empty1())throw"栈1下溢";
  41. top1--;
  42. return data[top1 + 1];
  43. }
  44. template<class T>
  45. T SharedStack<T>::Pop2() {//栈2出栈
  46. if (Empty2())throw"栈2下溢";
  47. top2++;
  48. return data[top2 - 1];
  49. }
  50. template<class T>
  51. T SharedStack<T>::GetTop1() {//get栈1顶端元素
  52. if (top1 != -1)
  53. return data[top];
  54. else throw"栈1空";
  55. }
  56. template<class T>
  57. T SharedStack<T>::GetTop2() {//get栈2顶端元素
  58. if (top2 != STACKSIZE)
  59. return data[top2];
  60. else throw"栈2空";
  61. }
  62. template<class T>
  63. bool SharedStack<T>::Empty1() {//判断栈1是否为空
  64. if (top1 == -1)
  65. return true;
  66. return false;
  67. }
  68. template<class T>
  69. bool SharedStack<T>::Empty2() {//判断栈2是否为空
  70. if (top2 == STACKSIZE)
  71. return true;
  72. return false;
  73. }

循环队列:

  1. #pragma once
  2. const int QUEUESIZE = 1000;
  3. //循环队列模板类
  4. template<class T>
  5. class LoopQueue {
  6. public:
  7. LoopQueue() { front = rear = 0; }//构造函数
  8. void EnterQueue(T x);//入队
  9. T OutQueue();//出队
  10. T GetFront();
  11. int GetLength();
  12. bool Empty() { return front == rear ? true : false; }
  13. private:
  14. int front;
  15. int rear;
  16. T data[QUEUESIZE];
  17. };
  18. template<class T>
  19. void LoopQueue<T>::EnterQueue(T x){//入队
  20. if ((rear + 1) % QUEUESIZE == front)throw"队满";
  21. rear = (rear + 1) % QUEUESIZE;
  22. data[rear] = x;
  23. }
  24. template <class T>
  25. T LoopQueue<T>::OutQueue() {//出队
  26. if (rear == front)throw"队空";
  27. front = (front + 1) % QUEUESIZE;
  28. return data[front];
  29. }
  30. template<class T>
  31. T LoopQueue<T>::GetFront() {//查找队头元素
  32. if (rear == front)throw"队空";
  33. return data[(front + 1) % QUEUESIZE];
  34. }
  35. template<class T>
  36. int LoopQueue<T>::GetLength() {
  37. return (QUEUESIZE + rear - front) % QUEUESIZE;
  38. }

链队列:

  1. #pragma once
  2. //链队列目标 类
  3. template<class T>
  4. class LinkQueue {
  5. public:
  6. LinkQueue() {//构造函数
  7. front = rear = new Node<T>;//初始化指针
  8. front->next = NULL;
  9. }
  10. ~LinkQueue();
  11. void EnterQueue(T x);
  12. T OutQueue();
  13. T GetFront();
  14. bool Empty() { return front == rear ? true : false; }
  15. private:
  16. struct Node<T>* front;
  17. struct Node<T>* rear;
  18. };
  19. template<class T>
  20. void LinkQueue<T>::EnterQueue(T x) {//入队
  21. rear->next = new Node<T>;
  22. rear = rear->next;
  23. rear->data = x;
  24. rear->next = NULL;
  25. }
  26. template<class T>
  27. T LinkQueue<T>::OutQueue() {//出队
  28. struct Node<T> * p = front->next;//保存队头元素指针
  29. if (!p)throw"队空";
  30. front->next = p->next;//队头元素出队
  31. T x=p->data;//保存队头元素数据
  32. delete[] p;//释放队头元素
  33. return x;//返回出队元素
  34. }
  35. template<class T>
  36. T LinkQueue<T>::GetFront() {//查找队头元素
  37. if (!front->next)throw"队空";
  38. return front->next->data;
  39. }
  40. template<class T>
  41. LinkQueue<T>::~LinkQueue() {//析构函数
  42. while (front) {
  43. rear = front->next;
  44. delete[] front;
  45. front = rear;
  46. }
  47. }

测试:

  1. // 实验1-栈和队列.cpp: 定义控制台应用程序的入口点。
  2. //测试main()函数
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include"SharedStack.h"
  6. #include"LinkStack.h"
  7. #include"LoopQueue.h"
  8. #include"LinkQueue.h"
  9. using namespace std;
  10. int main()
  11. {
  12. //测试共享栈
  13. cout << "测试共享栈:" << endl;
  14. cout << "请输入两个数(int)分别入栈1和栈2:" << endl;
  15. int test1, test2;
  16. cin >> test1 >> test2;
  17. SharedStack<int> s1;
  18. s1.Push1(test1);//栈1入栈
  19. s1.Push2(test2);//栈2入栈
  20. cout << "出栈结果:"<<s1.Pop1() << " " << s1.Pop2()<<endl<<endl;//栈1 栈2 出栈
  21. //测试链栈
  22. cout << "测试链栈:" << endl;
  23. cout << "输入两个入栈数据(int):" << endl;
  24. int test3, test4;
  25. cin >> test3 >> test4;
  26. LinkStack<int> s2;
  27. s2.Push(test3);
  28. s2.Push(test4);
  29. cout << "栈顶元素为:" << s2.GetTop() << endl;
  30. cout << "出栈结果:" << s2.Pop() << " " << s2.Pop() << endl<<endl;//出栈
  31. //测试循环队列
  32. cout << "测试循环队列:" << endl;
  33. cout << "输入两个int数据入队:" << endl;
  34. int test5, test6;
  35. cin >> test5 >> test6;
  36. LoopQueue<int> s3;
  37. s3.EnterQueue(test5);
  38. s3.EnterQueue(test6);
  39. cout << "测试队列长度:" << s3.GetLength() << endl;
  40. cout << "测试队头元素:" << s3.GetFront() << endl;
  41. cout << "出栈结果:" << s3.OutQueue() << " " << s3.OutQueue() << endl << endl;
  42. //测试链队列
  43. cout << "测试链队列:" << endl;
  44. cout << "输入两个int数据入队:" << endl;
  45. int test7, test8;
  46. cin >> test7 >> test8;
  47. LinkQueue<int> s4;
  48. s4.EnterQueue(test7);
  49. s4.EnterQueue(test8);
  50. cout << "测试队头元素:" << s4.GetFront() << endl;
  51. cout << "出队结果为:" << s4.OutQueue() << " " << s4.OutQueue() << endl;
  52. return 0;
  53. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/970401
推荐阅读
相关标签
  

闽ICP备14008679号