1.1. 使用栈实现队列结构
思路
我们可以使用两个栈,一个起名为 pushStack
一个 popStack
,在添加元素时,使用 pushStack
来添加元素,当需要取出元素时,若 popStack
中无数据,将 pushStack
中所有数据全部弹出到popStack
中,然后弹出popStack
中的第一个元素,若 popStack
中有数据,则弹出时调用popStack
栈中的数据弹出。
注意
保证每次转移数据 popStack
为空而且pushStack
要全部弹出到popStack
package com.zhang.study.chapter03;
import java.util.Stack;
/**
* 使用栈结构实现队列
*/
public class Code09_StackImplQueue {
public static class MyQueue<T> {
Stack<T> pushStack;
Stack<T> popStack;
public MyQueue() {
this.pushStack = new Stack<>();
this.popStack = new Stack<>();
}
public void pushToPop(){
if(popStack.isEmpty()){
while(!pushStack.isEmpty()){
popStack.push(pushStack.pop());
}
}
}
public void push(T item) {
pushStack.push(item);
pushToPop();
}
public T pop() {
if (popStack.empty() && pushStack.empty()) {
throw new RuntimeException("Queue is empty!");
}
pushToPop();
return popStack.pop();
}
public T peek() {
if (popStack.empty() && pushStack.empty()) {
throw new RuntimeException("Queue is empty!");
}
pushToPop();
return popStack.peek();
}
}
public static void main(String[] args) {
MyQueue<Integer> queue = new MyQueue<>();
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
}
}
使用队列实现栈结构
思路
使用两个队列来反复横跳,一个正常的 queue
一个帮助队列 helpQueue
在添加数据时,使用queue
添加数据,需要取数据时,将数据移到 helpQueue
留下最后一个返回。然后交换 queue
和 helpQueue
引用地址。
package com.zhang.study.chapter03;
import com.sun.jmx.remote.internal.ArrayQueue;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* 使用队列实现栈结构
* 使用两个队列来反复横跳,一个正常的 queue一个帮助队列 helpQueue 在添加数据时,
* 使用queue添加数据,需要取数据时,将数据移到 helpQueue 留下最后一个返回。然后交换 queue和 helpQueue引用地址。
*/
public class Code10_QueueImplStack {
public static class QueueImplStack<T> {
public Queue<T> queue;
public Queue<T> help;
public QueueImplStack() {
queue = new LinkedList<T>();
help = new LinkedList<T>();
}
public void push(T t) {
queue.add(t);
}
public T poll() {
if (queue.isEmpty()) {
throw new RuntimeException("Queue is empty!");
}
while (queue.size() > 1) {
help.offer(queue.poll());
}
T poll = queue.poll();
Queue<T> temp = help;
help = queue;
queue = temp;
return poll;
}
public boolean isEmpty() {
return queue.isEmpty();
}
public T peek() {
if (queue.isEmpty()) {
throw new RuntimeException("Queue is empty!");
}
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 static void main(String[] args) {
System.out.println("test begin");
QueueImplStack<Integer> myStack = new QueueImplStack<>();
Stack<Integer> test = new Stack<>();
int testTime = 1000000;
int max = 1000000;
for (int i = 0; i < testTime; i++) {
if (myStack.isEmpty()) {
if (!test.isEmpty()) {
System.out.println("Oops");
}
int num = (int) (Math.random() * max);
myStack.push(num);
test.push(num);
} else {
if (Math.random() < 0.25) {
int num = (int) (Math.random() * max);
myStack.push(num);
test.push(num);
} else if (Math.random() < 0.5) {
if (!myStack.peek().equals(test.peek())) {
System.out.println("Oops");
}
} else if (Math.random() < 0.75) {
if (!myStack.poll().equals(test.pop())) {
System.out.println("Oops");
}
} else {
if (myStack.isEmpty() != test.isEmpty()) {
System.out.println("Oops");
}
}
}
}
System.out.println("test finish!");
}
}