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

7-4 堆栈模拟队列 (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()。

输入格式:
输入首先给出两个正整数N1和N2,表示堆栈S1和S2的最大容量。随后给出一系列的队列操作: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

这道题我一开始用的stl,后来遇到重题了,就用数组模拟了一遍,发现用stl真的是很简单。下面分别附上stl和非stl的方法。

//stl实现
#include <bits/stdc++.h>
using namespace std;
int main()
{
	stack<int>s1,s2;
	int m,n,t;
	cin>>m>>n;
	if (n>m)
	{
		t=m;
		m=n;
		n=t;
	}      //s2 smaller
	char c;
	int num;
	getchar();
	while (1)
	{
		scanf("%c",&c);        //A 1 A 2 A 3 A 4 A 5 D A 6 D A 7 D A 8 D D D D T
		if (c=='T')
		{
			break;
		}
		if (s2.size()==n&&s1.empty())
		{
			while(!s2.empty())
			{
				s1.push(s2.top());
				s2.pop();
			}
		}
		if (c=='A'&&s2.size()!=n)
		{
			scanf("%d ",&num);
			s2.push(num);
		}
		else if (c=='A'&&s2.size()==n)
		{
			scanf("%d",&num);
			printf("ERROR:Full\n");
		}
		if (c=='D'&&s1.empty())
		{
			printf("ERROR:Empty\n");
		}
		else if (c=='D'&&!s1.empty())
		{
			printf("%d\n",s1.top());
			s1.pop();
		}
	}
	return 0;
}

//数组构建栈模拟
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n1,n2,top1=-1,top2=-1,t;
	cin>>n1>>n2;        //n2是比较小的
	if (n2>n1)
	{
		t=n1;
		n1=n2;
		n2=t;
	}
	getchar();
	int s1[100],s2[100];         //3 2
	char c;							//A 1 A 2 A 3 A 4 A 5 D A 6 D A 7 D A 8 D D D D T
	int x;
	scanf("%c",&c);
	while (c!='T')
	{
		if (c=='A')
		{
			if(top2<n2-1)      //如果短的2没满,一直填充到2.
			{
				scanf("%d",&x);
				s2[++top2]=x;
				getchar();
			}
			else if (top2==n2-1&&top1!=-1)      //如果2满了但是1不为空,此时无法进行数据移动,输出FULL. 
			{
				scanf("%d",&x);
				getchar();
				printf("ERROR:Full\n");
			}
			else if (top2==n2-1&&top1==-1)    //如果2满了但是1是空的,数据移动,全部移过去. 
			{
				while (top2!=-1)
				{
					s1[++top1]=s2[top2--];
				}
				scanf("%d",&x);
				s2[++top2]=x;
				getchar();
			}
		}
		if (c=='D')
		{
			getchar();
			if (top1!=-1)                    //如果1不是空的,先输出1,因为他的数输入的更早. 
			{
				printf("%d\n",s1[top1--]);
			}
			else if (top2!=-1&&top1==-1)          //如果2不是空的但是1是空的,转换数据,再输出. 
			{
				while (top2!=-1)
				{
					s1[++top1]=s2[top2--];
				}
				printf("%d\n",s1[top1--]);
			}
			else if (top1==-1&&top2==-1)         //都是空的,输出错误提示. 
			{
				printf("ERROR:Empty\n");
			}
		}
		scanf("%c",&c);
	}
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值