-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathstack.js
67 lines (60 loc) · 1.13 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const DoublyLinkedList = require('./doubly_linked_list');
/**
* Class for Stacks
*/
class Stack {
constructor(data) {
/** @private */
this._list = new DoublyLinkedList();
if (data !== undefined) {
this.push(data);
}
}
/**
* Get length of Stack
* @return {Number} Size of Stack
* @public
*/
get size() {
return this._list.length;
}
/**
* Check if Stack is empty or not
* @return {Boolean} `true` if empty else `false`
* @public
*/
isEmpty() {
return this._list.isEmpty();
}
/**
* Pushes node in Stack
* @param {*} data Data or value to be pushed
* @return {None}
* @public
*/
push(data) {
if (Array.isArray(data)) {
data.forEach(val => this._list.push(val));
} else {
this._list.push(data);
}
}
/**
* Pop node from Stack
* @return {*} Value of node which was popped
* @public
*/
pop() {
return this._list.pop().value;
}
/**
* Get top value on stack
* @return {*} Value of node at the top
* @public
*/
top() {
const top = this._list.head;
return top.value;
}
}
module.exports = Stack;