-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.array.js
More file actions
45 lines (36 loc) · 922 Bytes
/
stack.array.js
File metadata and controls
45 lines (36 loc) · 922 Bytes
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
class Stack {
constructor() {
this.stack = [];
}
push(data) {
this.stack.push(data);
}
pop() {
return this.stack.pop();
}
size() {
return this.stack.length;
}
peek() {
return this.stack[this.stack.length - 1];
}
print() {
console.log(this.stack);
}
}
module.exports.Stack = Stack;
// const stack = new Stack();
// stack.push(1);
// stack.push(2);
// stack.push(3);
// stack.print(); // => [ 1, 2, 3 ]
// console.log('size is 3:', stack.size()); // => 3
// console.log('peek is 3:', stack.peek()); // => 3
// console.log('pop is 3:', stack.pop()); // => 3
// stack.print(); // => [ 1, 2 ]
// console.log('peek is 2:', stack.peek()); // => 2
// console.log('pop is 2:', stack.pop()); // => 2
// console.log('size is 1:', stack.size()); // => 1
// console.log('pop is 1:', stack.pop()); // => 1
// stack.print(); // => '[]'
// console.log('pop is undefined:', stack.pop()); // => undefined