码迷,mamicode.com
首页 > 编程语言 > 详细

java 队列和栈相互实现

时间:2018-07-18 16:57:47      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:lse   array   结果   boolean   实现   null   ret   ati   new   

一、队列实现栈

public class queue2stack {

    public static void main(String[] args) {
QS qs
= new QS(); qs.push("1"); qs.push("2"); qs.push("3"); System.out.println(qs.pop()); System.out.println(qs.pop()); System.out.println(qs.peek()); QS qs2 = new QS(); qs2.push("1"); qs2.push("2"); qs2.push("3"); System.out.println(qs2.pop()); System.out.println(qs2.pop()); System.out.println(qs2.peek()); } static class QS{ private Queue queueMain = new ArrayDeque(); private Queue queueWork = new ArrayDeque(); public boolean push(Object object){ try { queueMain.offer(object); return true; } catch (Exception e) { return false; } } public int size() { return queueMain.size(); } public Object pop(){ if(queueMain.isEmpty()) { return null; } Queue temp = new ArrayDeque(); int size = queueMain.size(); for (int i = 0; i < size - 1; i ++) { temp.offer(queueMain.poll()); } Object o = queueMain.poll(); int size1 = temp.size(); for (int i = 0; i < size1; i ++) { queueMain.offer(temp.poll()); } return o; } public Object peek(){ if(queueMain.isEmpty()) { return null; } Queue temp = new ArrayDeque(); int size = queueMain.size(); for (int i = 0; i < size - 1; i ++) { temp.offer(queueMain.poll()); } Object o = queueMain.peek(); temp.offer(queueMain.poll()); int size1 = temp.size(); for (int i = 0; i < size1; i ++) { queueMain.offer(temp.poll()); } return o; } public boolean empty(){ return queueMain.isEmpty(); } /**********优化***********/ public boolean push2(Object object){ try { if (queueMain.isEmpty() && queueWork.isEmpty()) { queueMain.offer(object); } if (queueMain.isEmpty()) { queueWork.offer(object); } if(queueWork.isEmpty()) { queueMain.offer(object); } return true; } catch (Exception e) { return false; } } public Object pop2(){ if(queueMain.isEmpty() && queueWork.isEmpty()) { return null; } if (queueMain.isEmpty()) { while (queueWork.size() > 1) { queueMain.offer(queueWork.poll()); } return queueWork.poll(); } if (queueWork.isEmpty()) { while (queueMain.size() > 1) { queueWork.offer(queueMain.poll()); } return queueMain.poll(); } return null; } public Object peek2(){ if(queueMain.isEmpty() && queueWork.isEmpty()) { return null; } if (queueMain.isEmpty()) { while(queueWork.size() > 1) { queueMain.offer(queueWork.poll()); } Object e = queueWork.peek(); queueMain.offer(queueWork.poll()); return e; } if (queueWork.isEmpty()) { while(queueMain.size() > 1) { queueWork.offer(queueMain.poll()); } Object e = queueMain.peek(); queueWork.offer(queueMain.poll()); return e; } return null; } } }

二、栈实现队列

public class Stack2queue {

    public static void main(String[] args) {

        SQ sq = new SQ();
        sq.offer("a");
        sq.offer("b");
        sq.offer("c");
        System.out.println(sq.poll());
        System.out.println(sq.poll());
        sq.offer("d");
        sq.offer("e");
        System.out.println(sq.poll());
        System.out.println(sq.peek());
        System.out.println(sq.poll());
        System.out.println(sq.poll());

    }

    static class SQ{

        private Stack stackMain = new Stack();
        private Stack stackWork = new Stack();

        public boolean offer(Object ele) {
            stackMain.push(ele);
            return true;
        }

        public Object poll() {
            if (stackWork.empty()) {
                while(stackMain.size() > 0) {
                    stackWork.push(stackMain.pop());
                }
            }

            if (stackWork.empty()) {
                return null;
            }
            return stackWork.pop();
        }

        public Object peek() {
            if (stackWork.empty()) {
                while(stackMain.size() > 0) {
                    stackWork.push(stackMain.pop());
                }
            }

            if (stackWork.empty()) {
                return null;
            }
            return stackWork.peek();
        }
    }

}

结果自行运行测试

java 队列和栈相互实现

标签:lse   array   结果   boolean   实现   null   ret   ati   new   

原文地址:https://www.cnblogs.com/kingsonfu/p/9329411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!