Java栈和队列基础问题实现

问题列表

使用数组实现栈结构

 /**
     * 基于数组实现栈
     */
    public  class ArrayStack {
        private Integer[] arr;
        private Integer size;

        public ArrayStack(int capacity) {
            if (capacity < 0) {
                throw new IllegalArgumentException("The capacity <0");
            }
            arr = new Integer[capacity];
            size = 0;
        }

        public Integer peek() {
            if (size == 0) {
                return null;
            }
            return arr[size - 1];
        }

        public Integer pop() {
            if (size == 0) {
                throw new ArrayIndexOutOfBoundsException("The stack is empty");
            }
            return arr[--size];
        }

        public void push(int target) {
            if (size == arr.length) {
                throw new ArrayIndexOutOfBoundsException("The stack is full.");
            }
            arr[size++] = target;
        }

    }

使用数组实现队列结构

 public static class ArrayQueue {
        private Integer[] arr;
        private Integer size;
        private int first;
        private int last;

        public ArrayQueue(int capacity) {
            if (capacity <= 0) {
                throw new IllegalArgumentException("The capacity size <0");
            }
            arr = new Integer[capacity];
            size = 0;
            first = 0;
            last = 0;
        }

        public Integer peek() {
            if (size == null) {
                return 0;
            }
            return arr[first];
        }


        public void push(int target) {
            if (size == arr.length) {
                //队列已满
                throw new ArrayIndexOutOfBoundsException("The queue is full");
            }
            size++;
            arr[last] = target;
            last = last == arr.length - 1 ? 0 : last + 1; //到达数组最后一个位置后就移动到第一个为止
        }


        public Integer pop() {
            if (size == 0) {
                //队列为空
                throw new ArrayIndexOutOfBoundsException("The queue is empty");
            }
            size--;
            int index = first;
            first = first == arr.length - 1 ? 0 : first + 1;//到达数组最后一个位置后就移动到第一个为止
            return arr[index];
        }
    }

实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。

/**
 * 实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
 * 思路:
 * 1.构造两个栈,一个存放数据的data栈,一个存放最小值的min栈;
 * 2.入栈的时候往data栈中存入,同时和min栈顶做比较,把最小值压入min栈,保证min栈顶永远是栈中的最小元素,同时data栈和min栈元素size相等;
 * 3.出栈时,data栈出栈,同时min栈也出栈,min栈栈顶元素即为栈中最小元素
 */
public class MinStack {
    private Stack<Integer> dataStack;
    private Stack<Integer> minStack;

    private MinStack() {
        dataStack = new Stack<>();
        minStack = new Stack<>();
    }

    /**
     * peek操作只获取当前栈顶元素,不做出栈操作
     *
     * @return
     */
    public Integer peek() {
        return dataStack.peek();
    }

    /**
     * pop操作需要dataStack和minStack同时出栈
     */
    public Integer pop() {
        Integer pop = dataStack.pop();
        //同时需要移除minStack中的栈顶元素
        minStack.pop();
        return pop;
    }

    /**
     * push操作的时候,往dataStack中入栈,同时计算最小值添加到minStack栈中
     *
     * @param target
     */
    public void push(Integer target) {
        dataStack.push(target);
        if (minStack.isEmpty()) {
            minStack.push(target);
        } else {
            minStack.push(Math.min(target, getMin()));
        }
    }

    public Integer getMin() {
        return minStack.peek();
    }
}

用队列结构实现栈结构

  /**
     * 栈实现队列
     * 思路:根据栈的特性,构造两个栈,来回倒,从而实现先进先出流程;
     */
    public static class StackImplQueue {
        private Stack<Integer> pushStack;
        private Stack<Integer> popStack;

        public StackImplQueue() {
            pushStack = new Stack<>();
            popStack = new Stack<>();
        }

        public void push(int target) {
            pushStack.push(target);
        }

        public int pop() {
            if (popStack.isEmpty() && pushStack.isEmpty()) {
                throw new RuntimeException("Queue is empty");
            }
            if (popStack.isEmpty()) {
                while (!pushStack.isEmpty()) {
                    popStack.push(pushStack.pop());
                }
            }
            return popStack.pop();
        }

        public int peek() {
            if (popStack.isEmpty() && pushStack.isEmpty()) {
                throw new RuntimeException("Queue is empty");
            }
            if (popStack.isEmpty()) {
                while (!pushStack.isEmpty()) {
                    popStack.push(pushStack.pop());
                }
            }
            return popStack.peek();
        }
    }

队列实现栈结构

   public static class QueueImplStack {
        private Queue<Integer> queue;
        private Queue<Integer> help;

        public QueueImplStack() {
            queue = new LinkedList<>();
            help = new LinkedList<>();
        }

        public void push(int target) {
            queue.add(target);
        }

        /**
         * 不断把queue中元素丢到help队列中,然后取出最后一个元素,即表示栈顶元素,同时也需要把最后一个元素存入help队列【peek操作,不移除元素】,再修改引用
         *
         * @return
         */
        public int peek() {
            if (queue.isEmpty()) {
                throw new RuntimeException("Stack is empty");
            }
            while (queue.size() > 1) {
                help.add(queue.poll());
            }
            int result = queue.poll();
            help.add(result);
            swap();//调整下引用
            return result;
        }


        /**
         * 不断把queue中元素丢到help队列中,然后取出最后一个元素,即表示栈顶元素,同时不需要把最后一个元素存入help队列【pop操作,移除元素】,再修改引用
         *
         * @return
         */
        public int pop() {
            if (queue.isEmpty()) {
                throw new RuntimeException("Stack is empty");
            }
            while (queue.size() > 1) {
                help.add(queue.poll());
            }
            int result = queue.poll();
            swap();
            return result;

        }

        private void swap() {
            Queue<Integer> temp = help;
            help = queue;
            queue = temp;
        }

    }

结语

如果以上文章对您有一点点帮助,希望您不要吝啬的点个赞加个关注,您每一次小小的举动都是我坚持写作的不懈动力!ღ( ´・ᴗ・` )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值