////////////////////////////////////////////////// //////////////////////////////////
// seven 10. Title: two queue stacks
// Title : two stacks implement a queue, the queue declared as follows:
Template <typename the TYPE> class CQueue { public : CQueue () {} ~ CQueue () {} void appendTail ( const the TYPE & Node); the TYPE DeleteHead (); Private : Stack <the TYPE> m_stPushStack; Stack <the TYPE> m_stPopStack; }; @ method a: // insertion -> m_stPopStack not empty, the element is pressed into m_stPushStack; // -;> m_stPushStack not empty, the element is pressed into m_stPopStack deleting @ method two: // insertion -> m_stPushStack directly to insert a new element // When you delete -> If m_stPopStack is empty, insert m_stPushStack elements, or directly pop elements Template <typename the TYPE> the TYPE CQueue <the TYPE> :: DeleteHead () { #if 0 the while (! M_stPushStack.empty ()) { m_stPopStack. Push (m_stPushStack.top ()); m_stPushStack.pop (); } #else IF (m_stPopStack.empty ()) { the while (! m_stPushStack.empty ()) { m_stPopStack.push (m_stPushStack.top ()); m_stPushStack. POP (); } } #endif the 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 with two queue to implement a stack
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; }