Two queues to implement a stack + two stacks to implement a queue Java__java

Source: Internet
Author: User
Interviews often appear to let you write two of the queue to implement a stack, two stacks to achieve a queue problem, it is a headache. Today I will carefully analyze, think of the Java code to share with you:

(i) two queues to implement a stack:


Two queues add elements, which team columns are empty, because when the element is output, to move the corresponding element (excluding the tail element), the element is added to the queue that is not empty, and when the data is output, a two-queue operation is performed, and an empty queue adds elements to the empty queue in turn. Until the tail element is output.

/** * * * @author SUPERYC * Two queues implement a stack */public class Twoqueueimplstack {queue<integer> queue1 = new Arrayde
	Que<integer> ();
	
	queue<integer> queue2 = new arraydeque<integer> (); * * * push data into stack/public void push (Integer Element) {//Two queues are empty, give priority to queue1 if (Queue1.isempty () && queue2.i
			Sempty ()) {Queue1.add (element);
		Return
			//If queue1 is empty, queue2 has data, put directly into queue2 if (Queue1.isempty ()) {Queue2.add (element);
		Return
			//If Queue2 is empty, queue1 has data, put directly into queue1 if (Queue2.isempty ()) {Queue1.add (element);
		Return } * * * A data was popped from the stack/* public Integer Pop () {//If two stacks are empty, no elements can be ejected, exception if (Queue1.isempty () && queue2.ise
			Mpty ()) {try{throw new Exception ("Satck is empty!");
			}catch (Exception e) {e.printstacktrace ();  }//If there are no elements in the queue1, the elements in the queue2, and then the elements in their queue2 into the queue1 until the last element pops up if (Queue1.isempty ()) {while (Queue2.size ())
			> 1) {queue1.add (Queue2.poll ()); } return QUeue2.poll (); ///If there are no elements in the queue2, the elements in the queue1, and then the elements in their queue1 into the queue2 until the last element pops up if (Queue2.isempty ()) {while (Queue1.size ()) ;
			1) {Queue2.add (Queue1.poll ());
		return Queue1.poll ();
	return (Integer) null;
		public static void Main (string[] args) {twoqueueimplstack qs = new Twoqueueimplstack ();
		Qs.push (2);
		Qs.push (4);
		Qs.push (7);
		Qs.push (5);
		System.out.println (Qs.pop ());
		
		System.out.println (Qs.pop ());
		Qs.push (1);
	System.out.println (Qs.pop ()); }
}

(ii) two stacks to implement a queue:


The first stack is solely responsible for adding elements, and the second stack, when the element is ejected, first determines whether the current stack is empty, if it is empty directly to the first stack of data in the second stack, and then output the top elements of the stack, you can achieve the queue effect; If there is data in the second stack, add the data directly into the first stack, Output directly to the second stack of the top of the element can be.

/**
 * * 
 * @author superyc
 * Two stacks implement a queue
 */public
class Twostackimplqueue {

	stack< integer> stack1 = new stack<integer> ();
	stack<integer> Stack2 = new stack<integer> ();
	 * * * Queue data compaction Process * * Public
	void push (Integer element) {
		Stack1.push (element);
	}
	 * * * The data eject process for the queue */public
	Integer pop () {
		if (stack2.size () <= 0) {	//The second stack is null while
			( Stack1.size () > 0) {	//The first stack is not empty
				Stack2.push (Stack1.pop ());	Press the data on its first stack into the second stack
			}
		}
		if (Stack2.isempty ()) {
			try{
				throw new Exception ("Queue is empty");
			} catch (Exception e) {
				//e.printstacktrace ();
			}
		}
		Integer head = Stack2.pop ();
		return head;
	}
	
	public static void Main (string[] args) {
		Twostackimplqueue sq = new Twostackimplqueue ();
		Sq.push (1);
		Sq.push (3);
		Sq.push (5);
		Sq.push (4);
		Sq.push (2);
		
		System.out.println (Sq.pop ());
		System.out.println (Sq.pop ());
		
		Sq.push (7);
		System.out.println (Sq.pop ());
	}


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.