java 队列和栈相互实现
Posted king西阳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 队列和栈相互实现相关的知识,希望对你有一定的参考价值。
一、队列实现栈
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 队列和栈相互实现的主要内容,如果未能解决你的问题,请参考以下文章