-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.array.js
48 lines (41 loc) · 937 Bytes
/
stack.array.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
/**
* Type declaration for Stack using Array
* @constructor
*/
function StackArray() {
this.items = [];
this.push = function(data) {
this.items.push(data);
return {
message: 'Insertd successfully',
stack: this.items
}
}
this.pop = function() {
this.items.pop();
return {
message: 'Removed successfully',
stack: this.items
}
}
this.isEmptyStack = function() {
return this.items.length === 0;
}
this.top = function() {
if(this.isEmptyStack()) {
return {
message: 'Empty Stack',
value: null,
}
}
return {
value: this.items[this.items.length - 1]
}
}
this.size = function() {
return this.items.length;
}
}
module.exports = {
StackArray
};