forked from vJechsmayr/JavaScriptAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.js
More file actions
43 lines (42 loc) · 1.21 KB
/
Copy pathStack.js
File metadata and controls
43 lines (42 loc) · 1.21 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
//Language: Javascript
//Author: Uddesh Jain
//Github: https://github.com/UddeshJain
class Stack {
constructor() {
this._storage = {}
this._length = 0
}
/*
* Adds a new value in the end of the Stack
* @param {*} value - the value to push
*/
push(value) {
if (value) {
this._storage[this._length] = value
this._length++
}
}
/*
* Removes the value at the end of stack and returns it
* @return {*} the last and newest value in the Stack
*/
pop() {
if (this._length) {
const lastValue = this._storage[this._length - 1]
this._storage[this._length - 1] = undefined
this._length--
return lastValue
}
}
/*
* Returns the value at the end of Stack without removing it
* @return {*} the last and newest value in Stack
*/
peek() {
return lastValue = this._storage[this._storage - 1]
}
}
const myStack = new Stack() // Creates new instance
myStack.push('First') // Adds the value to the Stack
myStack.pop() // Removes the last value in Stack
myStack.peek() // Returns the last value in Stack