栈和队列的常见面试题-Java实现

栈和队列的常见面试题-Java实现


一:实现一个特殊的栈,在基本功能的基础上,再实现返回栈中最小元素的功能。
popElem、pushElem、getMinElem操作的时间复杂度都是O(1)。

    public static class MyStack{
        private static Stack<Integer> stackData;
        private static Stack<Integer> stackMin;

        public MyStack() {
            stackData = new Stack<>();
            stackMin = new Stack<>();
        }

        public int getMinElem(){
            if (stackMin.isEmpty()){
                System.out.println("栈为空!!");
            }
            return stackMin.peek();
        }

        public void pushElem(int newNum){
            if (stackMin.isEmpty()){
                stackMin.push(newNum);
            }else if (newNum < getMinElem()){
                stackMin.push(newNum);
            }else {
                stackMin.push(stackMin.peek());
            }
            stackData.push(newNum);
        }

        public int popElem(){
            stackMin.pop();
            return stackData.pop();
        }
    }

在这里插入图片描述
二:如何只用栈结构实现队列结构

从stackPush栈导数据到stackPop栈的规则
(1)stackPop栈为空
(2)导数据时,stackPush栈需一次性导出完

   public static class TwoStacksQueue {
        private Stack<Integer> stackPush;
        private Stack<Integer> stackPop;

        public TwoStacksQueue() {
            stackPush = new Stack<>();
            stackPop = new Stack<>();
        }

        //stackPush栈向stackPop栈中导入数据
        private void pushToPop() {
            if (stackPop.empty()) {
                while (!stackPush.isEmpty()){
                    stackPop.push(stackPush.pop());
                }
            }
        }

        public void add(int newNum) {
            stackPush.push(newNum);
            pushToPop();
        }

        public int poll() {
            if (stackPush.empty() && stackPop.empty()) {
                System.out.println("队列为空!!");
            }
            pushToPop();
            return stackPop.pop();
        }

        public int peek() {
            if (stackPush.empty() && stackPop.empty()) {
                System.out.println("队列为空!!");
            }
            pushToPop();
            return stackPop.peek();
        }
    }

三:如何只用队列结构实现栈结构
在这里插入图片描述

    public static class TwoQueueStack<T> {
        public Queue<T> queue;
        public Queue<T> help;

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

        public void push(T value) {
            queue.offer(value);
        }

        public T poll() {
            while (queue.size() > 1) {
                help.offer(queue.poll());
            }
            T ans = queue.poll();
            Queue<T> tmp = queue;
            queue = help;
            help = tmp;
            return ans;
        }

        public T peek() {
            while (queue.size() > 1) {
                help.offer(queue.poll());
            }
            T ans = queue.poll();
            help.offer(ans);
            Queue<T> tmp = queue;
            queue = help;
            help = tmp;
            return ans;
        }

        public boolean isEmpty() {
            return queue.isEmpty();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小本科生debug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值