-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackQueue.java
More file actions
262 lines (213 loc) · 6.3 KB
/
StackQueue.java
File metadata and controls
262 lines (213 loc) · 6.3 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
public class StackQueue {
public static void main(String[] args) {
Dog d = new Dog("bill");
Dog d2 = new Dog("greg");
Cat c = new Cat("keith");
Cat c2 = new Cat("meg");
ShelterQueue sq = new ShelterQueue();
sq.enqueue(d);
sq.enqueue(d2);
sq.enqueue(c);
sq.enqueue(c2);
System.out.println(sq.dequeueAny().getName());
System.out.println(sq.dequeueCats().getName());
}
// Last in first out (LIFO)
public static class Stack {
Node top;
Stack() {
top = null;
}
void push(int data) {
Node newNode = new Node(data);
newNode.next = top;
top = newNode;
}
Node pop() {
if (top != null) {
Node item = new Node(top.data);
top = top.next;
return item;
}
return null;
}
Integer peek() {
return top.data;
}
void display() {
Node n = top;
do {
System.out.print(n.data + "->");
n = n.next;
} while (n != null);
System.out.print("NULL\n");
}
}
//first in first out (FIFO)
public class Queue {
Node first, last;
void enqueue(Node item) {
if (first == null) {
last = new Node(item.data);
first = last;
} else {
last.next = new Node(item.data);
last = last.next;
}
}
Node dequeue() {
if (first != null) {
Node item = new Node(first.data);
first = first.next;
return item;
}
return null;
}
}
//3.2 Stack with min at O(1)
public static class StackWithMin extends java.util.Stack<Integer> {
private java.util.Stack<Integer> minStack;
public StackWithMin() {
minStack = new java.util.Stack<>();
}
public void push(int value) {
if (value <= min()) {
minStack.push(value);
}
super.push(value);
}
public Integer pop() {
int value = super.pop();
if (value == min()) {
minStack.pop();
}
return value;
}
public int min() {
if (minStack.isEmpty()) {
return Integer.MAX_VALUE;
} else {
return minStack.peek();
}
}
}
//3.4 towers of hanoi
public static void towerMain() {
int n = 5;
Tower[] towers = new Tower[n];
for (int i = 0; i < n; i++) {
towers[i] = new Tower(i);
}
for (int i = n - 1; i >= 0; i--) {
towers[0].add(i);
}
towers[0].moveDiscs(n, towers[2], towers[1]);
}
//3.4 towers of hanoi
public static class Tower {
private java.util.Stack<Integer> discs;
private int index;
public Tower(int i) {
discs = new java.util.Stack<>();
index = i;
}
public int getIndex() {
return index;
}
public void add(int d) {
if (!discs.isEmpty() && discs.peek() <= d) {
System.out.println("Error placing disk " + d);
} else {
discs.push(d);
}
}
public void moveTopTo(Tower t) {
int top = discs.pop();
t.add(top);
System.out.println("Move disk " + top + " from " + getIndex() + " to " + t.getIndex());
}
public void moveDiscs(int n, Tower destination, Tower buffer) {
if (n <= 0) return; //base case
moveDiscs(n - 1, buffer, destination); //move n-1 discs from (this) to buffer (mid)
moveTopTo(destination); //move disc from (this) to dest
buffer.moveDiscs(n - 1, destination, this); //move n-1 discs from buffer to dest
}
}
//3.6
//Ascending sort of a stack using only one stack
//O(n^2) time and O(n) space
public static java.util.Stack<Integer> sortStack(java.util.Stack<Integer> stack) {
java.util.Stack<Integer> sorted = new java.util.Stack<>();
while (!stack.isEmpty()) {
int temp = stack.pop();
while (!sorted.isEmpty() && sorted.peek() > temp) {
stack.push(sorted.pop());
}
sorted.push(temp);
}
return sorted;
}
public static abstract class Pet {
private int order;
String name;
public Pet(String n) {
name = n;
}
public String getName() {
return name;
}
public void setOrder(int ord) {
order = ord;
}
public int getOrder() {
return order;
}
public boolean isOlderThan(Pet p) {
return this.order < p.getOrder();
}
}
public static class Dog extends Pet {
public Dog(String n) {
super(n);
}
}
public static class Cat extends Pet {
public Cat(String n) {
super(n);
}
}
//3.7
//Queue using linked list
//using a dequeued by time
public static class ShelterQueue {
java.util.LinkedList<Dog> dogs = new java.util.LinkedList<>();
java.util.LinkedList<Cat> cats = new java.util.LinkedList<>();
private int order = 0;
public void enqueue(Pet p) {
p.setOrder(order);
order++;
if (p instanceof Dog) dogs.addLast((Dog) p);
else if (p instanceof Cat) cats.addLast((Cat) p);
}
public Pet dequeueAny() {
if (dogs.size() == 0) {
return dequeueCats();
} else if (cats.size() == 0) {
return dequeueDogs();
}
Dog dog = dogs.peek();
Cat cat = cats.peek();
if (dog.isOlderThan(cat)) {
return dequeueDogs();
} else {
return dequeueCats();
}
}
public Pet dequeueDogs() {
return dogs.poll();
}
public Pet dequeueCats() {
return cats.poll();
}
}
}