码迷,mamicode.com
首页 > 其他好文 > 详细

题目七:用两个栈实现队列

时间:2019-07-28 13:40:25      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:start   pre   队列实现   typename   one   ack   cst   tail   tac   

////////////////////////////////////////////////////////////////////////////////////
// 10.题目七:用两个栈实现队列
// 题目:用两个栈实现一个队列,队列的声明如下:

template <typename TYPE>
class CQueue
{
public:
    CQueue(){}
    ~CQueue(){}

    void AppendTail(const TYPE& node);

    TYPE DeleteHead();

private:
    stack<TYPE> m_stPushStack;
    stack<TYPE> m_stPopStack;

};

// 方法一:
//    插入时 --> m_stPopStack不为空,将元素压入m_stPushStack;
//    删除时 --> m_stPushStack不为空,将元素压入 m_stPopStack;

// 方法二:
//    插入时 --> 直接往 m_stPushStack 插入新元素
//    删除时 --> 如果m_stPopStack为空,插入m_stPushStack中元素,否则直接弹出元素
template <typename TYPE>
TYPE CQueue<TYPE>::DeleteHead()
{
#if 0
    while (!m_stPushStack.empty())
    {
        m_stPopStack.push(m_stPushStack.top());
        m_stPushStack.pop();
    }

#else 
    if (m_stPopStack.empty())
    {
        while (!m_stPushStack.empty())
        {
            m_stPopStack.push(m_stPushStack.top());
            m_stPushStack.pop();
        }
    }

#endif

    TYPE tmp = m_stPopStack.top();
    m_stPopStack.pop();

    return tmp;
}

template <typename TYPE>
void CQueue<TYPE>::AppendTail(const TYPE& node)
{
#if 0
    while (!m_stPopStack.empty())
    {
        m_stPushStack.push(m_stPopStack.top());
        m_stPopStack.pop();
    }

    m_stPushStack.push(node);
#else

    m_stPushStack.push(node);

#endif 
}

void QueueWithTwoStackTestFunc()
{
    cout << "\n\n --------------- QueueWithTwoStackTestFunc Start -------------->" << endl;
    CQueue<int> stQueue;
    stQueue.AppendTail(1);
    stQueue.AppendTail(2);
    stQueue.AppendTail(3);
    cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl;

    stQueue.AppendTail(4);
    stQueue.AppendTail(5);
    stQueue.AppendTail(6);
    cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl;
    cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl;
    cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl;
    cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl;
    cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl;

    cout << "\n\n --------------- QueueWithTwoStackTestFunc End -------------->" << endl;

}


/////////////////////////////////////////////////////////////////////////////////////////////
// 11.用两个队列实现一个栈

template <typename TYPE>
class CStack
{
public:
    CStack(){}
    ~CStack(){}

public:
    void AppendTail(const TYPE& value);

    TYPE DeleteHead();

private:
    queue<TYPE> m_stQueue1;
    queue<TYPE> m_stQueue2;
};

template <typename TYPE>
TYPE CStack<TYPE>::DeleteHead()
{
    TYPE head;
    if (m_stQueue1.empty())
    {
        while (m_stQueue2.size() > 1)
        {
            m_stQueue1.push(m_stQueue2.front());
            m_stQueue2.pop();
        }

        head = m_stQueue2.front();
        m_stQueue2.pop();
    }
    else
    {
        while (m_stQueue1.size() > 1)
        {
            m_stQueue2.push(m_stQueue1.front());
            m_stQueue1.pop();
        }

        head = m_stQueue1.front();
        m_stQueue1.pop();
    }

    return head;
}

template <typename TYPE>
void CStack<TYPE>::AppendTail(const TYPE& value)
{
    m_stQueue1.push(value);
}

void StackWithTwoQueueTestFunc()
{
    cout << "\n\n --------------- StackWithTwoQueueTestFunc Start -------------->" << endl;
    CStack<int> stStack;
    stStack.AppendTail(1);
    stStack.AppendTail(2);
    stStack.AppendTail(3);

    cout << "Stack Pop Node: " << stStack.DeleteHead() << endl;

    stStack.AppendTail(4);
    stStack.AppendTail(5);
    stStack.AppendTail(6);
    cout << "Stack Pop Node: " << stStack.DeleteHead() << endl;
    cout << "Stack Pop Node: " << stStack.DeleteHead() << endl;
    cout << "Stack Pop Node: " << stStack.DeleteHead() << endl;
    cout << "Stack Pop Node: " << stStack.DeleteHead() << endl;
    cout << "Stack Pop Node: " << stStack.DeleteHead() << endl;

    cout << "\n\n --------------- StackWithTwoQueueTestFunc End -------------->" << endl;

}

题目七:用两个栈实现队列

标签:start   pre   队列实现   typename   one   ack   cst   tail   tac   

原文地址:https://www.cnblogs.com/yzdai/p/11258619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!