-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.stack_object.js
More file actions
45 lines (38 loc) · 781 Bytes
/
18.stack_object.js
File metadata and controls
45 lines (38 loc) · 781 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.items = {};
this.head = 0
}
push(element) {
this.items[this.head] = element;
this.head++;
}
pop() {
const item = this.items[this.head - 1];
delete this.items[this.head - 1];
this.head--;
return item;
}
peek() {
return this.items[this.head - 1];
}
size() {
return this.head;
}
isEmpty() {
return this.head === 0;
}
print() {
console.log(this.items);
}
}
const stack = new Stack();
console.log(stack.isEmpty());
stack.push(20);
stack.push(10);
stack.push(30);
console.log(stack.size());
stack.print();
console.log(stack.pop());
console.log(stack.peek()); // the last element
stack.print();