forked from careercup/CtCI-6th-Edition-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.js
More file actions
42 lines (34 loc) · 829 Bytes
/
Copy pathStack.js
File metadata and controls
42 lines (34 loc) · 829 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
37
38
39
40
41
42
// implement a stack using linkedLists
var LinkedList = require('./LinkedList');
var Stack = function() {
this.top = null;
};
Stack.prototype.push = function(value) {
var node = new LinkedList(value);
node.next = this.top;
this.top = node;
};
Stack.prototype.pop = function() {
var popped = this.top;
if (this.top !== null) {
this.top = this.top.next;
}
return popped.value;
};
Stack.prototype.peek = function() {
return this.top !== null ? this.top.value : null;
};
Stack.prototype.isEmpty = function() {
return this.top === null;
};
module.exports = Stack;
/* TEST */
// var s = new Stack();
// s.push('a');
// s.push('b');
// s.push('c');
// console.log(s.pop(), 'c');
// console.log(s.peek(), 'b');
// console.log(s.pop(), 'b');
// console.log(s.pop(), 'a');
// console.log(s.isEmpty(), true);