Data structure and algorithm stack and queue one: two stacks simulating a queue and two queues simulating a stack

Source: Internet
Author: User

What we need to learn today is about the two structures, stacks and queues that are often seen in data structures. We can say that we are always using stacks, such as the stack of systems used in front of recursion, and the custom stack class stack that is introduced when the reverse output of the list is used, and recursively using the stack of the system. So, here we talk about the mutual implementation between these two more distinctive or closely related data structures.

One: two stack simulation to implement a queue: The stack is characterized by advanced, but the queue is characterized by FIFO.

    

 Public classQueen (Stack s1,stack s2) {//ways to implement insertions      Public voidAddintnum)     {S1.push (num); }     //Delete      Public voidDelete () {//determine if the S2 has a value          if(S2.size () > 0) {s2.pop (); }Else{                 //First, add the contents of S1 to S2.                  for(inti = 0;i < S1.size ()-1;i++) {S2.push (S1.pop ()); }                 //Just eject the rest of the S1 node.S1.pop (); }     }}                            

Two stacks to simulate a queue of ideas and algorithms is relatively simple, just need to pay attention to the details of the line, do not let their algorithm error.

Two: Simulate a stack with two queues:

        

1  Public classMyqueue () {2       //declaring two simulated queues3queue<integer> q1 =NewArraydeque<>(); 4queue<integer> q2 =NewArraydeque<>();5      //Add Action6       Public voidPushintnum) {7          //determine if two queues are empty8          if(Q1.isempty () &&Q2.isempty ()) {9                //prioritize Q1 perform increased operationsTen q1.offer (num); One}Else if(Q1.isempty ()) { A q2.offer (num); -}Else{ - q1.offer (num); the          } -      } -      //Delete Operation -       Public intpop () { +           if(Q1.isempty () &&Q2.isempty ()) { -                Throw NewExpection ("Empty stack no data popup");  +}Else if(Q1.isempty ()) { A                 for(inti = 0;i < Q2.size ()-1;i++){ at Q1.offer (q2.poll); -                } -                returnQ2.poll (); -}Else{ -                   for(inti = 0;i < Q1.size ()-1;i++){ - Q2.offer (q1.poll); in                } -                returnQ1.poll (); to          } +      } -}

Queue simulation stack or stack simulation queue, in fact, is nothing more than seize the queue FIFO, stack LIFO features and then to achieve their increased deletion. But it's important to pay attention to your rigor in the process.

Data structure and algorithm stack and queue one: two stacks simulating a queue and two queues simulating a stack

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.