-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackInJs.js
68 lines (63 loc) · 1.4 KB
/
StackInJs.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"use strict"
// class Stack{
// constructor() {
// this.items = [];
// }
// push(element){
// this.items.push(element);
// }
// pop(){
// if(this.items.length == 0)
// return "Underflow";
// return this.items.pop();
// }
// peek(){
// return this.items[this.items.length - 1];
// }
// isEmpty(){
// return this.items.length == 0;
// }
// // printStack function
// printStack(){
// var str = "";
// for (var i = 0; i < this.items.length; i++)
// str += this.items[i] + " ";
// return str;
// }
// }
// Creates a stack
var Stack = function() {
this.count = 0;
this.storage = {};
}
// Adds a value onto the end of the stack
Stack.prototype.push = function(value) {
this.storage[this.count] = value;
this.count++;
}
// Removes and returns the value at the end of the stack
Stack.prototype.pop = function() {
// Check to see if the stack is empty
if (this.count === 0) {
return undefined;
}
this.count--;
var result = this.storage[this.count];
delete this.storage[this.count];
return result;
}
// Returns the length of the stack
Stack.prototype.size = function() {
return this.count;
}
Stack.prototype.peek = function() {
return this.storage[this.count-1];
}
var stack = new Stack();
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
console.log(stack)
stack.pop();
console.log(stack.peek())