Title: Implement a queue with two stacks. The declaration of the queue is as follows, implementing its two functions Appendtail and Deletedhead, respectively, to complete the insertion of nodes at the end of the queue and the ability to delete nodes at the head of the queue.
We analyze the process of inserting and deleting elements in this queue through a concrete example. First insert an element A, you might want to insert it into STACK1, when the element in Stack1 has {A},stack2 is empty. Press into two elements B and C, or insert into Stack1, at which point the element in Stack1 has {a,b,c}, where C is at the top of the stack, and Stack2 is still empty.
At this point, we try to delete an element from the queue. According to the queue first in first out rule, because a than B, C first inserted into the queue, the first element to be deleted should be a. Element A is stored in stack1, but not at the top, and therefore cannot be deleted directly. Notice that stack2 we have not used, now is the time to let Stack2 play a role. If we pop the elements in the Stack1 one by one and press them into Stack2, the elements in Stack2 are in the opposite order of the original stack1. So after 3 pop-up stack1 and press-in STACK2 operation, Stack1 is empty, and stack2 element is {c,b,a}, this time can pop up Stack2 stack top A. At this point the Stack1 is empty, and the Stack2 element is {c,b}, which is at the top of the stack:
Java Code Implementation:
/** * */package swordforoffer;import java.util.stack;/** * @author Jinshuangqi * * July 27, 2015 */public class E07queuew Ithtwostacks {/** * implements a queue with two stacks, completing two functions Appendtail and Deletedhead, respectively, the function of inserting nodes at the end of the queue and deleting nodes at the head of the queue */private stack< string> stack1 = new stack<string> ();p rivate stack<string> stack2 = new stack<string> ();p ublic void Appendtail (String s) {Stack1.push (s);} Public String Deletedhead () throws Exception{if (Stack2.isempty ()) {while (!stack1.isempty ()) {Stack2.push (Stack1.pop ( ));}} if (Stack2.isempty ()) {throw new Exception ("Queue is empty, cannot be deleted");} return Stack2.pop ();} public static void Main (string[] args) throws exception{e07queuewithtwostacks test = new E07queuewithtwostacks (); Test.appendtail ("1"); Test.appendtail ("2"); Test.deletedhead ();}}
The stack is implemented with two queues.
The process of simulating a stack with a queue is analyzed by a series of push-in and eject operations, and we first press an element a into the stack. Since two queues are now empty, we can choose to insert a into any of the two queues. We might as well insert a into the queue1. Next, continue to press the B,C two elements into the net stack. We insert them all into the queue1. This time queue1 contains 3 elements a,b,c where A is in the head of the queue, and C is at the tail of the queue.
Now let's consider popping an element from within the stack. According to the principle of the post-first out of the stack, the last C that is pressed into the stack should be ejected first. Since c is at the tail of queue1 and we can only delete elements from the head of the queue at a time, we can remove the a/b/c from Queueu, insert it into queue2, and remove C from queue1. This is equivalent to popping the element C from the stack. We can pop the element b from the stack in the same way.
Next we consider pressing an element d from within the stack. Now that queue1 has an element, we insert D into the tail of the queue1. If we eject an element from the stack again, it should be the last to be pushed in the D. Since d is at the tail of queue1, we can only remove the queue1 element from the head and insert it into queue2 until Queue1 encounters D and deletes it directly. If the following is true:
Java Code Implementation:
/** * */package swordforoffer;import java.util.linkedlist;/** * @author Jinshuangqi * * July 27, 2015 */public class E07Stac Kswithtwoqueue {private linkedlist<string> queue1;private linkedlist<string> queue2;public E07stackswithtwoqueue () {queue1 = new linkedlist<string> (); queue2 = new linkedlist<string> ();} Public String Pop () {string re =null;if (queue1.size () = = 0 && queue2.size () = = 0) {return null;} if (queue2.size () = = 0) {while (Queue1.size () >0) {re = Queue1.removefirst (); if (Queue1.size ()! = 0) {queue2.addlast (re) ;}}} else if (queue1.size () = = 0) {while (Queue2.size () >0) {re = Queue2.removefirst (); if (Queue2.size ()!=0) {Queue1.addlast (re);}}} return re;} public string push (String str) {if (Queue1.size () ==0 && queue2.size () = = 0) {queue1.addlast (str);} if (Queue1.size ()!=0) {queue1.addlast (str);} else if (queue2.size ()!=0) {queue2.addlast (str);} return str;} public static void Main (string[] args) {e07stackswithtwoqueue stack=new e07stackswithtwoqueue (); String tmp; Stack.push ("1"); Stack.push ("2"); Stack.push ("3"); Tmp=stack.pop (); SYSTEM.OUT.PRINTLN (TMP);//3 Stack.push ("4"); Tmp=stack.pop (); SYSTEM.OUT.PRINTLN (TMP);//4 Tmp=stack.pop (); SYSTEM.OUT.PRINTLN (TMP);//2 Stack.push ("5"); Stack.push ("6"); Tmp=stack.pop (); SYSTEM.OUT.PRINTLN (TMP);//6 Tmp=stack.pop (); SYSTEM.OUT.PRINTLN (TMP);//5 Tmp=stack.pop (); SYSTEM.OUT.PRINTLN (TMP);//1}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sword Point 7 (Java Edition): Implement queue with two stacks and implement stack with two queues