PTA习题7-4 堆栈模拟队列

设已知有两个堆栈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 ERROR -1
typedef int ElementType;
typedef int Pos;
typedef struct SNode *Stack;
struct SNode
{
    ElementType *Data;
    Pos Top;
    int MaxSize;
};
Stack Creastack(int size)
{
    Stack S=(Stack)malloc(sizeof(struct SNode));
    S->Data=(ElementType*)malloc(size*sizeof(ElementType));
    S->Top=-1;
    S->MaxSize=size;
    return S;
}
int IsFull(Stack S)
{
    return S->Top==(S->MaxSize-1);
}
int IsEmpty(Stack S)
{
    return S->Top==-1;
}
void Push(Stack S,ElementType item)
{
    if(IsFull(S))
    {
        printf("ERROR:Full\n");
    }
    else
        S->Data[++S->Top]=item;
}
ElementType Pop(Stack S)
{
    if(IsEmpty(S))
    {
        return ERROR;
    }
    else
    {
        return S->Data[S->Top--];
    }
}
void AddQ(ElementType item,Stack S1,Stack S2)
{
    if (IsFull(S1) && !IsEmpty(S2))
    {
        printf("ERROR:Full\n");
        return;
    }
    if (IsFull(S1)&&IsEmpty(S2))
        {
            while(!IsEmpty(S1))
            {
                Push(S2,Pop(S1));
            }
        }
    Push(S1, item);
}
ElementType DeleteQ(Stack S1,Stack S2)
{
      if (IsEmpty(S1) && IsEmpty(S2))
      {
        printf("ERROR:Empty\n");
        return ERROR;
      }
    if (IsEmpty(S2))
    {
        while (!IsEmpty(S1))
        {
            Push(S2, Pop(S1));
        }
    }
    return Pop(S2);
}
int main()
{
    int N1,N2,item;
    char chao;
    Stack S1,S2;
    scanf("%d %d",&N1,&N2);
    S1=Creastack(N2);
    S2=Creastack(N1);
    while(scanf(" %c",&chao)!= EOF &&chao!='T')
    {
        if(chao=='A')
        {
            scanf(" %d",&item);
            AddQ(item,S1,S2);
        }
        else if(chao=='D')
        {
           ElementType ele=DeleteQ(S1,S2);
            if(ele!=ERROR)
                printf("%d\n",ele);
        }
    }
    return 0;
}

 但是实在没办法了,交换生成S1和S2时的预设大小,就可以过了,实在没搞懂......

#include <stdio.h>
#include <stdlib.h>
#define ERROR -1
typedef int ElementType;
typedef int Pos;
typedef struct SNode *Stack;
struct SNode
{
    ElementType *Data;
    Pos Top;
    int MaxSize;
};
Stack Creastack(int size)
{
    Stack S=(Stack)malloc(sizeof(struct SNode));
    S->Data=(ElementType*)malloc(size*sizeof(ElementType));
    S->Top=-1;
    S->MaxSize=size;
    return S;
}
int IsFull(Stack S)
{
    return S->Top==(S->MaxSize-1);
}
int IsEmpty(Stack S)
{
    return S->Top==-1;
}
void Push(Stack S,ElementType item)
{
    if(IsFull(S))
    {
        printf("ERROR:Full\n");
    }
    else
        S->Data[++S->Top]=item;
}
ElementType Pop(Stack S)
{
    if(IsEmpty(S))
    {
        return ERROR;
    }
    else
    {
        return S->Data[S->Top--];
    }
}
void AddQ(ElementType item,Stack S1,Stack S2)
{
    if (IsFull(S1) && !IsEmpty(S2))
    {
        printf("ERROR:Full\n");
        return;
    }
    if (IsFull(S1)&&IsEmpty(S2))
        {
            while(!IsEmpty(S1))
            {
                Push(S2,Pop(S1));
            }
        }
    Push(S1, item);
}
ElementType DeleteQ(Stack S1,Stack S2)
{
      if (IsEmpty(S1) && IsEmpty(S2))
      {
        printf("ERROR:Empty\n");
        return ERROR;
      }
    if (IsEmpty(S2))
    {
        while (!IsEmpty(S1))
        {
            Push(S2, Pop(S1));
        }
    }
    return Pop(S2);
}
int main()
{
    int N1,N2,item;
    char chao;
    Stack S1,S2;
    scanf("%d %d",&N1,&N2);
    S1=Creastack(N2);
    S2=Creastack(N1);
    while(scanf(" %c",&chao)!= EOF &&chao!='T')
    {
        if(chao=='A')
        {
            scanf(" %d",&item);
            AddQ(item,S1,S2);
        }
        else if(chao=='D')
        {
           ElementType ele=DeleteQ(S1,S2);
            if(ele!=ERROR)
                printf("%d\n",ele);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯基w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值