-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.linked-list.js
50 lines (42 loc) · 1001 Bytes
/
stack.linked-list.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
const { SinglyLinkedList } = require('../linked-lists');
/**
* Type declaration for Stack using Linked List
* @constructor
*/
function StackLinkedList() {
this.items = new SinglyLinkedList();
this.push = function(data) {
this.items.insertNode(data, 0);
return {
message: 'Insertd successfully',
stack: this.items
}
}
this.pop = function() {
this.items.deleteNode(0);
return {
message: 'Removed successfully',
stack: this.items
}
}
this.isEmptyStack = function() {
return this.items.size === 0;
}
this.top = function() {
if(this.isEmptyStack()) {
return {
message: 'Empty Stack',
value: null,
}
}
return {
value: this.items.getNode(0)
}
}
this.size = function() {
return this.items.size;
}
}
module.exports = {
StackLinkedList
};