Skip to content

Commit 3149f8f

Browse files
committed
Implement stack data structure using object
1 parent dd7c6b4 commit 3149f8f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

stack-object.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Stack {
2+
constructor() {
3+
this.items = {};
4+
this.head = 0;
5+
}
6+
7+
push(element) {
8+
this.items[this.head] = element;
9+
this.head++;
10+
}
11+
12+
pop() {
13+
const item = this.items[this.head - 1];
14+
delete this.items[this.head - 1];
15+
this.head--;
16+
return item;
17+
}
18+
19+
peek() {
20+
return this.items[this.head - 1];
21+
}
22+
23+
size() {
24+
return this.head;
25+
}
26+
27+
isEmpty() {
28+
return this.head === 0;
29+
}
30+
31+
print() {
32+
console.log(this.items);
33+
}
34+
}

0 commit comments

Comments
 (0)