Title Description
Two stacks to achieve a queue, the completion queue Push and Pop operations. Queue elements int.
Stack <Integer> = stack1 new new Stack <Integer> (); Stack <Integer> = stack2 divided by 2. new new Stack <Integer> (); public void Push ( int Node) {
// value of the stack 2 in stack1 all copy-in while (! {stack2.isEmpty ()) stack1.push (stack2.pop ()); } stack1.push (Node); } public int POP () {
// the two in a stack, the stack is copy-in output order : FIFO the while (! stack1.isEmpty ()) { stack2.push (stack1.pop ()); } return stack2.pop(); }
Second way:
public void push2 ( int Node) { stack1.push (Node); // data into the stack. 1 } public int POP2 () { IF (stack2.isEmpty ()) {// if there is no data in two stacks, the stack a data copying in the stack 2 has the direct output // the value of the first stack to the second stack into all the while (! stack1.isEmpty ()) { stack2.push (stack1.pop ()) ; } } // remove the value of the second stack return stack2.pop (); }