Java怎么用两个栈实现队列和用两个队列实现一个栈

55次阅读
没有评论

共计 2869 个字符,预计需要花费 8 分钟才能阅读完成。

这篇文章主要讲解了“Java 怎么用两个栈实现队列和用两个队列实现一个栈”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着丸趣 TV 小编的思路慢慢深入,一起来研究和学习“Java 怎么用两个栈实现队列和用两个队列实现一个栈”吧!

import java.util.ArrayList;
import java.util.List;
import java.util.Stack; 
 
 /* 
 * Q 57  用两个栈实现队列  
 */
public class QueueImplementByTwoStacks {
 private Stack Integer  stack1;
 private Stack Integer  stack2;
 QueueImplementByTwoStacks(){
 stack1=new Stack Integer 
 stack2=new Stack Integer 
 }
 public Integer poll(){
 Integer re=null;
 if(!stack2.empty()){// 如果 stack2 不为空则弹出栈顶元素
 re=stack2.pop();
 }else{// 如果 stack2 为空,将 stack1 中的元素弹出,依次压栈到 stack2 中,那么 stack1 栈底的元素就会成为 stack2 栈顶的元素,从而达到 stack1 先进先出的队列顺序
 while(!stack1.empty()){ re=stack1.pop();
 stack2.push(re);
 }
 if(!stack2.empty()){ re=stack2.pop();
 }
 }
 return re;
 }
 public Integer offer(int o){// 每次只从 stack1 压栈
 stack1.push(o);
 return o;
 }
 public static void main(String[] args) { QueueImplementByTwoStacks queue=new QueueImplementByTwoStacks();
 List Integer  re=new ArrayList Integer 
 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  用两个队列实现一个栈  
 */
public class StackImplementByTwoQueues {
 //use  queue1  and  queue2  as a queue.That means only use the method  addLast  and  removeFirst . 
 private LinkedList Integer  queue1;
 private LinkedList Integer  queue2;
 StackImplementByTwoQueues(){
 queue1=new LinkedList Integer 
 queue2=new LinkedList Integer 
 }
 public Integer pop(){// 队列 1 不为空,队列 2 为空,将队列顺序读到队列 2 中,则最后一个元素就是需要弹出的值
 Integer re=null;
 if(queue1.size()==0 queue2.size()==0){
 return null;
 }
 if(queue2.size()==0){ while(queue1.size() 0){ re=queue1.removeFirst();// 等同 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);// 等同 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 
 }
}

感谢各位的阅读,以上就是“Java 怎么用两个栈实现队列和用两个队列实现一个栈”的内容了,经过本文的学习后,相信大家对 Java 怎么用两个栈实现队列和用两个队列实现一个栈这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是丸趣 TV,丸趣 TV 小编将为大家推送更多相关知识点的文章,欢迎关注!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计2869字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)