Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report