-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackByArray.java
More file actions
97 lines (75 loc) · 1.55 KB
/
Copy pathStackByArray.java
File metadata and controls
97 lines (75 loc) · 1.55 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
89
90
91
92
93
94
95
96
97
import java.util.*;
public class StackByArray{
Stack<Integer> stack = new Stack<Integer>();
int[] arr = new int[10000];
int size=0;
public void push_stack(int v){
stack.push(v);
}
public void push(int v){
if(size<arr.length){
//System.out.println("push "+v);
arr[size++] = v;
}else{
//System.out.println("push "+v);
System.out.println("栈已满");
}
}
public int pop_stack(){
if(!stack.isEmpty()){
return stack.pop();
}
return -1;
}
public int pop(){
if(size>0){
int v = arr[--size];
//System.out.println("pop "+v);
return v;
}else{
//System.out.println("栈为空");
return -1;
}
}
// public int getPushPIndex(int index){
// return (index+1) >= arr.length ? 0 : index+1;
// }
public void RandomOption(int pushCount, int popCount,BecomeStackByArray stack){
while(true){
int r = (int)(Math.random()*9999);
if(r%2 == 0){
if(pushCount >0 ){
stack.push(r);
stack.push_stack(r);
pushCount--;
}
}else{
if(popCount >0){
stack.pop();
stack.pop();
popCount--;
}
}
if(pushCount == 0 && popCount ==0) {
return;
}
}
}
public static void main(String args[]){
int count = 1000000;
while(count-- > 0){
BecomeStackByArray stack = new BecomeStackByArray();
stack.RandomOption(10000,5000,stack);
int pop_array = stack.pop();
int pop_stack = stack.pop_stack();
if(pop_array != pop_stack){
break;
}
}
if(count > 0){
System.out.println("出错了...");
}else{
System.out.println("成功了...");
}
}
}