forked from firstcoder55/code-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
36 lines (28 loc) · 650 Bytes
/
Copy pathstack.js
File metadata and controls
36 lines (28 loc) · 650 Bytes
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
var Stack = module.exports = function () {
this.head = null;
this.length = 0;
};
Stack.prototype.push = function (value) {
var node = {
value: value,
next: null
};
if (!this.head) {
this.head = node;
} else {
node.next = this.head;
this.head = node;
}
return this.length += 1;
};
Stack.prototype.pop = function () {
// If there is no head node, return `undefined`
if (!this.head) { return; }
var node = this.head;
// Update the head reference and remove the next node reference from the
// previous head.
this.head = node.next;
node.next = null;
this.length -= 1;
return node.value;
};