Sword point offer programming problem Java implementation--face question 7 related problems with two queue implementation of a stack

Source: Internet
Author: User

The sword refers to the offer surface Question 7 related topic: Implements a stack with two queues


Problem-solving ideas: Based on the first in and out of the stack and the first-in first out of the queue characteristics
1. In push, add elements to the non-empty queue
2. At the time of the pop, poll the size ()-1 elements in the non-empty queue, add to another empty queue, and poll the last element in the queue.
Two queues in case the stack is not empty, there is always one empty and the other is not empty. Push adds elements to a non-empty queue, the pop transfers the elements of the non-empty queue to another empty queue until the last element is left, which is the element to be stacked (the last element added to the queue).

1  Packagesolution;2 3 Importjava.util.LinkedList;4 ImportJava.util.Queue;5 6 /**7 * The sword refers to the offer surface question 7 related topics: Implement a stack with two queues8 * Problem Solving ideas: According to the first in and out of the stack and the first-in first out of the queue characteristics9 * Add elements to a non-empty queue at pushTen * In the pop, the non-empty queue of size ()-1 elements poll out, add to another empty queue, and then the last element in the queue poll out One * Two queues in case the stack is not empty, there is always one empty and the other is not empty. Push adds elements to a non-empty queue, pop transfers elements of non-empty queues to another empty queue, A * Until the last element is left, this element is the element that will be out of the stack (the last element added to the queue).  -  * @authorGL -  * the  */ -  Public classNo7stackwithtwoqueues { -  -      Public Static voidMain (string[] args) { +Push (1); -Push (2); +Push (3); A pop (); atPush (4); - pop (); - pop (); - pop (); - pop (); -  in     } -  to     Private StaticQueue<object> queue1=NewLinkedlist<object>(); +     Private StaticQueue<object> queue2=NewLinkedlist<object>(); -      the     /* * * Add elements to a non-empty queue when performing a stack operation into a queue $      */Panax Notoginseng      Public Static voidpush (Object item) { -         if(!queue1.isempty ()) the Queue1.offer (item); +         Else A Queue2.offer (item); theSystem.out.println ("into the stack element is:" +item); +     } -      $      Public Static voidpop () { $         if(!IsEmpty ()) { -             if(Queue1.isempty ()) { -                  while(Queue2.size () >1){ the Queue1.offer (Queue2.poll ()); -                 }WuyiSystem.out.println ("Out of stack element:" +Queue2.poll ()); the}Else{ -                  while(Queue1.size () >1){ Wu Queue2.offer (Queue1.poll ()); -                 } AboutSystem.out.println ("Out of stack element:" +Queue1.poll ()); $             } -         } -         Else -             Throw NewRuntimeException ("Stack is empty, unable to perform a stack operation"); A     } +      the     /* - * Check if stack is empty $      */ the     Private Static BooleanIsEmpty () { the         returnQueue1.isempty () &&queue2.isempty (); the     } the}

Sword point offer programming problem Java implementation--face question 7 related problems with two queue implementation of a stack

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.