题目描述
设已知有两个堆栈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()。
请在下面的不完整程序中begin和end之间将DeleteQ()函数定义完整并提交通过。
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int *data;
int top;
int maxsize;
};
typedef struct stack * Stack;
void InitStack(Stack &S, int n)
{
S = (Stack)malloc(sizeof(struct stack));
S->data = (int *)malloc( n * sizeof(int));
S->top = -1;
S->maxsize = n;
}
void push(Stack S, int item)
{
S->top++;
S->data[S->top] = item;
}
int pop(Stack S)
{
return S->data[S->top--];
}
int IsFull(Stack S)
{
if (S->top == S->maxsize - 1)
return 1;
else
return 0;
}
int IsEmpty(Stack S)
{
if (S->top == -1)
return 1;
else
return 0;
}
void AddQ(int item, Stack S1, Stack S2)
{
int n;
if (IsFull(S1)==0)
{
push(S1, item);
}
else
{
if (IsEmpty(S2))
{
while (IsEmpty(S1) == 0)
{
n = pop(S1);
push(S2, n);
}
push(S1, item);
}
else
{
printf("ERROR:Full\n");
}
}
}
void DeleteQ(Stack S1, Stack S2)
{
/************** begin **************/
/************** end **************/
}
int main()
{
int n1, n2;
int num1, num2;
scanf("%d %d", &num1, &num2);
n1=num1<num2?num1:num2;
n2=num1<num2?num2:num1;
Stack S1,S2;
InitStack(S1,n1);
InitStack(S2,n2);
char c;
int d;
scanf("%c", &c);
while (c != 'T')
{
switch (c)
{
case 'A':
scanf("%d", &d);
AddQ(d, S1, S2);
break;
case 'D':
DeleteQ(S1, S2);
break;
}
scanf("%c", &c);
}
return 0;
}
输入
输入首先给出两个正整数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
提示
解题思路:
这道题是用两个堆栈实现队列。栈遵循先进后出,队列遵循先进先出。两个堆栈一个负责输出,另一个负责输入。
把这两个栈S1和S2中最大容量较小的那个当成输入的栈S1,另外一个当”缓冲区“栈S2,数据从S2输出。
刚开始数据都进入S1,如果S1满了且S2为空,将S1中所有数据转入S2中;数据从S2中出来。实现先进先出。
记最大容量较小的栈为S1,最大容量较大的栈为S2,输入输出均分三种考虑的情况:
1、输入时:
1)S1没满,S2为空,数据输入到S1中。
2)S1满了,S2为空,将S1中的数据转入S2中,再输入数据到S1中。
3)S1满了,S2不为空(不要求S2满了),输出ERROR:Full。
2、输出时:
1)S2不是空的,直接输出S2.top()。
2)S2是空的,S1不是空的,将S1中的元素转入S2中,再输出S2.top()。
3)S2是空的,S1也是空的,输出ERROR:Empty。
代码实现
int item;
if(IsEmpty(S2)==0) //s2是是空的,直接输出
{
printf("%d\n",pop(S2));
}
else //s2是空的
{
if (IsEmpty(S1)==0) //s1不是空的
{
while (IsEmpty(S1) == 0) //将S1中的元素转入S2中,再输出S2.top()
{
item = pop(S1);
push(S2, item);
}
printf("%d\n",pop(S2));
}
else //s1,s2,都是空的
{
printf("ERROR:Empty\n");
}
}
整体代码
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int *data;
int top;
int maxsize;
};
typedef struct stack * Stack;
void InitStack(Stack &S, int n)
{
S = (Stack)malloc(sizeof(struct stack));
S->data = (int *)malloc( n * sizeof(int));
S->top = -1;
S->maxsize = n;
}
void push(Stack S, int item) //将元素item压入堆栈S
{
S->top++;
S->data[S->top] = item;
}
int pop(Stack S) //删除并返回S的栈顶元素
{
return S->data[S->top--];
}
int IsFull(Stack S) //判断堆栈S是否已满,返回1或0
{
if (S->top == S->maxsize - 1)
return 1;
else
return 0;
}
int IsEmpty(Stack S) //判断堆栈S是否为空,返回1或0
{
if (S->top == -1)
return 1;
else
return 0;
}
void AddQ(int item, Stack S1, Stack S2)
{
int n;
if (IsFull(S1)==0) //S1没满,S2为空,数据输入到S1中
{
push(S1, item);
}
else //S1满了
{
if (IsEmpty(S2)) //S2为空,将S1中的数据转入S2中
{
while (IsEmpty(S1) == 0)
{
n = pop(S1);
push(S2, n);
}
push(S1, item); //再输入数据到S1中
}
else //S1满了,S2不为空(不要求S2满了)
{
printf("ERROR:Full\n");
}
}
}
void DeleteQ(Stack S1, Stack S2) //出队
{
/************** begin **************/
///
int item;
if(IsEmpty(S2)==0) //s2是是空的,直接输出
{
printf("%d\n",pop(S2));
}
else //s2是空的
{
if (IsEmpty(S1)==0) //s1不是空的
{
while (IsEmpty(S1) == 0) //将S1中的元素转入S2中,再输出S2.top()
{
item = pop(S1);
push(S2, item);
}
printf("%d\n",pop(S2));
}
else //s1,s2,都是空的
{
printf("ERROR:Empty\n");
}
}
///
/************** end **************/
}
int main()
{
int n1, n2;
int num1, num2;
scanf("%d %d", &num1, &num2);
n1=num1<num2?num1:num2;
n2=num1<num2?num2:num1;
Stack S1,S2;
InitStack(S1,n1);
InitStack(S2,n2);
char c;
int d;
scanf("%c", &c);
while (c != 'T')
{
switch (c)
{
case 'A':
scanf("%d", &d);
AddQ(d, S1, S2);
break;
case 'D':
DeleteQ(S1, S2);
break;
}
scanf("%c", &c);
}
return 0;
}