队列和栈

来源:互联网 发布:淘宝店铺年度运营计划 编辑:程序博客网 时间:2024/06/07 07:00

两个栈构成一个队列

问题描述:用两个栈组成一个队列这个很容易想出,队列的特点是先进先出,栈的特点是先进后出。思路:操作顺序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
原创粉丝点击