栈结构实现队列,队列结构实现栈

pull/3/head
Leo 5 years ago
parent 25b5d1834c
commit 11c46f9587

@ -0,0 +1,107 @@
package leo.class02;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* @author Leo
* @ClassName TwoQueueImplementStack
* @DATE 2020/11/21 12:29
* @Description
*/
public class TwoQueueImplementStack {
public static class MyStack<T>{
private Queue<T> queue;
private Queue<T> help;
public MyStack() {
queue = new LinkedList<>();
help = new LinkedList<>();
}
public void push(T value) {
queue.offer(value);
}
/**
* queue,push
* queue
*/
public T pop() {
while (queue.size() > 1) {
help.offer(queue.poll());
}
T value = queue.poll();
Queue<T> temp = queue;
queue = help;
help = temp;
return value;
}
/**
*
*/
public T peek() {
while (queue.size() > 1) {
help.offer(queue.poll());
}
T value = queue.poll();
help.offer(value);
Queue<T> temp = queue;
queue = help;
help = temp;
return value;
}
public boolean isEmpty() {
return queue.size() == 0;
}
}
public static void main(String[] args){
int testTime = 10000;
int range = 500;
MyStack<Integer> myStack = new MyStack<>();
Stack<Integer> stack = new Stack<>();
System.out.println("Start!");
for (int i = 0; i < testTime; i++) {
if (myStack.isEmpty()) {
if (!stack.isEmpty()) {
System.out.println("stack isEmpty fuck");
return;
}
int value = (int) (range * Math.random());
myStack.push(value);
stack.push(value);
}else{
double random = Math.random();
if (random < 0.25) {
int value = (int) (range * Math.random());
myStack.push(value);
stack.push(value);
} else if (random < 0.5) {
if (!myStack.pop().equals(stack.pop())) {
System.out.println("pop fuck!");
return;
}
}else if (random < 0.75){
if (!myStack.peek().equals(stack.peek())) {
System.out.println("peek fuck!");
return;
}
}else{
if (myStack.isEmpty() != stack.isEmpty()) {
System.out.println("isEmpty fuck!");
return;
}
}
}
}
System.out.println("End!");
}
}

@ -0,0 +1,99 @@
package leo.class02;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* @author Leo
* @ClassName TwoStacksImplementQueue
* @DATE 2020/11/21 12:09
* @Description 使
*/
public class TwoStacksImplementQueue {
public static class MyQueue{
private Stack<Integer> push;
private Stack<Integer> pop;
public MyQueue() {
this.push = new Stack<>();
this.pop = new Stack<>();
}
private void pushToPop() {
if (this.pop.isEmpty()) {
while (!this.push.isEmpty()) {
this.pop.push(this.push.pop());
}
}
}
public void push(int value) {
push.push(value);
pushToPop();
}
public int poll() throws Exception {
if (this.push.isEmpty() && this.pop.isEmpty()) {
throw new Exception("queue is empty");
}
pushToPop();
return pop.pop();
}
public int peek() throws Exception {
if (this.push.isEmpty() && this.pop.isEmpty()) {
throw new Exception("queue is empty");
}
pushToPop();
return this.pop.peek();
}
}
public static void main(String[] args) {
int testTime = 1000000;
int range = 30;
Queue<Integer> queue = new LinkedList<>();
MyQueue myQueue = new MyQueue();
for (int i = 0; i < testTime; i++) {
if (Math.random() < 0.5) {
int value = randomInt(range);
queue.offer(value);
myQueue.push(value);
}else{
try {
int myQueueValue = myQueue.poll();
int queueValue = queue.poll();
if (myQueueValue != queueValue) {
System.out.println("queueValue : "+queueValue);
System.out.println("myQueueValue : "+myQueueValue);
return;
}
} catch (Exception e) {
continue;
}
}
}
System.out.println("OK!🙆‍️");
}
public static int randomInt(int range) {
return (int) (range * Math.random() - range * Math.random()) + 2;
}
}
Loading…
Cancel
Save