-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRingArray.java
More file actions
298 lines (249 loc) · 8.13 KB
/
RingArray.java
File metadata and controls
298 lines (249 loc) · 8.13 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package class01;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Random;
import java.util.Stack;
/**
* @author pacai
* @version 1.0
*/
//@SuppressWarnings("all")
public class RingArray {
public static class MyQueue<T> {
private int size = 0;
private Object[] arr;
private int limit;
private int pushi;
private int popi;
public MyQueue(int limit) {
this.limit = limit;
pushi = popi = 0;
arr = new Object[limit];
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void push(T data) {
if (size() == limit) {
throw new RuntimeException("栈满");
}
size++;
arr[pushi] = data;
pushi = nextIndex(pushi);
}
public T pop() {
if (size() == 0) {
throw new RuntimeException("栈空");
}
size--;
T ans = (T) arr[popi];
popi = nextIndex(popi);
return ans;
}
public int nextIndex(int index) {
return index + 1 > limit ? 0 : index + 1;//等效于取模
}
public T peek() {
if (size() == 0) {
throw new RuntimeException("栈空");
}
return (T) arr[popi];
}
}
public static class MyStack<T> {
int size = 0;
Object[] arr;
int limit;
public MyStack(int limit) {
this.limit = limit;
}
public boolean push(T data) {
if (size() == 0) {
arr = new Object[limit];
}
if (isFull()) {
return false;
}
arr[size++] = data;
return true;
}
public T pop() {
if (isEmpty()) {
return null;
}
return (T) arr[--size];
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public boolean isFull() {
return size == limit;
}
}
//最小栈
static class MyStack1 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack1() {
stackData = new Stack<>();
stackMin = new Stack<>();
}
public void push(int data) {
if (stackMin.isEmpty()) {
stackMin.push(data);
} else if (data < getMin()) {
stackMin.push(data);
} else {
stackMin.push(stackMin.peek());
}
this.stackData.push(data);
}
public int pop() {
if (stackData.isEmpty()) {
throw new RuntimeException("the stack is empty");
}
stackMin.pop();
return stackData.pop();
}
public int getMin() {
if (stackMin.isEmpty()) {
throw new RuntimeException("the stack is empty");
}
return stackMin.peek();
}
//最小栈
public static class MyStack2{
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack2() {
stackData = new Stack<>();
stackMin = new Stack<>();
}
public void push(int data){
if(stackMin.isEmpty()){
stackMin.push(data);
}
stackData.push(data);
if(stackMin.peek() >= data){
stackMin.push(data);
}
}
public int pop(){
if(stackData.isEmpty()){
throw new RuntimeException("the stack is empty");
}
if(stackData.pop().equals(stackMin.peek())){
stackMin.pop();
}
return stackData.pop();
}
public int getMin(){
if(stackMin.isEmpty()){
throw new RuntimeException("the stack is empty");
}
return stackMin.peek();
}
}
//队列模拟栈
public static class TwoQueueStack{
private ArrayList<Integer> queuePush;
private ArrayList<Integer> queuePop;
public TwoQueueStack(ArrayList<Integer> queuePush, ArrayList<Integer> queuePop) {
this.queuePush = queuePush;
this.queuePop = queuePop;
}
public void push(int data){
if(queuePush.isEmpty() && queuePop.isEmpty()){
queuePush.add(data);
}else if(!queuePush.isEmpty()){
queuePush.add(data);
}else{
queuePop.add(data);
}
}
public void getStack(){
if(queuePop.isEmpty() && queuePush.isEmpty()){
throw new RuntimeException("the stack is empty");
}
if(queuePop.isEmpty()) {
for (int i = 0; i < queuePush.size() - 1; i++) {
queuePop.add(queuePush.remove(0));
}
}else if(queuePush.isEmpty()){
for (int i = 0; i < queuePop.size() - 1; i++) {
queuePush.add(queuePop.remove(0));
}
}
}
public int pop(){
getStack();
return queuePush.size() == 1 ? queuePush.remove(0) : queuePop.remove(0);
}
public int peek(){
getStack();
return queuePush.size() == 1 ? queuePush.get(0) : queuePop.get(0);
}
}
//栈模拟队列
public static class TwoStackQueue {
private Stack<Integer> stackPush;
private Stack<Integer> stackPop;
public TwoStackQueue() {
stackPush = new Stack<>();
stackPop = new Stack<>();
}
public void push(int data) {
stackPush.push(data);
pushTopPop();
}
public void pushTopPop() {
if (stackPop.isEmpty()) {
while (!stackPush.isEmpty()) {
stackPop.push(stackPush.pop());
}
}
}
public int pop() {
if (stackPop.isEmpty() && stackPush.isEmpty()) {
throw new RuntimeException("the stack is empty");
}
pushTopPop();
return stackPop.pop();
}
public int geek() {
if (stackPop.isEmpty() && stackPush.isEmpty()) {
throw new RuntimeException("the stack is empty");
}
pushTopPop();
return stackPop.peek();
}
}
}
public static void main(String[] args) {
int limit = 20;
int maxValue = 10000;
int times = 500000;
Random random = new Random();
Stack<Integer> stack = new Stack<>();
MyStack<Integer> myStack = new MyStack<>(limit);
for (int i = 0; i < times; i++) {
for (int j = 0; j < random.nextInt() * limit; j++) {
stack.push(random.nextInt() * maxValue);
myStack.push(stack.pop());
}
for (int j = 0; j < stack.size(); j++) {
if (!myStack.pop().equals(stack.pop())) {
System.out.println("Oops");
return;
}
}
}
System.out.println("test success");
}
}