#include"Stack.h"
#include<iostream>
using namespace std;
//此处实现三个队列运算Enqueue,Dequeue,QueueEmpty
//使用Stack.h中的栈操作实现队列运算
//自己设计参数
/************Begin*************/
int Enqueue(Stack &S1,Stack &S2,int &x)
{
if(!StackOverflow(S1)){
Push(S1,x);
return 1;
}
if(StackOverflow(S1)&&StackEmpty(S2)){
while(!StackEmpty(S1)){
Pop(S1,x);
Push(S2,x);
}
}
Push(S1,x);
return 1;
}
void DeQueue(Stack &S1,Stack &S2,int &x)
{
if(!StackEmpty(S2))
Pop(S2,x);
else if(StackEmpty(S1))
printf("队列空\n");
else{
while(!StackEmpty(S1))
{
Pop(S1,x);
Push(S2,x);
} //while
Pop(S2,x);
} //else
}
int QueueEmpty(Stack S1,Stack S2)
{
if(StackEmpty(S1)&&StackEmpty(S2))
return 1;
else
return 0;
}
/************End**************/
int main()
{
Stack S1,S2;
InitStack(S1);
InitStack(S2);
int n,x;
char op;
cin>>n;
/************Begin*************/
while(n--)
{
cin>>op;
if(op=='I')
{
cin>>x;
Enqueue(S1,S2,x);
}
else
{
DeQueue(S1,S2,x);
}
}
while(!QueueEmpty(S1,S2))
{
DeQueue(S1,S2,x);
cout<<x<<' ';
}
/************End**************/
return 0;
}
第3关:两栈模拟一个队列
最新推荐文章于 2024-07-12 18:56:03 发布