基础实验3-2.5 堆栈模拟队列

设已知有两个堆栈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 <stdio.h>
#include <stdlib.h>

#define MAXN 1000

typedef enum{ false, true } bool;
typedef int ElementType;

/*-----堆栈的定义 -----*/ 
typedef int Position;
typedef struct SNode *PtrToSNode;
struct SNode {
	ElementType *Data;
	Position Top;
	int MaxSize;
};
typedef PtrToSNode Stack;

Stack CreateStack( int MaxSize );
bool IsEmpty( Stack S );
bool IsFull( Stack S );
bool Push( Stack S, ElementType X );
ElementType Pop( Stack S );
ElementType Peek( Stack S );
void Clear( Stack S );
/*-----堆栈的定义结束 -----*/
//------队列操作-------// 
int addQ(Stack stack1, Stack stack2, int data);
void deleteQ(Stack stack1,Stack stack2);

int main()
{

    int n1,n2;
    char op;
    Stack stack1,stack2;
    scanf("%d%d",&n1,&n2);
    if(n1>n2){
    	int tmp=n1;
    	n1=n2;
    	n2=tmp;
	}
    stack1 = CreateStack(n1);
    stack2 = CreateStack(n2);

    getchar();
    scanf("%c", &op);

    while (op != 'T') {
        // 缓冲一下输入的空格
        getchar();
        if (op == 'A') {
            int data;
            scanf("%d", &data);
            getchar();
            if (!addQ(stack1, stack2, data))
                printf("ERROR:Full\n");
        } else if (op == 'D') {
            // 栈2空的话从栈1进货
            deleteQ(stack1,stack2);
            
        }
        scanf("%c", &op);
    }
    
    return 0;
}
int addQ(Stack stack1, Stack stack2, int data) {
    if (!Push(stack1, data)) {
        // 错误:入栈1失败,说明栈1满,接下来判断是否可以把栈1的元素压入栈2
        if (IsEmpty(stack2)) {
            while (!IsEmpty(stack1)) {
                Push(stack2, Pop(stack1));
            }
            Push(stack1, data);
        } else {
            return 0;
        }
    }
    return 1;
}
// 是否可以出队还需另外判断,只出队。
void deleteQ(Stack stack1,Stack stack2) {
	if (IsEmpty(stack2)) {
        if (IsEmpty(stack1))
            printf("ERROR:Empty\n");
        else {
            while (!IsEmpty(stack1))
                Push(stack2, Pop(stack1));
        }
    }
    if (!IsEmpty(stack1) || !IsEmpty(stack2))
        printf("%d\n", Pop(stack2));
}

Stack CreateStack( int MaxSize )
{
	Stack S = (Stack)malloc(sizeof(struct SNode));
	S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
	S->Top = -1;
	S->MaxSize = MaxSize;
	return S;
}

bool IsEmpty( Stack S )
{
	return (S->Top == -1);
}

bool IsFull( Stack S )
{
	return (S->Top == (S->MaxSize-1));
}

bool Push( Stack S, ElementType X )
{
	if ( IsFull(S) ) return false;
	else  {
		S->Data[++(S->Top)] = X;
		return true;
	}
}

ElementType Pop( Stack S )
{
	if ( IsEmpty(S) ) return 0;
	else {
		return S->Data[(S->Top)--];
	}
}
ElementType Peek( Stack S )
{
	return ( S->Data[S->Top] );
}
void Clear( Stack S )
{
	while ( !IsEmpty(S) ) Pop(S);
}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值