-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
44 lines (33 loc) · 993 Bytes
/
stack.js
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
//https://medium.freecodecamp.com/data-structures-stacks-on-stacks-c25f2633c529#.16qnw3llm
class Stack {
constructor(){
this._storage = {};
this._position = -1;
}
push(value){
this._storage[++this._position] = value;
}
pop(){
if(this._position > -1){
let val = this._storage[this._position];
delete this._storage[this._position--];
return val;
}
}
top(){
return this._position;
}
}
/*let browserHistory = new Stack();
browserHistory.push("google.com"); //navigating to Medium
browserHistory.push("medium.com"); // navigating to Free Code Camp
browserHistory.push("freecodecamp.com"); // navigating to Netflix
browserHistory.push("netflix.com"); // current site
console.log("top:", browserHistory.top());
console.log("popped:", browserHistory.pop()); //Returns netflix.com
console.log("top:", browserHistory.top());*/
//push (addition) : O(1)
//pop (removal) : O(1)
//top : O(1)
//find in stack : O(1)
module.exports = Stack;