-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathstacksAndQueues.js
126 lines (118 loc) · 2.79 KB
/
stacksAndQueues.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
const assert = require("assert");
class Stack {
constructor() {
this.top;
}
pop() {
if (!this.top) return;
const poppedVal = this.top.val;
this.top = this.top.next;
return poppedVal;
}
push(val) {
const newNode = new StackNode(val);
if (this.top) {
newNode.next = this.top;
this.top = newNode;
} else {
this.top = newNode;
}
}
peek() {
if (!this.top) return;
return this.top.val;
}
isEmpty() {
return !this.top;
}
}
class StackNode {
constructor(val, next) {
this.val = val === undefined ? null : val;
this.next = next === undefined ? null : next;
}
}
const arrayToStack = (arr) => {
const stack = new Stack();
for (const el of arr) {
stack.push(el);
}
return stack;
};
class Queue {
constructor() {
this.start;
this.end;
}
add(val) {
const newNode = new QueueNode(val);
if (this.end) {
this.end.next = newNode;
}
this.end = newNode;
if (!this.start) {
this.start = this.end;
}
}
remove() {
if (!this.start) return;
const removedVal = this.start.val;
this.start = this.start.next;
if (!this.start) {
this.end = null;
}
return removedVal;
}
peek() {
return this.start.val;
}
isEmpty() {
return !this.start;
}
}
class QueueNode {
constructor(val, next) {
this.val = val === undefined ? null : val;
this.next = next === undefined ? null : next;
}
}
const arrayToQueue = (arr) => {
const queue = new Queue();
for (const el of arr) {
queue.add(el);
}
return queue;
};
describe(`${module.filename} - Stack`, () => {
it("should create a stack and perform stack methods correctly", () => {
const arr = [1, 2, 3, 4, 5];
const stack = arrayToStack(arr);
assert.strictEqual(stack.peek(), 5);
assert.strictEqual(stack.pop(), 5);
assert.strictEqual(stack.peek(), 4);
assert.ok(!stack.isEmpty());
assert.strictEqual(stack.pop(), 4);
assert.strictEqual(stack.pop(), 3);
assert.strictEqual(stack.pop(), 2);
assert.strictEqual(stack.pop(), 1);
assert.ok(stack.isEmpty());
stack.push(10);
assert.strictEqual(stack.peek(), 10);
});
it("should create a queue and perform queue methods correctly", () => {
const arr = [1, 2, 3, 4, 5];
const queue = arrayToQueue(arr);
assert.strictEqual(queue.peek(), 1);
assert.strictEqual(queue.remove(), 1);
assert.strictEqual(queue.peek(), 2);
assert.ok(!queue.isEmpty());
assert.strictEqual(queue.remove(), 2);
assert.strictEqual(queue.remove(), 3);
assert.strictEqual(queue.remove(), 4);
assert.strictEqual(queue.remove(), 5);
assert.ok(queue.isEmpty());
queue.add(10);
assert.strictEqual(queue.peek(), 10);
});
});
module.exports = { arrayToQueue, arrayToStack, Queue, Stack };