Two stack implementation queue + two queue implementation stack----Java

Source: Internet
Author: User

Two stack implementation queue + two queue implementation stack----Java

I. Two stacks to implement a queue

Idea: all elements into the stack1, then all out of stack1 and into stack2. Implementation of the first-in-order queue is: If the Stack2 is not empty, we need to stack top, out of the stack; To add elements to the queue, that is, advanced sack1, if the Stack2 is not empty when the stack, When empty, put stack1 all the stack into Stack2

Package Com.sheepmu;import Java.util.stack;public class Stackstoqueue {stack<integer> stack1=new Stack< Integer> (); Stack<integer> stack2=new stack<integer> ();  public void Addtotail (int x)//add element to the end of the queue--Enter the team---{stack1.push (x); } public int Deletehead ()//delete the first--out of the team---do not need to be the team is not empty ability to delete ~ ~ ~ ~ ~ ~ ~ ~ {if (pSize ()!=0)//queue is not empty {if (Stack2.isempty ())//If Stack2 is  Empty, then put Stack1 all join Stack2 Stack1tostack2 ();  return Stack2.pop ();  } else {System.out.println ("queue is already empty, cannot run from team head out"); return-1;}  } public void Stack1tostack2 ()//Put stack1 all into Stack2 {while (!stack1.isempty ()) Stack2.push (Stack1.pop ());} public int pSize ()//Queue size () {return stack1.size () +stack2.size ();//Two empty queue is empty} public static void Main (string[] args) {stackstoqueue q=new stackstoqueue (); Q.addtotail (1); Q.addtotail (2); Q.addtotail (3); Q.addtotail (4); System.out.println (Q.deletehead ()); System.out.println (Q.deletehead ()); Q.addtotail (5); System.out.println (Q.deletehead ()); System.out.println (Q.deletehead ()); SystEm.out.println (Q.deletehead ()); System.out.println (Q.deletehead ()); }}

Output:

1
2
3
4
5
Queue is empty and cannot be run from team head out
-1

Two. Two queues to implement a stack

Ideas: All elements into the Q1, because we are the purpose of the stack, that is, the first C, the son team from the head started out, all first AB out Q1 into Q2, at this time the target C ran to the team head, out of the Q1. At this time Q1 has been empty, the next to be out of B, a from Q2 out of the team Q1, at this time goal B in the Q2 team head, out of the team ....

That is: the non-empty queue of n-1 to the column, the remaining nth out of the team ... That is, there is always a queue that is empty.


PS: Photo Original from Cambridge offer, from the network

Package Com.sheepmu;import Java.util.linkedlist;public class Queuestostack {linkedlist<integer> queue1=new Linkedlist<integer> (); Linkedlist<integer> queue2=new linkedlist<integer> ();p ublic void push (int value)//Enter stack {queue1.addlast ( value);} public int pop ()///The stack must be non-empty stack talent out of the stack ah {if (ssize ()!=0)//stack is not empty {//move a team n-1 to also have a medium if (!queue1.isempty ())//q1 empty {Putn_ 1ToAnthor (); return Queue1.removefirst ();} else//Q2 null {putn_1toanthor (); return Queue2.removefirst ();}} Else{system.out.println ("Stack is empty, cannot be out of stack"); return-1;}} public int ssize () {return queue1.size () +queue2.size ();} public void Putn_1toanthor ()//from non-airborne n-1 to a queue because the queue is always empty one non-empty {if (!queue1.isempty ()) {while (Queue1.size () >1) { Queue2.addlast (Queue1.removefirst ());}} else if (!queue2.isempty ()) {while (Queue2.size () >1) {Queue1.addlast (Queue2.removefirst ())}}} public static void Main (string[] args) {queuestostack stack=new queuestostack (); Stack.push (1); Stack.push (2); Stack.push (3); Stack.push (4); System.out.println (Stack.pop ()); System.out.pRintln (Stack.pop ()); Stack.push (5); Stack.push (6); System.out.println (Stack.pop ()); System.out.println (Stack.pop ()); System.out.println (Stack.pop ()); System.out.println (Stack.pop ()); System.out.println (Stack.pop ());}}
Output:

4
3
6
5
2
1
Stack is empty, cannot be out of the stack
-1


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.