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

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
#include<bits/stdc++.h> 
using namespace std;
stack<int>s1;  
stack<int>s2;  
  
int main()
{  
    int m,n,i,a;  
    char c;  
    cin>>m>>n;  
    if(m>n)
		swap(m,n);  
    while(1)
	{  
        cin>>c;  
        if(c=='T')  
            return 0;
        if(c=='A')
		{  
            cin>>a;  
            if((s1.size()==m)&&(s2.size()!=0))
                cout<<"ERROR:Full"<<endl;  
            else
			{  
                int len1,t;  
                if(s1.size()==m)
				{  
                    len1=s1.size();  
                    while(len1--)
					{  
                        t=s1.top();  
                        s1.pop();  
                        s2.push(t);  
                    }  
                    s1.push(a);  
                }  
                else if(s1.size()!=m)
                    s1.push(a);  
            }  
        }  
        else if(c=='D')
		{  
            if((s1.size()==0)&&(s2.size()==0)) 
                cout<<"ERROR:Empty"<<endl;   
            else
			{  
                int len2;  
                if(s2.size()==0)
				{  
                    len2=s1.size();  
                    while(len2--)
					{  
                        int t=s1.top();  
                        s1.pop();  
                        s2.push(t);  
                    }  
                    cout<<s2.top()<<endl;  
                    s2.pop();  
                }  
                else if(s2.size()!=0)
				{  
                    cout<<s2.top()<<endl;  
                    s2.pop();  
                }  
            }  
        }  
    }  
    return 0;  
}
说一下这道题的解题思路,用c++堆栈模拟队列,输入两个堆栈的长度,长度短的作为输入栈,长的作为输出栈,如果输入栈满了但输出栈不空,此时不能再输入了,为栈满,其他情况都可以输入,如果输入栈满了,输出栈为空,就可以将输入栈的元素转移到输出栈,再向输入栈添加元素,如此就可以实现模拟队列的功能
后来我用c++队列做了一下,只能过一个样例,模拟队列和真实队列还是有本质的区别的
#include<bits/stdc++.h> 
using namespace std;
queue<int> q; 
  
int main()
{  
    int m,n,i,a;  
    char c;  
    cin>>m>>n;  
    n+=m;	
    for(i=0;;i++)
	{  
        cin>>c;  
        if(c=='T')  
            return 0;
        if(c=='A')
		{  
            cin>>a;  
            if(q.size()==n)
			{
                cout<<"ERROR:Full"<<endl;  
            }  
            else
			{  
                q.push(a);  
            }  
        }  
        else if(c=='D')
		{  
            if(q.size()==0)
			{  
                cout<<"ERROR:Empty"<<endl;  
            }  
            else
			{  
                cout<<q.front()<<endl;  
                q.pop();   
            }  
        }  
    }  
    return 0;  
}

猜你喜欢

转载自blog.csdn.net/hebau_pss/article/details/79368721