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…
Reference in new issue