堆栈模拟队列

7-22 堆栈模拟队列 (25 分)

设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q。

所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数:

  • int IsFull(Stack S):判断堆栈S是否已满,返回1或0;
  • int IsEmpty (Stack S ):判断堆栈S是否为空,返回1或0;
  • void Push(Stack S, ElementType item ):将元素item压入堆栈S
  • ElementType Pop(Stack S ):删除并返回S的栈顶元素。

实现队列的操作,即入队void AddQ(ElementType item)和出队ElementType DeleteQ()

输入格式:

输入首先给出两个正整数N1N2,表示堆栈S1S2的最大容量。随后给出一系列的队列操作:A item表示将item入列(这里假设item为整型数字);D表示出队操作;T表示输入结束。

输出格式:

对输入中的每个D操作,输出相应出队的数字,或者错误信息ERROR:Empty。如果入队操作无法执行,也需要输出ERROR:Full。每个输出占1行。

输入样例:

3 2
A 1 A 2 A 3 A 4 A 5 D A 6 D A 7 D A 8 D D D D T

输出样例:

ERROR:Full
1
ERROR:Full
2
3
4
7
8
ERROR:Empty

 

 

 

 

 1 #include <iostream>
 2 #include <stack>
 3 using namespace std;
 4 class myQueue{
 5 private:
 6     int Max1, Max2;
 7     bool flag;
 8     stack<int> st1;
 9     stack<int> st2;
10 public:
11     myQueue(int max1, int max2){
12         flag = max1 > max2 ? true : false; 
13         Max1 = max1;
14         Max2 = max2;
15     }
16     void Push(int X){
17         if(flag){
18             //此时栈1为出栈,栈2为入栈 
19             if(st2.size() == Max2 && !st1.empty()){//栈2满,栈1不为空 
20                 cout << "ERROR:Full" << endl;
21             }else if(st2.size() == Max2){//栈2满,栈1空 
22                 while(!st2.empty()){//将栈2全部导入栈1 
23                     st1.push(st2.top());
24                     st2.pop();
25                 }
26                 st2.push(X);//将X加入栈2    
27             }else{//不满足上面俩种情况,直接入栈1 
28                 st2.push(X);
29             }
30         }else{
31             //此时栈2为出栈,栈1为入栈 
32             if(st1.size() == Max1 && !st2.empty()){//栈1满,栈2不为空 
33                 cout << "ERROR:Full" << endl;
34             }else if(st1.size() == Max1){//栈1满,栈2空 
35                 while(!st1.empty()){//将栈1全部导入栈2 
36                     st2.push(st1.top());
37                     st1.pop();
38                 }
39                 st1.push(X);//此时可以入栈1    
40             }else{
41                 st1.push(X);//直接入栈1 
42             }
43         }
44     }
45     void Pop(){ 
46         if(flag){
47             //栈1为出栈,栈2为入栈 
48             if(st1.empty() && st2.empty()){//栈1栈2都为空 
49                 cout << "ERROR:Empty";
50             
51             }else if(st1.empty()){//栈1为空,栈2不为空 
52                     while(!st2.empty()){//将栈2中的元素直接导入栈1 
53                         st1.push(st2.top());
54                         st2.pop();
55                     }
56                     cout << st1.top();//弹出栈1中的栈顶元素 
57                     st1.pop();
58             }else{
59                 cout << st1.top();//不是上面俩种情况就直接弹出栈1 
60                 st1.pop();
61             }
62         }else{//栈2为出栈,栈1为入栈 
63             if(st1.empty() && st2.empty()){
64                 cout << "ERROR:Empty";
65             
66             }else if(st2.empty()){
67                     while(!st1.empty()){//将栈1元素全部导入栈2 
68                         st2.push(st1.top());
69                         st1.pop();
70                     }
71                     cout << st2.top();//弹出栈2栈顶元素 
72                     st2.pop();
73             }else{
74                 cout << st2.top();//不是上面两种情况直接弹出栈1栈顶元素 
75                 st2.pop();
76             }
77         }    
78     }    
79 };
80 int main(){
81     int N1, N2;
82     cin >> N1 >> N2;//输入两个栈的容量 
83     myQueue q(N1, N2);//构造用栈构造的队列 
84     char Op;
85     do{
86         
87         int X;
88         cin >> Op;//读入操作 
89 
90         switch(Op){
91             case 'A': cin >> X; q.Push(X);
92             break;
93             case 'D': q.Pop(); cout << endl;
94         } 
95     }while(Op != 'T');
96     return 0;
97 } 

 

posted @ 2019-03-05 09:40  kakaluotedj  阅读(448)  评论(0)    收藏  举报