java-用两个栈实现队列&&用两个队列实现一个栈

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;


/*

* Q 57 用两个栈实现队列

思路:对于insert,把数据插入到第一个堆栈中;
      对于remove,如果第二个堆栈为空,把第一个堆栈的所有元素pop出来并放入第二个堆栈中,然后返回第二个堆栈的第一个元素。

*/


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()){
re=stack2.pop();
}else{
while(!stack1.empty()){//move to stack2 to make stack1 have only one element.Then pop this element.
re=stack1.pop();
stack2.push(re);
}
if(!stack2.empty()){
re=stack2.pop();
}
}
return re;
}
public Integer offer(int o){
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 用两个队列实现一个栈

思路:对于Push: 如果两个队列都为空,就插入到第一个队列中;否则就插入到非空的那个队列中;
      对于Pop: 把非空的那个队列的每个元素remove出来,然后插入到另一个队列中,直到剩下最后一个元素,然后将其返回。

*/
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(){
Integer re=null;
if(queue1.size()==0&&queue2.size()==0){
return null;
}
if(queue2.size()==0){
while(queue1.size()>0){
re=queue1.removeFirst();
if(queue1.size()!=0){//do not add the last element of queue1 to queue2不将queue1最后一个元素放入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);
}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
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值