-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoQueueStack.java
More file actions
88 lines (68 loc) · 1.65 KB
/
TwoQueueStack.java
File metadata and controls
88 lines (68 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.*;
public class TwoQueueStack{
Queue<Integer> queue = new LinkedList<Integer>();
Queue<Integer> help_queue = new LinkedList<Integer>();
Stack<Integer> stack = new Stack<Integer>();
public void push(int v){
queue.offer(v);
}
public void stackPush(int v){
stack.push(v);
}
public void stackPop(){
if(!stack.isEmpty()){
stack.pop();
}
}
public int pop(){
if(queue.isEmpty()) return -1;
while(queue.size() > 1){
help_queue.offer(queue.poll());
}
int v = queue.poll();
Queue<Integer> temp = queue;
queue = help_queue;
help_queue = temp;
return v;
}
public int size(){
return queue.size() + help_queue.size();
}
public Stack<Integer> getStack(){
return stack;
}
public Boolean RandomOptions(){
int max = 100000;
for (int j=0;j<max ;j++ ) {
TwoQueueStack stack = new TwoQueueStack();
for (int i=0;i<300 ;i++ ) {
int r = (int)(Math.random()*99999);
if(r%4==0){
//System.out.println("push--- j = "+j +",r="+r);
stack.push(r);
stack.stackPush(r);
}else{
//System.out.println("pop--- j = "+j);
stack.pop();
stack.stackPop();
}
}
Stack<Integer> _stack=stack.getStack();
if(stack.size() != _stack.size()){
System.out.println("_stack.size = "+_stack.size() +"stack.size="+stack.size());
return false;
}
while(!_stack.isEmpty()){
int v = _stack.pop();
int v1 = stack.pop();
//System.out.println("v = "+v +"v1"+v1);
if(v!=v1) return false;
}
}
return true;
}
public static void main(String[] args){
TwoQueueStack stack = new TwoQueueStack();
System.out.print("result = "+stack.RandomOptions());
}
}