队列和栈

两个栈构成一个队列

问题描述:用两个栈组成一个队列
这个很容易想出,队列的特点是先进先出,栈的特点是先进后出。
思路:操作顺序push(1),push(2),push(3),push(4),pop(),pop(),push(5),push(6),pop().pop()....
对于push(1),push(2),push(3),push(4),先存入一个stack1中,下一个操作pop(),则需要将stack1中的全部数,从stack1中弹出,再弹入stack2中,执行pop(),pop(),后,stack2中还有2,1。所以,push(5),push(6),是先将5,6存入stack1中,然后,将stack2中的数字一次弹出(1,2),再将5,6弹入stack2,

总之一句话就是,必须将stack2中的数字全部弹出后,才能弹入

public class Test<T> {
    /*
     * 用两个栈实现 一个队列
     */
    Stack<T> stack1=new Stack<T>();
    Stack<T> stack2=new Stack<T>();
    int size=0;
    public void push(T t){
        stack1.push(t);
        size++;
    }
    public T pop(){
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        size--;
        return stack2.pop();
    }
    public int size(){
        return size;
    }
    public T peek(){
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }
    public boolean isEmpty(){
        boolean result=true;
        if(size==0){
            result=false;
        }
        return result;
    }
}

两个队列构成一个栈

问题描述:两个队列构成一个栈

这里写图片描述
这里写图片描述

这时,将4弹出,如果要加入新的元素,则加到queue2中,则要再次弹出时,讲queue2中,除栈顶元素全部弹出到queue1中,再将queue2中的唯一元素弹出。
public class Test<T> {
    /*
     * 两个队列组成一个栈
     */

    Queue<T> queue1=new LinkedList<T>();
    Queue<T> queue2=new LinkedList<T>();
    int size=0;
    public void push(T t){
        if(!queue1.isEmpty()){
            queue1.offer(t);
        }
        else{
            queue2.offer(t);
        }
        size++;
    }
    public T pop(){
        T pop=null;
        if(!queue1.isEmpty()){
            while(queue1.size()>1){
                queue2.offer(queue1.poll());
            }
            pop=queue1.poll();
        }
        else{
            while(queue2.size()>1){
                queue1.offer(queue2.poll());
            }
            pop=queue2.poll();
        }
        size--;
        return pop;
    }
    public int size(){
        return size;
    }
    public T peek(){
        T pop=null;
        if(!queue1.isEmpty()){
            while(queue1.size()>1){
                queue2.offer(queue1.poll());
            }
            pop=queue1.peek();
            queue2.offer(queue1.poll());
        }
        else{
            while(queue2.size()>1){
                queue1.offer(queue2.poll());
            }
            pop=queue2.peek();
            queue1.offer(queue2.poll());
        }
        return pop;
    }
}

求栈中的最小元素

问题描述:设计一个栈,除了能够求最基本的栈元素外,还能够求得栈中的最小元素。

这里写图片描述

/*
 * 实现最小栈
 */
public class MinStack{
    Stack<Integer> stack =new Stack<Integer>();
    Stack<Integer> ministack=new Stack<Integer>();
    int min=0;
    public void push(Integer t){
        stack.push(t);
        if(ministack.isEmpty()){
            min=t;
            ministack.push(min);
        }
        else{
            if(t<min)min=t;
            ministack.push(min);
        }
    }
    public int pop(){
        return ministack.pop();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值