Idea: set two stacks stack1 and stack2, stack1 to implement the inbound queue function, and stack2 to implement the outbound queue function.
(1) inbound queue: Inbound stack stack1 (2) outbound queue: If stack2 is not empty, the top stack element in stack2 is displayed directly. If stack2 is empty, then, the elements in stack1 are popped up and put into stack2, and the c ++ code of the top element in stack2 is displayed:
# Include "stdafx. h" # include <iostream> # include <stack> using namespace std; template <class T> class CQueue {public: CQueue (void );~ CQueue (void); void AddTail (const T & addData); void DeleteHead (T & deleteData); private: stack <T> stack1; stack <T> stack2 ;}; template <class T> CQueue <T >:: CQueue (void) {}template <class T> CQueue <T> ::~ CQueue (void) {}template <class T> void CQueue <T >:: AddTail (const T & addData) {stack1.push (addData );} template <class T> void CQueue <T>: DeleteHead (T & deleteData) {if (stack2.empty () {while (! Stack1.empty () {T topData = stack1.top (); stack2.push (topData); stack1.pop () ;}} if (stack2.empty () {cout <"no element in the queue! "<Endl ;}else {deleteData = stack2.top (); stack2.pop () ;}} int _ tmain (int argc, _ TCHAR * argv []) {CQueue <int> q; for (int I = 0; I <6; I ++) {q. addTail (I) ;}int deleteData = 0; for (int I = 0; I <6; I ++) {q. deleteHead (deleteData); cout <deleteData <"" ;}cout <endl; q. deleteHead (deleteData); system ("pause"); return 0 ;}