-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.js
49 lines (41 loc) · 954 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
45
46
47
48
49
function Stack() {
this.stackSize = 0;
this.stackStorage = {};
}
Stack.prototype.push = function(data) {
var size = this.stackSize++;
this.stackStorage[size] = data;
};
Stack.prototype.pop = function() {
var size = this.stackSize;
var deletedData;
if (size >=1) {
deletedData = this.stackStorage[size-1];
delete this.stackStorage[size-1];
this.stackSize--;
return deletedData;
}
else {
return null;
}
};
Stack.prototype.printStack = function() {
console.log(this);
};
// Test it
stack = new Stack();
stack.printStack();
var nullPoped = stack.pop();
console.log("Poped value",nullPoped);
console.log("Pushed value",4);
stack.push(4);
stack.printStack();
console.log("Pushed value",5);
stack.push(5);
stack.printStack();
console.log("Pushed value",8);
stack.push(8);
stack.printStack();
var poped = stack.pop();
console.log("Poped value",poped);
stack.printStack();