-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackQueue.js
351 lines (300 loc) · 6.69 KB
/
StackQueue.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
const print = console.log;
// * STACKS AND QUEUES CHAPTER
/**
* ? Stack Definition
*/
function StackNode(val, next = null) {
this.val = val;
this.next = next;
}
const QueueNode = StackNode;
const Stack = function() {
this.top = null;
this.size = 0;
};
Stack.prototype.push = function(val) {
if (!this.top) {
this.top = new StackNode(val);
} else {
this.top = new StackNode(val, this.top);
}
this.size += 1;
};
Stack.prototype.pop = function() {
if (!this.top) return null;
const temp = this.top;
this.top = this.top.next;
this.size -= 1;
return temp.val;
};
Stack.prototype.peek = function() {
if (this.top) return this.top.val;
return null;
};
Stack.prototype.empty = function() {
return this.size === 0;
};
/**
* ! Stack Definition
*/
/**
* ? Queue Definition
*/
const Queue = function() {
this.top = null;
this.tail = null;
this.size = 0;
};
Queue.prototype.enqueue = function(val) {
if (!this.top) {
this.top = new QueueNode(val);
this.tail = this.top;
} else {
this.tail.next = new QueueNode(val);
this.tail = this.tail.next;
}
this.size += 1;
};
Queue.prototype.dequeue = function() {
if (!this.top) return;
const temp = this.top;
this.top = this.top.next;
this.size -= 1;
return temp.val;
};
Queue.prototype.peek = function() {
if (this.top) return this.top.val;
return null;
};
Queue.prototype.empty = function() {
return this.size === 0;
};
/**
* ! Queue Definition
*/
/**
* ? Ring Buffer Definition
*/
const Ring = function() {
this.buffer = new Array(10);
this.addIndex = 0;
this.removeIndex = 0;
};
Ring.prototype.add = function(val) {
this.buffer[this.addIndex] = val;
if (this.addIndex === this.removeIndex) {
this.removeIndex += 1;
this.removeIndex %= 10;
}
this.addIndex += 1;
this.addIndex %= 10;
};
Ring.prototype.remove = function() {
if (this.buffer[this.removeIndex] === null) return null;
const temp = this.buffer[this.removeIndex];
this.buffer[this.removeIndex] = null;
this.removeIndex += 1;
this.removeIndex %= 10;
return temp;
};
/**
* ! Ring Buffer Definition
*/
/**
* ? Deque Definition
*/
const Deque = function() {
this.head = null;
this.tail = null;
this.size = 0;
};
const DequeNode = function(val, next = null, prev = null) {
this.val = val;
this.next = next;
this.prev = prev;
};
Deque.prototype.insert = function(val) {
if (this.head === null) {
this.head = new DequeNode(val);
this.tail = this.head;
} else this.head = new DequeNode(val, this.head);
this.size += 1;
};
Deque.prototype.push = function(val) {
if (this.head === null) {
this.head = new DequeNode(val);
this.tail = this.head;
} else {
this.tail.next = new DequeNode(val, null, this.tail);
this.tail = this.tail.next;
}
this.size += 1;
};
Deque.prototype.pop = function() {
if (this.tail === null) return null;
const temp = this.tail.val;
if (this.head === this.tail) {
this.head = null;
this.tail = null;
} else {
this.tail = this.tail.prev;
this.tail.next = null;
}
this.size -= 1;
return temp;
};
Deque.prototype.shift = function() {
if (this.head === null) return null;
const temp = this.head.val;
if (this.head === this.tail) {
this.head = null;
this.tail = null;
} else {
this.head = this.head.next;
this.head.prev = null;
}
this.size -= 1;
return temp;
};
/**
* ! Deque Definition
*/
/**
* * Question 3.2 Stack Min
*/
(function() {
const MinStack = function() {
this.top = null;
this.size = 0;
};
function minNode(val, min, next = null) {
this.val = val;
this.min = min;
this.next = next;
}
MinStack.prototype.push = function(val) {
if (!this.top) {
this.top = new minNode(val, val);
} else {
let min = this.min();
min = Math.min(val, min);
this.top = new minNode(val, min, this.top);
}
this.size += 1;
};
MinStack.prototype.pop = function() {
if (!this.top) return null;
const temp = this.top;
this.top = this.top.next;
this.size -= 1;
return temp.val;
};
MinStack.prototype.min = function() {
return this.top.min;
};
});
/**
* ! Question 3.2
*/
/**
* * Question 3.4 Queue via Stacks
*/
(function() {
const QueueWithStack = function() {
this.left = new Stack();
this.right = new Stack();
};
QueueWithStack.prototype.add = function(val) {
this.left.push(val);
};
QueueWithStack.prototype.remove = function() {
if (!this.right.empty()) return this.right.pop();
if (this.left.empty()) return null;
while (!this.left.empty()) {
let temp = this.left.pop();
this.right.push(temp);
}
return this.right.pop();
};
});
/**
* ! Question 3.4
*/
/**
* * Question 3.5 Sort non-empty Stack
*/
(function() {
// Using a temp stack, we will sort the elements
// one by one
Stack.prototype.sort = function() {
if (!this.top) return; // empty check
const tempStack = new Stack();
let top = this.pop();
// fill the temp stack
while (!this.empty()) {
tempStack.push(this.pop());
}
// Stack with single element is sorted
this.push(top);
// Push top of tempStack to the correct layer
// Until it is empty
while (!tempStack.empty()) {
let top = tempStack.pop();
// transfer greater than top values to the temp
let count = 0;
while (this.peek() > top) {
tempStack.push(this.pop());
count += 1;
}
// Push the top we are locating
this.push(top);
// Push the above layers back
for (let i = 0; i < count; i += 1) {
this.push(tempStack.pop());
}
}
};
});
/**
* ! Question 3.5
*/
/**
* * Question 3.6 Animal Shelter
*/
(function() {
const AnimalShelter = function() {
this.shelter = {
dogs: new Queue(),
cats: new Queue()
};
};
AnimalShelter.prototype.enqueue = function(animal) {
this.shelter[animal].enqueue(Date.now());
};
AnimalShelter.prototype.dequeue = function(animal = null) {
if (animal) {
return this.shelter[animal].dequeue();
}
if (this.shelter.dogs.empty()) return this.shelter.cats.dequeue();
if (this.shelter.cats.empty()) return this.shelter.dogs.dequeue();
let oldestDog = this.shelter.dogs.peek();
let oldestCat = this.shelter.cats.peek();
if (oldestDog <= oldestCat) {
return this.shelter.dogs.dequeue();
} else return this.shelter.cats.dequeue();
};
// ? Tests
const shelter = new AnimalShelter();
shelter.enqueue('dogs');
shelter.enqueue('dogs');
shelter.enqueue('cats');
shelter.enqueue('cats');
shelter.enqueue('dogs');
shelter.dequeue();
shelter.dequeue();
shelter.dequeue();
shelter.dequeue('cats');
})();
/**
* ! Question 3.6
*/