用两个栈完成队列入队,出队,判空

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
using namespace std;

typedef struct
{
    int elem[maxsize];
    int top;
}Sqstack;
int Isempty(Sqstack st)
{
    if(st.top==-1) return 1;
    else return 0;
}
int push(Sqstack &st,int x)
{
    if(st.top==maxsize-1) return 0;
    else
    {
        st.elem[++st.top]=x;
        return 1;
    }
}
int pop(Sqstack &st)
{
    int x;
    if(st.top==-1) return 0;
    else
    {
        x=st.elem[st.top--];
        return x;
    }
}
int Enquene(Sqstack &st1,Sqstack &st2,int x)
{
    int y;
    if(st1.top==maxsize-1)
    {
        if(!Isempty(st2)) return 0;
        else if(Isempty(st2))
        {
           while(!Isempty(st1))
           {
               y=pop(st1);
               push(st2,y);
           }
           push(st1,x);
            return 1;
        }

    }
    else
    {
        push(st1,x);
        return 1;
    }
}
int outquene(Sqstack &st1,Sqstack &st2)
{
    int y,x;
    if(!Isempty(st2))
    {

        return pop(st2);;
    }
    else
    {
        if(Isempty(st1)) return 0;
        else
        {
            while(!Isempty(st1))
            {
                y=pop(st1);
                push(st2,y);
            }

            return pop(st2);
        }
    }
}
int Isemptyq(Sqstack st1,Sqstack st2)
{
    if(st1.top==-1&&st2.top==-1) return 1;
    else return 0;
}
int main()
{
    Sqstack st1,st2;
    st1.top=-1;
    st2.top=-1;
    int x;
    Enquene(st1,st2,3);
    Enquene(st1,st2,1);
    printf("%d\n",outquene(st1,st2));

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39350434/article/details/81291399