-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-Stack-Class.js
More file actions
56 lines (50 loc) · 1.64 KB
/
02-Stack-Class.js
File metadata and controls
56 lines (50 loc) · 1.64 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
// Create a Stack using a constructor function
let Stack = function () {
// keeps track of how many items are in the stack
this.count = 0;
// initialize storage to an empty object
this.storage = {};
// Adds a value onto the end of the stack
this.push = function (value) {
this.storage[this.count] = value;
this.count++;
};
// Removes and returns a value at the end of the stack
this.pop = function () {
// Check if the stack is empty
// If count is 0, then there are no items within the stack
if (this.count === 0) {
return undefined;
}
// Decrement count because an item is being removed
this.count--;
// Create a result variable and set it to the storage count
// this.storage is the object for our storage
// this.count is the last item in the stack
let result = this.storage[this.count];
// Remove the item from the storage
delete this.storage[this.count];
// Return the new result, returns the last item
return result;
};
this.size = function () {
// this.count is the number of items within the stack
return this.count;
};
// Returns the value at the end of the stack
// Does NOT remove it like pop()
this.peek = function () {
return this.storage[this.count - 1];
};
};
let myStack = new Stack();
myStack.push(1);
myStack.push(2);
console.log("Peek: ", myStack.peek());
console.log("Pop: ", myStack.pop());
console.log("Peek: ", myStack.peek());
myStack.push("Adam is the best at data structures!");
console.log("Size: ", myStack.size());
console.log("Peek: ", myStack.peek());
console.log("Pop: ", myStack.pop());
console.log("Peek: ", myStack.peek());