-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.js
More file actions
56 lines (49 loc) · 1.24 KB
/
Stack.js
File metadata and controls
56 lines (49 loc) · 1.24 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
/**
* A stack uses LIFO (last-in first-out) ordering. That is, as in a stack of
* dinner plates, the most recent item added to the stack is the first item to
* be removed.
*
* Note that a stack can also be implemented using a linked list, if items were
* added and removed from the same side.
*
* A stack can also be used to implement a recursive algorithm iteratively.
*
* Complexity Analysis:
* Stack
* Search O(n)
* Access O(n)
* Insert O(1)
* Remove O(1)
*/
class StackNode {
constructor(data, next) {
this.data = data;
this.next = next;
}
}
class Stack {
top = null; // StackNode
size = 0;
// return if the stack is empty
isEmpty() {
return this.size === 0;
}
// push the element on the stack
push(elem) {
this.top = new StackNode(elem, this.top);
this.size++;
}
// pop the element off the stack
pop() {
if (this.isEmpty()) throw new Error('EmptyStackException');
const item = this.top.data;
this.top = this.top.next;
this.size--;
return item;
}
peek() {
if (this.isEmpty()) throw new Error('EmptyStackException');
return this.top.data;
}
}
module.exports = { Stack };