forked from Vatsalparsaniya/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
33 lines (28 loc) · 627 Bytes
/
Copy pathstack.js
File metadata and controls
33 lines (28 loc) · 627 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
/**
* Implementation of stack with an array
*/
class Stack {
constructor() {
this.stack = [];
}
// Insert an element on top of the stack
push(element) {
this.stack.push(element);
}
//Removes the element at the top of the stack and returns that same element
pop() {
if (this.isEmpty())
return 'Stack is empty!';
return this.stack.pop();
}
// Returns the element that is on top of the stack
peek() {
if (this.isEmpty())
return 'Stack is empty';
return this.stack[this.stack.length - 1];
}
// helper method
isEmpty() {
return !this.stack.length;
}
}