7-22 堆栈模拟队列

设已知有两个堆栈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

解题思路:

首先需要定义两个栈,可以用STL也可以自己写,然后要将大的那个栈作为S1,小的栈作为S2,因为我们需要将S1中的数据全部倒入S2中,所以S2一定要大于等于S1

这道题的操作分为两个部分:

输入:

 S1满S1不满
S2空将S1数据倒入S2并输入数据到S1直接输入数据到S1
S2不空ERROR:Full直接输入数据到S1

 

输出:

 S1空S1不空
S2空ERROR:Empty将S1数据倒入S2并输出S2数据
S2不空输出S2数据输出S2数据

 

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 1010

/*
*	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的栈顶元素。
*/

typedef int ElementType ;
//栈结构体 
typedef struct Stack{
	ElementType s[MAXSIZE];
	int size;
	int top;
}*st;
//栈初始化 
st init(){
	st s = (st)malloc(sizeof(Stack));
	s->top=-1;
	return s;
}

//满了返回1,没满返回0 
int IsFull(st S){
	if(S->top == S->size-1){
		return 1;	//满了 
	} else {
		return 0;
	} 
} 

//空返回1,没空返回0 
int IsEmpty(st S){
	if(S->top == -1){
		return 1;	//空 
	} else {
		return 0;
	}
}

//入栈操作 
void Push(st S, ElementType item){
	S->top++;
	S->s[S->top] = item;
}

//出栈操作
ElementType Pop(st S){
	ElementType e = S->s[S->top];
	S->top--;
	return e;
} 

//将S1的数据倒入S2中 
void change(st S1, st S2){
	while(!IsEmpty(S1)){
		Push(S2, Pop(S1));
	}
}

//两个数据交换 
void swap(int &a, int &b){
	int tmp = a;
	a = b;
	b = tmp;
}


int main(){
	st S1, S2;	//刚刚初始化的时候他们只是空指针 
	S1=init();
	S2=init();
	int n1, n2;
	scanf("%d %d", &n1, &n2);
	if(n1 > n2){		//反正栈S1的容量要小于栈S2的容量 
		swap(n1, n2);
	}
	S1->size=n1;  S2->size=n2;
	char a;
	int tmp;
	while(~scanf("%c", &a)){
		if(a==' '){
			continue;
		}
		if(a=='A'){	//输入 
			scanf("%d", &tmp);
			if(!IsFull(S1)){	//S1不满,输入到S1中 
				Push(S1, tmp);
			} else if(IsFull(S1) && IsEmpty(S2)){	//S1满,S2空,则将S1中数据倒入S2中 
				change(S1, S2);
				Push(S1, tmp);
			} else if(IsFull(S1) && !IsEmpty(S2)){	//S1满,S2不空,则队列满了 
				printf("ERROR:Full\n");
			} 
		}else if(a=='D'){	//输出 
			if(!IsEmpty(S2)){		//S2不空直接输出 
				printf("%d\n", Pop(S2));
			} else if(IsEmpty(S2) && !IsEmpty(S1)){	//S2空且S1不空,则将S1的数据倒入S2中再输出 
				change(S1, S2);
				printf("%d\n", Pop(S2));
			} else if(IsEmpty(S1) && IsEmpty(S2)){	//S1和S2都为空,则队列空了 
				printf("ERROR:Empty\n");
			}
		} else if(a=='T'){
			break;
		}
	}
	return 0;
} 

 

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值