Get the App
SLTechnology News&Howtos  ›  Servers  › 

How Java implements queues with two stacks and one stack with two queues

Shulou Source: shulou.com Published: 2022-05-31 17:56:51 10月04日 Update

This article mainly explains the "Java how to use two stacks to achieve a queue and two queues to achieve a stack", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Java how to use two stacks to achieve queues and two queues to achieve a stack" bar!

Import java.util.ArrayList;import java.util.List;import java.util.Stack; / * * Q 57 implements queue * / public class QueueImplementByTwoStacks {private Stack stack1; private Stack stack2; QueueImplementByTwoStacks () {stack1=new Stack (); stack2=new Stack ();} public Integer poll () {Integer re=null with two stacks If (! stack2.empty ()) {/ / if stack2 is not empty, pop up the top stack element re=stack2.pop () } else {/ / if stack2 is empty, pop up the elements in stack1 and press them into stack2 in turn, then the elements at the bottom of stack1 stack will become the elements at the top of stack2 stack, thus reaching the queue order of stack1 first-in-first-out queue while (! stack1.empty ()) {re=stack1.pop (); stack2.push (re) } if (! stack2.empty ()) {re=stack2.pop ();}} return re;} public Integer offer (int o) {/ / only press stack stack1.push (o) from stack1 each time; return o;} public static void main (String [] args) {QueueImplementByTwoStacks queue=new QueueImplementByTwoStacks () List re=new ArrayList (); queue.offer (1); queue.offer (2); queue.offer (3); re.add (queue.poll ()); queue.offer (4); re.add (queue.poll ()); queue.offer (5); re.add (queue.poll ()); re.add (queue.poll ()) Re.add (queue.poll ()); System.out.println (re.toString ());}} import java.util.LinkedList;/* * Q 57 implements a stack with two queues * / public class StackImplementByTwoQueues {/ / use 'queue1' and' queue2' as a queue.That means only use the method 'addLast' and' removeFirst'. Private LinkedList queue1; private LinkedList queue2; StackImplementByTwoQueues () {queue1=new LinkedList (); queue2=new LinkedList ();} public Integer pop () {/ / queue 1 is not empty, queue 2 is empty, and the queue sequence is read into queue 2, then the last element is the pop-up value Integer re=null; if (queue1.size () = = 0&&queue2.size () = 0) {return null } if (queue2.size () = = 0) {while (queue1.size () > 0) {re=queue1.removeFirst (); / / equivalent to re=queue1.remove (); if (queue1.size ()! = 0) {/ / do not add the last element of queue1 to queue2 queue2.addLast (re) } else if (queue1.size () = = 0) {while (queue2.size () > 0) {re=queue2.removeFirst (); if (queue2.size ()! = 0) {/ / do not add the last element of queue2 to queue1 queue1.addLast (re) } return re;} public Integer push (Integer o) {if (queue1.size ()) = = 0&&queue2.size () = = 0) {queue1.addLast (o); / / queue2.addLast (o); is also ok} if (queue1.size ()! = 0) {queue1.addLast (o) / equivalent queue1.=add (0)} else if (queue2.size ()! = 0) {queue2.addLast (o);} return o;} public static void main (String [] args) {StackImplementByTwoQueues stack=new StackImplementByTwoQueues (); int tmp=0; 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}} Thank you for your reading, the above is the content of "how Java implements queues with two stacks and one stack with two queues". After the study of this article, I believe you have a deeper understanding of how Java implements queues with two stacks and one stack with two queues, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Queue two elements learning content that is order advanced ideas situations articles more knowledge points articles follow questions first out practice push Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Huawei Docker Redmi