Two stack implementation queue and two queue implementation stack
Queuing (queue) FIFO linear table, stack (stack) advanced out of the linear table.
Two stacks to implement a queue
fa Yi Ideas:
S1 is in the stack, S2 is out of the stack.
- Into the queue: directly pressed into the S1;
- Out queue: If S2 is not empty, the top element of the S2 is ejected directly, otherwise all the elements of S1 are ejected into S2, and then the top element of the S2 is ejected.
Code:
#include <stack>#include<iostream>#include<cassert>using namespacestd; template<classT>classQueue { Public: BOOLEmpty ()Const{returnM_stack1.empty () &&m_stack2.empty ();} size_t size ()Const{returnM_stack1.size () +m_stack2.size ();} voidPushConstt&x); voidpop ();Private: Stack<T>M_stack1; Stack<T>M_stack2;}; Template<classT>voidQueue<t>::p Ush (Constt&x) {M_stack1.push (x);} Template<classT>voidQueue<t>::p op () {//if M_stack2 is empty,and there is some elements in m_stack1, push them in M_stack2 if(M_stack2.empty ()) { while( !M_stack1.empty ()) {T& data =M_stack1.top (); M_stack1.pop (); M_stack2.push (data); } } //push the element into M_stack2ASSERT (!m_stack2.empty ()); M_stack2.pop ();} intMain () {Queue<int>Q; for(inti =1; I <=5; ++i) Q.push (i); Q.pop (); Q.push (6 ); Q.pop (); Q.pop (); Q.push (7 ); Q.pop (); Q.pop (); Q.pop (); Q.pop (); return 0;}
Law Two ideas:
Implemented with two stacks, one of the stack instack is dedicated to the queue, and the other stack Outstack specifically handles the team work.
- Into the queue: directly add elements to the instack, the Complexity of O (1);
- Out queue: Because the queue is first-in, first-out, and the stack is first-in and out, so the output should be reversed. For example, Instack received the order of elements 1, 2, 3, the top element of the stack is 3, but the team would like to let 1 first out. The solution is to put the elements in the instack into the Outstack, so Outstack received the order of the Elements is 3, 2, 1, the top element of the stack is what we want 1, the time complexity of O (N).
Summary, the queue directly instack.push (element), the team first determine whether the outstack is empty, if not empty, then direct outstack.pop (), if it is empty, the instack element into the Outstack, and then execute Outstack.pop ().
#include <stack>#include<string>#include<iostream>#include<cassert>using namespacestd;template<classT>classyl_queue{ Public: voidEnqueueConstT &element);//QueueT dequeue ();//out TeamT top ();//Team first element BOOLEmpty ()Const;//determine if the queue is emptysize_t size ()Const;//Size of the queue
Private: Stack<T>Instack; Stack<T>Outstack;}; Template<classT>voidYl_queue<t>::enqueue (ConstT &Element) {Instack.push (element);} Template<classT>T Yl_queue<T>::d equeue () {Assert (!empty ()); T temp; if(!Outstack.empty ()) {Temp=Outstack.top (); Outstack.pop (); returntemp; } Else { while(!Instack.empty ()) {Temp=Instack.top (); Instack.pop (); Outstack.push (temp); } temp=Outstack.top (); Outstack.pop (); returntemp; }}template<classT>T Yl_queue<T>:: Top () {Assert (!empty ()); T temp; if(!Outstack.empty ()) {Temp=Outstack.top (); returntemp; } Else { while(!Instack.empty ()) {Temp=Instack.top (); Instack.pop (); Outstack.push (temp); } temp=Outstack.top (); returntemp; }}template<classT>BOOLYl_queue<t>::empty ()Const{ return(Size () = =0);} Template<classT>size_t Yl_queue<t>::size ()Const{ returnInstack.size () +outstack.size ();}
voidMain () {Yl_queue<int>Myqueue; Myqueue.enqueue (1); Myqueue.enqueue (2); Myqueue.enqueue (3); Myqueue.enqueue (4); Myqueue.enqueue (5); cout<<"1 The size of the queue is:"<<myqueue.size () <<Endl; cout<<"1 Elements of the first team are:"<<myqueue.top () <<Endl; Myqueue.dequeue (); Myqueue.dequeue (); cout<<"2 the size of the queue is:"<<myqueue.size () <<Endl; cout<<"2 Elements of the first team are:"<<myqueue.top () <<Endl; Myqueue.dequeue (); Myqueue.dequeue (); Myqueue.dequeue (); cout<<"3 The size of the queue is:"<<myqueue.size () <<Endl;}
Two queue implementation stacks
Ideas
The S1 is into the queue, S2 is the secondary S1 into the queue.
- Into the queue: if the S1 is not empty, push the S1 element to S2, then push the new element to S1, then push the S2 element to S1; otherwise, push the element directly to S1
- Out of queue: Pop directly from S1
Code:
#include <iostream>#include<queue>#include<cassert>using namespacestd;template<classT>classStack { Public: BOOLEmpty ()Const{returnM_queue1.empty () &&m_queue2.empty ();} size_t size ()Const{returnM_queue1.size () +m_queue2.size ();} T&top (); Constt& Top ()Const; voidPushConstt&x); voidpop ();Private: Queue<T>m_queue1; Queue<T>m_queue2;}; Template<classT>T& stack<t>:: Top () {Assert (!m_queue1.empty ()); returnM_queue1.front ();} Template<classT>Constt& Stack<t>::top ()Const{assert (!m_queue1.empty ()); returnM_queue1.front ();} Template<classT>voidStack<t>::p Ush (Constt&x) { while(!m_queue1.empty ()) {//S1 not empty, push element to S2 Constt& val =M_queue1.front (); M_queue1.pop (); M_queue2.push (Val); } m_queue1.push (x); //push new element to S1 while(!m_queue2.empty ()) {//s2 not empty, put element in push back S1 Constt& val =M_queue2.front (); M_queue2.pop (); M_queue1.push (Val); }}template<classT>voidStack<t>::p op () {Assert (!m_queue1.empty ()); M_queue1.pop ();}intMain () {Stack<int>s; for(inti =1; I <=5; ++i) S.push (i); cout<<"1. Number of stack elements:"<< s.size () <<Endl; cout<<"1. Stack top elements:"<< s.top () <<Endl; S.pop (); S.push (6 ); cout<<"2. Number of stack elements:"<< s.size () <<Endl; cout<<"2. Stack top elements:"<< s.top () <<Endl; S.pop (); S.pop (); S.pop (); S.push (7 ); cout<<"3. Number of stack elements:"<< s.size () <<Endl; cout<<"3. Stack top elements:"<< s.top () <<Endl; S.pop (); S.pop (); S.pop (); cout<<"4. Number of stack elements:"<< s.size () <<Endl; return 0;}
Algorithm--Two stack implementation queue and two queue implementation stacks