用两个栈实现一个队列/用两个队列实现一个栈

1.用两个栈实现一个队列

mport java.util.Arrays;
class SeqStack<T> {
    private T[] stack;  //存储栈的元素的数组
    private int top;    //top表示栈顶的位置

    public SeqStack() {    //默认的构造函数,默认开辟十个位置的空间
        this(10);
    }

    public SeqStack(int size) {    //用户可以指定栈的大小size
        this.stack = (T[]) new Object[size];
        this.top = 0;
    }

    public void push(T val) {    //入栈操作
        if (full()) {     //如果栈满了进行二倍扩容
            this.stack = Arrays.copyOf(this.stack, this.stack.length * 2);
        }
        this.stack[this.top] = val;
        this.top++;
    }

    public void pop() {       //出栈操作
        if (empty())
            return;
        this.top--;
        if (this.top < this.stack.length / 2) {
            this.stack = Arrays.copyOf(this.stack, this.stack.length / 2);
        }
    }

    public T top() {     //获取栈顶元素
        return this.stack[this.top - 1];
    }

    public boolean full() {   //判断栈满
        return this.top == this.stack.length;
    }

    public boolean empty() {    //判断栈空
        return this.top == 0;
    }
}
    class Queue<E> {
        private SeqStack<E> s1;  //栈1
        private SeqStack<E> s2;  //栈二

        //构造函数 offer poll peek full empty
        public Queue() {
            this.s1 = new SeqStack<>();
            this.s2 = new SeqStack<>();
        }

        public void offer(E val) { 
            this.s1.push(val);    
        }
  
        public E poll() {//从s1出栈,把出栈后的最后一个元素返回
            E data = null;
            while (!this.s1.empty()) {//把s1里边的所有元素出队,放入s2里边,然后把s1最后一个出栈的元素直接返回,不用放入s2
                data = s1.top();
                s1.pop();
                if (s1.empty()) {
                    break;
                }
                this.s2.push(data);
            }

            while (!this.s2.empty()) { // 获取该出队的元素以后,再把s2的元素再放入s1里边
                this.s1.push(s2.top());
                s2.pop();
            }
            return data;
        }

        public boolean full() {
            return this.s1.full();
        }

        public boolean empty() {
            return this.s1.empty();
        }
    }
        /**
         * 描述:
         *
         * @Author shilei
         * @Date 2019/4/27
         */
        public class 用两个栈实现一个队列 {
            public static void main(String[] args) {
                Queue<Integer> q = new Queue<>();
                for (int i = 0; i < 10; ++i) {
                    q.offer(i);
                }

                while (!q.empty()) {
                    System.out.print(q.poll() + " ");
                }
                System.out.println();
            }
        }

2.用两个队列实现一个栈

/**
 * 循环队列 特点:先入先出、后入后出
 */
class Queue<E>{
    // 存储队列元素的数组
    private E[] que;
    // 表示队头的位置
    private int front;
    // 表示队尾的位置
    private int rear;

    /**
     * 默认构造队列,初始大小是10
     */
    public Queue(){
        this(10);
    }

    /**
     * 用户可以指定队列的大小size
     * @param size
     */
    public Queue(int size){
        this.que = (E[])new Object[size];
        this.front = this.rear = 0;
    }

    /**
     * 入队操作
     * @param val
     */
    public void offer(E val){
        if(full()){
            // 扩容
            E[] newQue = Arrays.copyOf(this.que,// int[] arr2=Arrays.copyOf(原数组名,新数组长度);
                    this.que.length*2);
            int index = 0;
            for(int i=this.front;
                i != this.rear;
                i=(i+1)%this.que.length){//??
                newQue[index++] = this.que[i];
            }
            this.front = 0;
            this.rear = index;
            this.que = newQue;
        }
        this.que[this.rear] = val;
        this.rear = (this.rear+1)%this.que.length;
    }

    /**
     * 出队操作,并把出队的元素的值返回
     */
    public E poll(){
        if(empty()){
            return null;
        }
        E front = this.que[this.front];
        this.front = (this.front+1)%this.que.length;
        return front;
    }

    /**
     * 查看队头元素
     * @return
     */
    public E peek(){
        if(empty()){
            return null;
        }
        return this.que[this.front];
    }

    /**
     * 判断队满
     * @return
     */
    public boolean full(){
        return (this.rear+1)%this.que.length == this.front;
    }

    /**
     * 判断队空
     * @return
     */
    public boolean empty(){
        return this.rear == this.front;
    }
}

/**
 * 让你用两个队列实现一个栈结构
 *
 * @param <E>
 */

/**
 * 描述:
 *
 * @Author shilei
 * @Date 2019/4/27
 */
class SeqStack<E>{
    private Queue<E> que1; // 存放栈的元素
    private Queue<E> que2; // 做一个辅助操作

    public SeqStack(){
        this.que1 = new Queue<>();
        this.que2 = new Queue<>();
    }

    public SeqStack(int size){
        this.que1 = new Queue<>(size);
        this.que2 = new Queue<>(size);
    }

    public void push(E val){
        this.que1.offer(val);
    }

    public E pop(){
        // 从que1出队,把最后一个出队的元素返回
        E data = null;
        /**
         * 把que1里面的所有元素出队,放入que2里面,
         * 然后把que1最后一个出队的元素直接返回,不用放入que2
         */
        while(!this.que1.empty()){
            data = this.que1.poll();
            if(this.que1.empty()){
                break;
            }
            this.que2.offer(data);
        }

        // 获取该出栈的元素以后,再把que2的元素再放入que1里面
        while(!this.que2.empty()){
            this.que1.offer(this.que2.poll());
        }

        return data;
    }

    public E top(){
        // 从que1出队,把最后一个出队的元素返回
        E data = null;

        while(!this.que1.empty()){
            data = this.que1.poll();
            this.que2.offer(data);
        }

        // 获取该出栈的元素以后,再把que2的元素再放入que1里面
        while(!this.que2.empty()){
            this.que1.offer(this.que2.poll());
        }

        return data;
    }

    public boolean full(){
        return this.que1.full();
    }

    public boolean empty(){
        return this.que1.empty();
    }
}

/**
 * 描述: Java的泛型编程 Object
 * 1. 免了类型强转
 * 2. 可以做类型检查     String val = (String)que.poll();
 *
 * @Author shilei
 * @Date 2019/4/27
 */
public class 两个队列实现一个栈 {
    public static void main(String[] args) {
        SeqStack<Integer> s = new SeqStack<>();
        for(int i=0; i<10; ++i){
            s.push(i);
        }

        while(!s.empty()){
            System.out.print(s.pop() + " ");
        }
        System.out.println();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_44822679/article/details/90033054