链表实现——堆栈模拟队列 2020.09.20

基础实验3-2.5 堆栈模拟队列 (25分)

设已知有两个堆栈S1S2,请用这两个堆栈模拟出一个队列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>
#include<stdbool.h>
#define ERROR -1
typedef int ElementType; 
typedef struct SNode * PrtToSNode;
struct SNode
{
    ElementType Data;
    PrtToSNode Next;
    int MaxSize;
};
typedef PrtToSNode Stack;

// Stack
Stack CreateStack(int M)
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Next = NULL;
    S->MaxSize = M;
    return S;
}

bool SIsEmpty(Stack S)
{
    return (S->Next == NULL);
}

bool SIsFull(Stack S, int M)
{
    return (S->MaxSize == M);
}

bool Push(Stack S, ElementType X)
{
    PrtToSNode TmpCell;

    TmpCell = (PrtToSNode)malloc(sizeof(struct SNode)); // create a new node
    TmpCell->Data = X; // save the value
    TmpCell->Next = S->Next; // link the new node
    TmpCell->MaxSize = S->MaxSize;
    S->Next = TmpCell;
    return true;
}

ElementType Pop(Stack S)
{
    PrtToSNode FirstCell;

    ElementType TopElem;

    if(SIsEmpty(S))
    {
        printf("Stack Empty.");
        return ERROR;
    }
    else
    {
        FirstCell = S->Next; // We get the pre-stored node
        TopElem = FirstCell->Data; // get the value
        S->Next = FirstCell->Next; //update the node position
        free(FirstCell); // free the space
        return TopElem; // return the value
    }
}


void AddQ(ElementType item)
{

}

ElementType DeleteQ()
{

}
/*
思路:输入两个堆栈的长度,长度短的作为输入栈,长度长的作为输出栈,
如果输入栈满了并且输出栈不是空的,此时为栈满,其他情况都可以输入。
当输入栈满了,输出栈为空时,就可以将输入栈的元素全部转移到输出栈,
再向输入栈添加元素。
*/

int main()
{
    int N1, N2, size1, size2;
    scanf("%d %d", &N1, &N2);
    getchar(); // eliminate the '\n'
    size1 = N1 < N2 ? N1 : N2;
    size2 = N1 + N2 - size1;
    Stack S1 = CreateStack(size1);
    Stack S2 = CreateStack(size2);
    char ch; int num;
    int cur_size1 = 0, cur_size2 = 0;
    while(true)
    {
        scanf("%c", &ch);
        if(ch == ' ')
            continue;
        switch(ch)
        {
            case 'T':
                exit(0);
            case 'A':
                scanf("%d", &num);
                if(SIsFull(S1, cur_size1) && !SIsEmpty(S2)) // Stack full
                    printf("ERROR:Full\n");
                else if(SIsFull(S1, cur_size1) && SIsEmpty(S2))
                {
                    while(cur_size1--)
                    {
                        ElementType ElemNum = Pop(S1);
                        Push(S2, ElemNum);
                        cur_size2++;
                    }
                    cur_size1 = 1;
                    Push(S1, num);
                }
                else if(cur_size1 < size1)
                {
                    Push(S1, num);
                    cur_size1++;
                }
                break;

            case 'D':
                if(!SIsEmpty(S2))
                {
                    printf("%d\n", Pop(S2));
                    cur_size2--;
                }
                else if(SIsFull(S1, cur_size1) && SIsEmpty(S2))
                {
                    while(cur_size1--)
                    {
                        ElementType ElemNum = Pop(S1);
                        Push(S2, ElemNum);
                    }
                    printf("%d\n", Pop(S2));
                    cur_size2--;                    
                    cur_size1 = 0;
                }
                else
                    printf("ERROR:Empty\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值