Problem
Implement a queue with two stacks to complete the push and pop operations of the queue. The elements in the queue are of type int.
Ideas
Add the queued elements, join the STACK1, if there is a team operation, then stack1 elements in the Stack2, the element is ejected from the stack2 is equal to the team operation, until all elements are ejected from the Stack2, then the element is added from the stack1.
Packageoffer007;ImportJava.util.Stack;/*** @Title: Cqueue.java * @Package: offer007 * @Description Two stacks to implement a queue *@authorHan * @date 2016-4-17 a.m. 11:24:17 *@versionV1.0 *@param<T>*/ Public classCqueue<t> { /*** First stack: The element used to hold the input queue*/ PrivateStack<t>Stack1; /*** Second stack: The element used to hold the output queue*/ PrivateStack<t>Stack2; PublicCqueue () {Stack1=NewStack<t>(); Stack2=NewStack<t>(); } /*** @Description Input Queue *@authorHan *@paramItem *@return */ Publict push (t Item) {Stack1.push (item); returnitem; } /*** @Description output to the head element *@authorHan *@return */ PublicT Pop () {/*** No elements in stack 2*/ if(Stack2.size () <= 0){ /*** Press the elements in stack 1 onto the stack 2*/ while(Stack1.size () > 0) {Stack2.push (Stack1.pop ()); } } if(Stack2.size () <= 0){ Throw NewRuntimeException ("The Queue is empty!!"); } /*** Popup Stack 2 header element*/ returnStack2.pop (); }}
Test
@Test Public voidTestcqueue ()throwsException {cqueue<Integer> queue =NewCqueue<integer>(); Try{Queue.push (3); Queue.push (4); System.out.println (Queue.pop ()); System.out.println (Queue.pop ()); System.out.println (Queue.pop ()); Queue.push (4); Queue.push (5); System.out.println (Queue.pop ()); System.out.println (Queue.pop ()); } Catch(Exception e) {System.out.println (E.getmessage ()); } }
Results
Extended
Using two queues to implement the stack
Ideas
In the stack operation, which team is listed as empty, then which element is added to the empty queue, when the stack operation, the elements in the queue1 (in addition to the end of the team) into the queue2, and the queue1 team tail elements as the top element of the stack pop, the next time out of the stack, The elements in the queue2 (except for the end of the team) are added to the queue1 in turn, and the queue2 tail element is ejected as the top element of the stack.
Packageoffer007;Importjava.util.LinkedList;ImportJava.util.Queue;/*** @Title: Cstack.java * @Package: offer007 * @Description using two queue implementation stacks *@authorHan * @date 2016-4-17 pm 2:35:38 *@versionV1.0*/ Public classCstack<t> { /*** Queue One*/ PrivateQueue<t>queue1; /*** Queue two*/ PrivateQueue<t>queue2; PublicCstack () {queue1=NewLinkedlist<t>(); Queue2=NewLinkedlist<t>(); } /*** @Description push elements into the stack *@authorHan *@paramItem *@return */ Publict push (t item) {/*** If queue one is empty, add elements to queue two*/ if(queue1.size () = = 0) {Queue2.offer (item); }Else{Queue1.offer (item); } returnitem; } /*** @Description The top element of the stack pops up *@authorHan *@return */ PublicT Pop () {/*** Which queue is not empty, which queue to use as the output queue, and the other as the input queue*/ if(Queue1.size ()! = 0){ returnGetTop (queue1, queue2); }Else if(Queue2.size ()! = 0){ returnGetTop (queue2, queue1); } Throw NewRuntimeException ("Stack top no elements!! "); } /*** @Description Get stack top element *@authorHan *@paramOutput *@paramInput *@return */ PrivateT getTop (queue<t> output, queue<t>input) { /*** Add the output queue (except the tail element) to the input queue and return the tail element as the top element of the stack*/ while(Output.size ()! = 1) {Input.offer (Output.poll ()); } returnOutput.poll (); }}
Test
@Test Public voidTestcstack ()throwsException {cstack<Integer> queue =NewCstack<integer>(); Try{Queue.push (3); Queue.push (4); System.out.println (Queue.pop ()); System.out.println (Queue.pop ()); Queue.push (4); Queue.push (5); System.out.println (Queue.pop ()); System.out.println (Queue.pop ()); System.out.println (Queue.pop ()); } Catch(Exception e) {System.out.println (E.getmessage ()); } }
Results
Summarize
Clearly know about the characteristics of the queue (FIFO) and Stack (LIFO).
Using two stacks to implement a queue