用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
先简单介绍下栈和队列
栈:栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。栈具有记忆作用,对栈的插入与删除操作中,不需要改变栈底指针。(先进后出or后进先出)
队列:队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。(先进先出)
实现方式1
package com.jason.algorithm;import java.util.Stack;public class QueueImplmentByStatck { public static void main(String[] args) { QueueImplmentByStatck queueImplmentByStatck = new QueueImplmentByStatck(); queueImplmentByStatck.push(1); queueImplmentByStatck.push(2); queueImplmentByStatck.push(3); System.out.PRintln(queueImplmentByStatck.pop()); System.out.println(queueImplmentByStatck.pop()); queueImplmentByStatck.push(4); System.out.println(queueImplmentByStatck.pop()); queueImplmentByStatck.push(5); System.out.println(queueImplmentByStatck.pop()); System.out.println(queueImplmentByStatck.pop()); } private int count = 0; // statck先进后出,Queue先进先出 Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { stack2.push(count); count++; return stack1.get(stack2.pop()); }}实现方式2package com.jason.algorithm;import java.util.Stack;public class QueueImplmentByStatck1 { public static void main(String[] args) { QueueImplmentByStatck1 queueImplmentByStatck = new QueueImplmentByStatck1(); queueImplmentByStatck.push(1); queueImplmentByStatck.push(2); queueImplmentByStatck.push(3); System.out.println(queueImplmentByStatck.pop()); System.out.println(queueImplmentByStatck.pop()); queueImplmentByStatck.push(4); System.out.println(queueImplmentByStatck.pop()); queueImplmentByStatck.push(5); System.out.println(queueImplmentByStatck.pop()); System.out.println(queueImplmentByStatck.pop()); } // statck先进后出,Queue先进先出 Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { // 当stack1不为null时,全部放到stack2里这时,stack2里就是和之前的stack1顺序刚好是反的 while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } int first = stack2.pop(); while (!stack2.isEmpty()) { stack1.push(stack2.pop()); } return first; }}实现方式3package com.jason.algorithm;import java.util.Stack;public class QueueImplmentByStatck2 { public static void main(String[] args) { QueueImplmentByStatck2 queueImplmentByStatck = new QueueImplmentByStatck2(); queueImplmentByStatck.push(1); queueImplmentByStatck.push(2); queueImplmentByStatck.push(3); System.out.println(queueImplmentByStatck.pop()); System.out.println(queueImplmentByStatck.pop()); queueImplmentByStatck.push(4); System.out.println(queueImplmentByStatck.pop()); queueImplmentByStatck.push(5); System.out.println(queueImplmentByStatck.pop()); System.out.println(queueImplmentByStatck.pop()); } // statck先进后出,Queue先进先出 Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { if (stack1.empty() && stack2.empty()) { throw new RuntimeException("Queue is empty!"); } // 每次stack2为空时,再去把stack1里内容放stack2 if (stack2.empty()) { while (!stack1.empty()) { stack2.push(stack1.pop()); } } return stack2.pop(); }}
新闻热点
疑难解答