Interview question 6: queue using two stacks

Source: Internet
Author: User


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: Stack stack1

(2) out queue: If stack2 is not empty, the top element of stack2 is directly displayed.

If stack2 is empty, the elements in stack1 are displayed in stack2, and the top elements in stack2 are displayed.

C ++ code:

 

# 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 ;}# 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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.