-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackAndQueue.java
306 lines (269 loc) · 8.07 KB
/
StackAndQueue.java
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
299
300
301
302
303
304
305
306
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Deque;
public class StackAndQueue {
public static void main(String[] args) {
DequeByThreeStacks d = new StackAndQueue().new DequeByThreeStacks();
d.offerLast(227);
d.offerFirst(32);
d.pollFirst();
d.peekFirst();
}
/**
* Sort With 2 Stacks
* <p>
* Given an array that is initially stored in one stack, sort it with one
* additional stacks (total 2 stacks). After sorting the original stack should
* contain the sorted integers and from top to bottom the integers are sorted in
* ascending order.
*/
public void sort(LinkedList<Integer> s1) {
if (s1.isEmpty()) {
return;
}
LinkedList<Integer> s2 = new LinkedList<>();
int prevMin = Integer.MIN_VALUE;
int count = 0;
while (s1.peekLast() > prevMin) {
int min = Integer.MAX_VALUE;
while (!s1.isEmpty() && s1.peekLast() > prevMin) {
int val = s1.pollLast();
if (val < min) {
min = val;
count = 1;
} else if (val == min) {
count++;
}
s2.offerLast(val);
}
while (count-- >= 0) {
s1.offerLast(min);
}
while (!s2.isEmpty()) {
int val = s2.pollLast();
if (val != min) {
s1.offerLast(val);
}
}
prevMin = min;
}
}
/**
* Queue By Two Stacks Java: Implement a queue by using two stacks
* <p>
* The queue should provide size(), isEmpty(), offer(), poll() and peek()
* operations. When the queue is empty, poll() and peek() should return null.
*/
public class QueueByTwoStacks {
private LinkedList<Integer> in;
private LinkedList<Integer> out;
public QueueByTwoStacks() {
in = new LinkedList<Integer>();
out = new LinkedList<Integer>();
}
public Integer poll() {
if (isEmpty()) {
return null;
}
shuffle();
return out.pollLast();
}
public void offer(int element) {
in.offerLast(element);
}
public Integer peek() {
shuffle();
return out.peekLast();
}
public int size() {
return in.size() + out.size();
}
public boolean isEmpty() {
return in.isEmpty() && out.isEmpty();
}
private void shuffle() {
if (out.isEmpty()) {
while (!in.isEmpty()) {
out.offerLast(in.pollLast());
}
}
}
}
/**
* Stack With min()
* <p>
* Enhance the stack implementation to support min() operation. min() should
* return the current minimum value in the stack. If the stack is empty, min()
* should return -1.
*/
public class StackWithMin {
private LinkedList<Integer> stack;
private LinkedList<Integer> minStack;
public StackWithMin() {
stack = new LinkedList<>();
minStack = new LinkedList<>();
}
public int pop() {
if (stack.isEmpty()) {
return -1;
}
minStack.pollLast();
return stack.pollLast();
}
public void push(int element) {
stack.offerLast(element);
if (minStack.isEmpty()) {
minStack.offerLast(element);
return;
}
int curMin = minStack.peekLast();
minStack.offerLast(Math.min(curMin, element));
}
public int top() {
if (stack.isEmpty()) {
return -1;
}
return stack.peekLast();
}
public int min() {
if (stack.isEmpty()) {
return -1;
}
return minStack.peekLast();
}
}
/**
* Stack by Queue(s)
* <p>
* Implement a stack containing integers using queue(s). The stack should
* provide push(x), pop(), top() and isEmpty() operations.
*/
public class StackByQueue {
private Queue<Integer> queue;
public StackByQueue() {
queue = new ArrayDeque<>();
}
public void push(int x) {
queue.offer(x);
}
public Integer pop() {
if (queue.isEmpty()) {
return null;
}
int size = queue.size();
while (--size > 0) {
queue.offer(queue.poll());
}
return queue.poll();
}
public Integer top() {
if (queue.isEmpty()) {
return null;
}
int val = pop();
push(val);
return val;
}
public boolean isEmpty() {
return queue.isEmpty();
}
}
/**
* Deque By Three Stacks
* <p>
* Java: Implement a deque by using three stacks. The queue should provide
* size(), isEmpty(), offerFirst(), offerLast(), pollFirst(), pollLast(),
* peekFirst() and peekLast() operations. When the queue is empty, pollFirst(),
* pollLast(), peekFirst() and peek() should return null.
*/
public class DequeByThreeStacks {
private Deque<Integer> stack1;
private Deque<Integer> stack2;
private Deque<Integer> stack3;
private void shuffle() {
if (!stack1.isEmpty() && !stack2.isEmpty()) {
return;
}
if (size() == 0) {
return;
}
if (stack1.isEmpty()) {
int size = size() / 2;
while (size-- > 0) {
stack3.offerLast(stack2.pollLast());
}
while (!stack2.isEmpty()) {
stack1.offerLast(stack2.pollLast());
}
while (!stack3.isEmpty()) {
stack2.offerLast(stack3.pollLast());
}
return;
}
if (stack2.isEmpty()) {
int size = size() / 2;
while (size-- > 0) {
stack3.offerLast(stack1.pollLast());
}
while (!stack1.isEmpty()) {
stack2.offerLast(stack1.pollLast());
}
while (!stack3.isEmpty()) {
stack1.offerLast(stack3.pollLast());
}
}
}
public DequeByThreeStacks() {
stack1 = new ArrayDeque<>();
stack2 = new ArrayDeque<>();
stack3 = new ArrayDeque<>();
}
public void offerFirst(int element) {
stack1.offerLast(element);
}
public void offerLast(int element) {
stack2.offerLast(element);
}
public Integer pollFirst() {
if (isEmpty()) {
return null;
}
if (stack1.isEmpty()) {
shuffle();
}
return stack1.pollLast();
}
public Integer pollLast() {
if (isEmpty()) {
return null;
}
if (stack2.isEmpty()) {
shuffle();
}
return stack2.pollLast();
}
public Integer peekFirst() {
Integer result = pollFirst();
if (result == null) {
return null;
}
offerFirst(result);
return result;
}
public Integer peekLast() {
Integer result = pollLast();
if (result == null) {
return null;
}
offerLast(result);
return result;
}
public int size() {
return stack1.size() + stack2.size();
}
public boolean isEmpty() {
return size() == 0;
}
}
}