7. Two stack simulation queues and two queues simulation stacks

Source: Internet
Author: User

7. Two stack simulation queues and two queues simulation stacks

Simulate a queue using two stacks

Stack1, stack2

First, add data to stack1. If you need to output data, delete the data from stack2. If stack2 is empty, import the data from stack1 to stack2.


# Include "static. h" # include
 
  
# Include
  
   
Template
   
    
Class CQuue {public: CQuue (){};~ CQuue () {}; void appendTail (const T & node); T deleteHead (); private: stack
    
     
M_stack1; stack
     
      
M_stack2 ;//
      <存入数据的时候放入到stack1当中,取数据从stack2中取出,如果stack2为空,将stack1中数据转入stack2当中};template
        Void CQuue
       
         : AppendTail (const T & node) {m_stack1.push (node);} template
        
          T CQuue
         
           : DeleteHead () {if (m_stack2.empty () {if (m_stack1.empty () {printf ("There is no data in stack"); exit (1 );} while (! M_stack1.empty () {m_stack2.push (m_stack1.top (); m_stack1.pop () ;}} T valueTemp = m_stack2.top (); m_stack2.pop (); printf ("% d", valueTemp ); return valueTemp;} int main () {CQuue
          
            Cqueue; cqueue. appendTail (1); cqueue. appendTail (3); cqueue. deleteHead (); cqueue. appendTail (5); cqueue. appendTail (7); cqueue. deleteHead (); cqueue. deleteHead (); cqueue. deleteHead (); return 0 ;}
          
         
        
       
      
     
    
   
  
 

Two queue simulation stacks

The method used here is not the same as the method mentioned in the book, but the general meaning is the same, but the method in the book will reduce the judgment statement

Two Queues: Put data into an empty queue each time, copy data from the other queue to the current queue, and keep an empty queue

Output data from non-empty queues

However, this method requires a copy for every input data, which is also troublesome.

In the book, the previous data needs to be copied to another queue every time the data is output.

# Include "static. h" # include
 
  
# Include
  
   
Template
   
    
Class CStack {public: CStack (){};~ CStack () {}; void appendTail (const T & node); T deleteHead (); private: queue
    
     
M_queue1; queue
     
      
M_queue2 ;//
      <存入数据的时候放入到m_queue1当中,然后再将数据都导入进queue2当中,然后每次添加数据放入一个空的queue当中};template
        Void CStack
       
         : AppendTail (const T & node) {if (m_queue1.size () = 0) {m_queue1.push (node); while (! M_queue2.empty () {m_queue1.push (m_queue2.front (); m_queue2.pop () ;}} else {m_queue2.push (node); while (! M_queue1.empty () {m_queue2.push (m_queue1.front (); m_queue1.pop () ;}} template
        
          T CStack
         
           : DeleteHead () {T tempValue; if (m_queue1.empty () & m_queue2.empty () {printf ("the stack is empty"); exit (1 );} if (m_queue1.size ()> 0) {tempValue = m_queue1.front (); m_queue1.pop ();} else {tempValue = m_queue2.front (); m_queue2.pop ();} printf ("% d ", tempValue); return tempValue;} int main () {CStack
          
            Cstack; cstack. appendTail (1); cstack. deleteHead (); cstack. appendTail (3); cstack. appendTail (5); cstack. appendTail (7); cstack. deleteHead (); cstack. deleteHead (); cstack. deleteHead (); return 0 ;}
          
         
        
       
      
     
    
   
  
 


Related Article

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.