Repository navigation
Expand file tree
/
Copy pathLinkedListToQueueAndStack.java
More file actions
235 lines (191 loc) · 5.98 KB
/
Copy pathLinkedListToQueueAndStack.java
File metadata and controls
235 lines (191 loc) · 5.98 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
package Novice_class;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
// 单链表实现队列和栈
public class LinkedListToQueueAndStack {
public static class Node<V> {
public V value;
Node<V> next;
public Node(V value) {
this.value = value;
}
}
public static class MyQueue<V> {
Node<V> head;
Node<V> tail;
int usedSized;
public MyQueue() {
head = null;
tail = null;
usedSized = 0;
}
public boolean isEmpty() {
return this.usedSized == 0;
}
public int size() {
return usedSized;
}
public void offer(V value) {
Node<V> cur = new Node<>(value);
if (head == null) {
head = cur;
tail = cur;
} else {
tail.next = cur;
tail = cur;
}
tail.next = null;
usedSized++;
}
public V poll() {
if (this.isEmpty()) {
return null;
}
V cur = head.value;
head = head.next;
if (head == null) {
tail = null;
}
usedSized--;
return cur;
}
public V peek() {
return this.isEmpty() ? null : this.head.value;
}
}
// 单链表实现栈
public static class MyStack<V> {
Node<V> head;
int usedSize;
public MyStack() {
this.head = null;
this.usedSize = 0;
}
public Boolean isEmpty() {
return this.usedSize == 0;
}
public int size() {
return this.usedSize;
}
public void push(V value) {
Node<V> cur = new Node<>(value);
cur.next = head;
head = cur;
usedSize++;
}
public V pop() {
if (this.isEmpty()) {
return null;
}
V value = this.head.value;
head = head.next;
//usedSize--;
return value;
}
public V peek() {
return this.isEmpty() ? null : this.head.value;
}
}
// for test
public static void testStack() {
MyStack<Integer> myStack = new MyStack<>();
Stack<Integer> test = new Stack<>();
int testTime = 5000000;
int maxValue = 200000000;
System.out.println("测试开始!");
for (int i = 0; i < testTime; i++) {
if (myStack.isEmpty() != test.isEmpty()) {
System.out.println("isempty Oops!");
}
if (myStack.size() != test.size()) {
System.out.println("size Oops!");
}
double decide = Math.random(); // 主要作用是判断概率
if (decide < 0.33) {
int num = (int) (Math.random() * maxValue);
myStack.push(num);
test.push(num);
} else if (decide < 0.66) {
if (!myStack.isEmpty()) {
int num1 = myStack.pop();
int num2 = test.pop();
if (num1 != num2) {
System.out.println("pop Oops!");
}
}
} else {
if (!myStack.isEmpty()) {
int num1 = myStack.peek();
int num2 = test.peek();
if (num1 != num2) {
System.out.println("isempty Oops!");
}
}
}
}
if (myStack.size() != test.size()) {
System.out.println("size Oops!");
}
// 全部弹出
while (!myStack.isEmpty()) {
int num1 = myStack.pop();
int num2 = test.pop();
if (num1 != num2) {
System.out.println("isempty Oops!");
}
}
System.out.println("测试结束!");
}
public static void main(String[] args) {
//testQueue();
testStack();
}
// for test
public static void testStack2() {
MyStack<Integer> stack1 = new MyStack<>();
Stack<Integer> stack2 = new Stack<>();
int testTime = 1000000;
int MaxValue = 2200000;
System.out.println("测试开始");
for (int i = 0; i < testTime; i++) {
if (stack1.isEmpty() != stack2.isEmpty()) {
System.out.println("oops");
}
if (stack1.size() != stack2.size()) {
System.out.println("oops");
}
double decide = Math.random();
if (decide < 0.33) {
int value = (int) (Math.random() * MaxValue);
stack1.push(value);
stack2.push(value);
} else if (decide < 0.66) {
if (!stack1.isEmpty()) {
int num1 = stack1.pop();
int num2 = stack2.pop();
if (num1 != num2) {
System.out.println("oops");
}
}
} else {
if (!stack1.isEmpty()) {
int num1 = stack1.peek();
int num2 = stack2.peek();
if (num1 != num2) {
System.out.println("oops");
}
}
}
}
if (stack1.size() != stack2.size()) {
System.out.println("oops");
}
while (!stack1.isEmpty()) {
if (stack1.pop() != stack2.pop()) {
System.out.println("oops");
}
}
System.out.println("测试结束");
}
}