用两个栈模拟一个队列
来源:互联网 发布:java 线程间通信 编辑:程序博客网 时间:2024/05/17 01:11
思路:
让栈A提供入队功能,栈B提供出队功能。
入队列:栈A。
出队列:如果栈B不为空,直接弹出栈B的栈顶数据;如果栈B为空,则依次弹出栈A的数据,放入栈B中,再弹出栈B的栈顶数据。
方法一:
#include<iostream>#include<stack>#include<string>using namespace std; template<class T>struct MyQueue{ void Enqueue(T t) { s1.push(t); } T Dequeue() { if(s2.empty()) { if(0 == s1.size()) { exit(1); } while(!s1.empty()) { s2.push(s1.top()); s1.pop(); } } T retVal = s2.top(); s2.pop(); return retVal; } stack<T> s1; stack<T> s2;}; intmain(){ MyQueue<int> mq; int begin = 0; int end = 10; int i; for(i = begin; i < end; i++) { mq.Enqueue(i); } for(i = begin; i < end; i++) { cout<<mq.Dequeue()<<' '; } cout<<endl; MyQueue<float> mq2; mq2.Enqueue(1.1f); mq2.Enqueue(2.2f); cout << mq2.Dequeue() << endl; MyQueue<string> mq3; string s1 = "hello"; string s2 = "world"; mq3.Enqueue(s1); mq3.Enqueue(s2); cout << mq3.Dequeue() << endl; return 0;}运行结果:
01 2 3 4 5 6 7 8 9
1.1
hello
Pressany key to continue
方法二:
#include <iostream>using namespace std;struct Node{public:Node(int i):data(i),next(NULL){}int data;Node *next;};class Stack{public:Stack():top(NULL){}void Push(Node node);//入栈int Pop();//出栈bool IsEmpty();//是否为空void Print();//打印所有元素private:Node *top;};void Stack::Push(Node node){Node *n = new Node(node.data);n->next = top;top = n;}int Stack::Pop(){if (IsEmpty()){cout << "Stack Empty!" << endl;exit(1);}else{int topVal = top->data;Node *temp = top;top = top->next;delete temp;return topVal;}}bool Stack::IsEmpty(){if (NULL == top){return 1;}else{return 0;}}void Stack::Print(){if(IsEmpty()){cout << "Inner Stack Empty!" << endl;}else{Node *node = top;while (node != NULL){cout << node->data << " ";node = node->next;}cout << endl;}}class Queue{public:void Enqueue(Node node);//入队int Dequeue();//出队bool IsEmpty();//是否为空private:Stack s1;//用于入队Stack s2;//用于出队};void Queue::Enqueue(Node node){s1.Push(node);//只对s1进行进栈操作cout << "Enqueue " << node.data << endl;}int Queue::Dequeue(){if(s2.IsEmpty()){if(s1.IsEmpty())//s2和s1都为空{cout << "Queue Empty! Can not be dequeued again!" << endl;exit(1);}else//s2为空s1不为空,把s1的所有元素都倒入s2中{while(!s1.IsEmpty()){int i = s1.Pop();s2.Push(Node(i));}}}return s2.Pop();}bool Queue::IsEmpty(){if (s1.IsEmpty() && s2.IsEmpty()){return 1;}else{return 0;}}int main(){Queue q;q.Enqueue(Node(1));q.Enqueue(Node(2));q.Enqueue(Node(3));cout << "Dequeue " << q.Dequeue() << endl;q.Enqueue(Node(4));cout << "Dequeue " << q.Dequeue() << endl;cout << "Dequeue " << q.Dequeue() << endl;cout << "Dequeue " << q.Dequeue() << endl;return 0;}
运行结果:
Enqueue1
Enqueue2
Enqueue3
Dequeue1
Enqueue4
Dequeue2
Dequeue3
Dequeue4
Pressany key to continue
0 0
- 用两个栈模拟一个队列
- 用两个栈模拟一个队列
- 用两个队列模拟一个栈
- 用两个栈模拟一个队列
- 用两个队列模拟一个栈
- 用两个栈模拟一个队列
- 用两个栈模拟一个队列
- 两个栈模拟一个队列/两个队列模拟一个栈
- 两个栈模拟一个队列
- 两个队列模拟一个栈
- 两个队列模拟一个栈
- 两个队列模拟一个栈
- 两个栈模拟一个队列
- 两个队列模拟一个栈
- 两个栈模拟一个队列
- 两个队列模拟一个栈
- 两个栈模拟一个队列
- 两个栈模拟一个队列
- 用Modelsim SE 10.1a进行仿真的大致步骤
- 优先队列:priority_queue
- linux学习之c语言swap函数
- C++ 17 的最新动态
- Redis 入门学习时不理解的问题
- 用两个栈模拟一个队列
- eclipse 快捷键
- pat1001:害死人不偿命的(3n+1)猜想
- MySQL启动远程连接
- Round robin
- 黑马程序员,未来的星星大牛的 基础java
- uva10602 - Editor Nottoobad
- Python内存管理机制
- 初次使用Android Studio的配置问题