用两个栈模拟一个队列

思路:
让栈A提供入队功能,栈B提供出队功能。
入队列:栈A。
出队列:如果栈B不为空,直接弹出栈B的栈顶数据;如果栈B为空,则依次弹出栈A的数据,放入栈B中,再弹出栈B的栈顶数据。

方法一:

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

template<class T>
struct MyQueue
{
    void Enqueue(T t)
    {
        s1.push(t);
    }

    T Dequeue()
    {
        if(s2.empty())
        {
            if(0 == s1.size())
            {
                exit(1);
            }

            while(!s1.empty())
            {
                s2.push(s1.top());
                s1.pop();
            }
        }

        T retVal = s2.top();
        s2.pop();
        return retVal;
    }

    stack<T> s1;
    stack<T> s2;
};

int main()
{
    MyQueue<int> mq;
    int begin = 0;
    int end = 10;
    int i;

    for(i = begin; i < end; i++)
    {
        mq.Enqueue(i);
    }

    for(i = begin; i < end; i++)
    {
        cout<<mq.Dequeue()<<' ';
    }
    cout<<endl;

    MyQueue<float> mq2;
    mq2.Enqueue(1.1f);
    mq2.Enqueue(2.2f);
    cout << mq2.Dequeue() << endl;

    MyQueue<string> mq3;
    string s1 = "hello";
    string s2 = "world";
    mq3.Enqueue(s1);
    mq3.Enqueue(s2);
    cout << mq3.Dequeue() << endl;

    return 0;
}

运行结果:

0 1 2 3 4 5 6 7 8 9
1.1
hello
Press any key to continue

方法二:

#include <iostream>
using namespace std;

struct Node
{
public:
    Node(int i):data(i),next(NULL){}
    int data;
    Node *next;
};

class Stack
{
public:
    Stack():top(NULL){}
    void Push(Node node);   //入栈
    int Pop();              //出栈
    bool IsEmpty();         //是否为空
    void Print();           //打印所有元素
private:
    Node *top;
};

void Stack::Push(Node node)
{
    Node *n = new Node(node.data);
    n->next = top;
    top = n;
}

int Stack::Pop()
{
    if (IsEmpty())
    {   
        cout << "Stack Empty!" << endl;
        exit(1);
    }
    else
    {
        int topVal = top->data;
        Node *temp = top;
        top = top->next;
        delete temp;
        return topVal;
    }
}

bool Stack::IsEmpty()
{
    if (NULL == top)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void Stack::Print()
{
    if(IsEmpty())
    {
        cout << "Inner Stack Empty!" << endl;
    }
    else
    {
        Node *node = top;
        while (node != NULL)
        {
            cout << node->data << " ";
            node = node->next;
        }
        cout << endl;
    }
}

class Queue
{
public:
    void Enqueue(Node node);    //入队
    int Dequeue();              //出队
    bool IsEmpty();             //是否为空
private:
    Stack s1;                   //用于入队
    Stack s2;                   //用于出队
};

void Queue::Enqueue(Node node)
{
    s1.Push(node);              //只对s1进行进栈操作
    cout << "Enqueue " << node.data << endl;
}

int Queue::Dequeue()
{
    if(s2.IsEmpty())
    {
        if(s1.IsEmpty())        //s2和s1都为空
        {
            cout << "Queue Empty! Can not be dequeued again!" << endl;
            exit(1);
        }
        else                    //s2为空s1不为空,把s1的所有元素都倒入s2中
        {
            while(!s1.IsEmpty())
            {
                int i = s1.Pop();
                s2.Push(Node(i));
            }
        }
    }

    return s2.Pop();
}

bool Queue::IsEmpty()
{
    if (s1.IsEmpty() && s2.IsEmpty())
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{
    Queue q;
    q.Enqueue(Node(1));
    q.Enqueue(Node(2));
    q.Enqueue(Node(3));
    cout << "Dequeue " << q.Dequeue() << endl;
    q.Enqueue(Node(4));
    cout << "Dequeue " << q.Dequeue() << endl;
    cout << "Dequeue " << q.Dequeue() << endl;
    cout << "Dequeue " << q.Dequeue() << endl;


    return 0;
}

运行结果:

Enqueue 1
Enqueue 2
Enqueue 3
Dequeue 1
Enqueue 4
Dequeue 2
Dequeue 3
Dequeue 4
Press any key to continue
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值