用两个栈结构来实现一个队列的功能

思路如下:

假设2个栈初始都为空,栈A和栈B。

我们栈A提供入队列的功能,为栈B专门提供出队列的功能。

这里我们有一个问题需要考虑,因为对A栈实现插入操作比较简单,直接使用栈的插入操作就可以了,但是对B栈的出队列操作就会有点小问题。因为初始栈B是空的,这样我们可以考虑一种方法。

出队列的时候有两种选择:

  • 如果栈B为空的话,就将A栈的数据依次弹出并将元素放入到B栈中,最后在弹出栈B的元素。
  • 如果栈B不为空的话,就直接弹出栈B的栈定元素,作为出队操作
要考虑到队列的性质是先入队列的先出队列,
这个时候可以根据栈的代码来实现该功能。用C++语言实现,代码:

#include <iostream>
using namespace std;

typedef  int SNodeType;

typedef  struct SNode
{
    struct SNode *  next; //下一个节点信息,
    SNodeType       data; //数据信息
}SNode;

typedef struct LinkStack
{
    SNode *   top;
    SNode *   bottom;
    int       length;
}LinkStack;

//初始化链表
void InitStack(LinkStack *S)
{
    if(S == NULL)
        return ;
    S->top = S->bottom = NULL;
    S->length = 0;
}

void Push(LinkStack *S,SNodeType message)
{
    SNode  *current = new SNode;
    current->data = message;
    current->next = NULL;

    if(!S->bottom) //如果栈为空的话
    {
        S->bottom = S->top = current;
        S->length++;
    }
    else
    {
        S->top->next = current;
        S->top = current;
        S->length++;
    }
}


void Print(LinkStack *S)
{
    if(!S->bottom)
        return ;
    else
    {
        SNode *current = S->bottom;
        while(current != S->top)
        {
            cout << current->data << " ";
            current = current ->next;
        }
        cout << current->data ;
        cout << endl;
    }
}
void Pop(LinkStack *L,SNodeType *message)
{
    if( !L->top || !L->bottom)
        return ;

    SNode *current ;
    //*message = current->data;

    if(L->top == L->bottom)//如果只有一个元素
    {
        current = L->top;
        *message = current->data;
        delete(current);
        L->top = L->bottom = NULL;

    }
    else
    {
        current = L->bottom;
        while(current->next != L->top)
            current = current->next;
        *message = current->next->data;
        delete(current->next);
        L->top = current;
        L->top->next =NULL;

    }
    L->length--;
}
typedef struct  MyQueue
{
    LinkStack A,B;
}MyQueue;

void InitMyQueue(MyQueue *Q)
{
    InitStack(&(Q->A));
    InitStack(&(Q->B));

}

//队列插入操作
void QueuePush(MyQueue *Q,SNodeType message)
{
    Push(&(Q->A),message);  //对A栈插入元素
}

//队列出队操作
void QueuePop(MyQueue *Q,SNodeType *message)
{
    SNodeType temp = 0;
    //这里首先判断栈B 是否为空,如果为空,则依次弹出A的元素并且插入到B中,并且B弹出栈定元素
    //如果不为空,直接弹出栈顶元素
    if((Q->B).length == 0)//如果栈B为空
    {
        if((Q->A).length == 0)
            return ;
        else
        {
            while((Q->A).length != 0)//将A的元素全部出栈,并且放入到栈B中
            {
                Pop(&(Q->A),&temp);
                Push(&(Q->B),temp);
            }
        }
        Pop(&(Q->B),message);
    }
    else
    {
        Pop(&(Q->B),message);
    }
}
//用两个栈实现队列的功能
int main()
{
    //声明和初始化 队列
    MyQueue  Q;
    InitMyQueue(&Q);

    SNodeType i,message;
    //cout << " message = " << message <<endl;
    for(i = 1;i < 10; i++)
        QueuePush(&Q,i);
    //Print(&(Q.A));
    for(i = 1;i < 10; i++)
    {
        QueuePop(&Q,&message);
        cout << " the top of zhan is : " << message <<endl;
    }
    return 0;
}
/*int main()
{
    LinkStack L;
    InitStack(&L);
    int i;
    for(i = 0;i < 10; i++)
        Push(&L,i);
    int s;
    while(L.length != 0)
    {
        Pop(&L,&s);
        cout << "zhanding is " << s << endl;
    }
    cout << endl;
    return 0;
}*/







运行结果:


可以看到  按照顺序如队列,先进的仍然先出来。

补充版本。用类模板和stack 

#include <iostream>
using namespace std;
#include <stack>

//声明模板队列类
template <class T> struct MyQueue
{
    //声明2个栈
    stack <T> stackA;
    stack <T> stackB;

    //插入操作
    void push(T &t)
    {
         stackA.push(t);
    }

    //返回队列首元素
    T front()
    {
        if(stackB.empty())//如果stackB 为空
        {
            while(!stackA.empty())
            {
                stackB.push(stackA.top());
                stackA.pop();

            }
        }
        return stackB.top();
    }
    //出队列
    void pop()
    {
        if(stackB.empty())//如果stackB 为空
        {

            while(!stackA.empty())
            {
                stackB.push(stackA.top());
                stackA.pop();

            }
        }
        if(!stackB.empty())
            stackB.pop();
    }

};

int main()
{
    MyQueue <int> Q; //声明int类型的模板队列
    int i;
    for(i = 0;i < 10; i++)
    {
        Q.push(i);
    }
    for(i = 0;i < 10; i++)
    {
        cout << Q.front() << endl;
        Q.pop();
    }
    return 0;
}
运行结果:



可以看出来  代码量少了很多,


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值