zoukankan      html  css  js  c++  java
  • 用两个栈实现队列

    要求:编写一个类,用两个栈实现队列,支持队列的基本操作(add、poll、peek)。

    add 增加一个元索  如果队列已满,则抛出一个IIIegaISlabEepeplian异常
    remove 移除并返回队列头部的元素  如果队列为空,则抛出一个NoSuchElementException异常
    element  返回队列头部的元素  如果队列为空,则抛出一个NoSuchElementException异常
    offer 添加一个元素并返回true  如果队列已满,则返回false
    poll 移除并返问队列头部的元素  如果队列为空,则返回null
    peek 返回队列头部的元素  如果队列为空,则返回null
    put 添加一个元素  如果队列满,则阻塞
    take 移除并返回队列头部的元素  如果队列为空,则阻塞

    思路:栈的特点:先进先出; 队列的特点:先进后出;用两个栈正好顺序反过来,可以实现队列的操作;

    方法: 一个压入栈:只完成push操作,stackPush

           一个弹出栈:只完成pop操作,stackPop

    3

    【重要】要完成如上操作,必须满足:

    stackPush 执行pop 操作至stackPop时,stackPush必须全部倒出 & stackPop必须为null

    public class Problem02_TwoStacksImplementQueue {
        public static class TwoStacksQueue {
            public Stack<Integer> stackPush;
            public Stack<Integer> stackPop;
    
            public TwoStacksQueue() {
                stackPush = new Stack<Integer>();
                stackPop = new Stack<Integer>();
            }
    
            /*
             * add操作: stackPush中执行push操作
             */
            public void add(int pushInt) {
                stackPush.push(pushInt);
            }
            
            /*
             * poll操作: 移除并返问队列头部的元素【注意队列为空的情况】
             * 1.stackPush  stackPop均为空时,队列为空队列,print 异常;
             * 2. 执行poll操作,即stackPush不为空时,将stackPush.pop的元素push进stackPop中【前提条件stackPop为空】
             * * 执行完“倒”的操作后,返回stackPop.pop()
             */
            public int poll() {
                if (stackPop.empty() && stackPush.empty()) {
                    throw new RuntimeException("Queue is empty!");
                } else if (stackPop.empty()) {
                    while (!stackPush.empty()) {
                        stackPop.push(stackPush.pop());
                    }
                }
                return stackPop.pop();
            }
    
            /*
             * peek操作: 返回队列头部的元素  如果队列为空,则返回null【注意队列为空的情况】
             * 1.stackPush  stackPop均为空时,队列为空队列,print 异常;
             * 2. 执行peek操作,即stackPush不为空时,将stackPush.pop的元素push进stackPop中【前提条件stackPop为空】
             * 执行完“倒”的操作后,返回stackPop.peek()
             */
            public int peek() {
                if (stackPop.empty() && stackPush.empty()) {
                    throw new RuntimeException("Queue is empty!");
                } else if (stackPop.empty()) {
                    while (!stackPush.empty()) {
                        stackPop.push(stackPush.pop());
                    }
                }
                return stackPop.peek();
            }
        }
    
        public static void main(String[] args) {
            TwoStacksQueue test = new TwoStacksQueue();
            test.add(1);
            test.add(2);
            test.add(3);
            System.out.println(test.peek());
            System.out.println(test.poll());
            System.out.println(test.peek());
            System.out.println(test.poll());
            System.out.println(test.peek());
            System.out.println(test.poll());
        }
    
    }

    运行结果:

    1
    1
    2
    2
    3
    3
  • 相关阅读:
    用人之道(一) 如何组建软件开发队伍[转]
    用人之道(二) 何管理软件开发团队[转]
    2005年度世界500强公司名单[转]
    人类的15个欲望与游戏设计[转&收藏]
    Flash读取Cookie[转]
    高效程序员应该养成的七个习惯
    六度隔离学说,1967年,哈佛大学,Stanley Milgram
    做技术,切不可沉湎于技术[转&收藏]
    庆祝VSX团队成立,加入VSX团队申请帖
    如何把菜单添加到另外一个VSPackage的菜单里?
  • 原文地址:https://www.cnblogs.com/xiyuan2016/p/6796797.html
Copyright © 2011-2022 走看看