Skip to content

Commit dd7c6b4

Browse files
committed
Implement stack data structure using array
1 parent 823e1f7 commit dd7c6b4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Diff for: stack-array.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Stack {
2+
constructor() {
3+
this.items = [];
4+
}
5+
6+
push(element) {
7+
this.items.push(element);
8+
}
9+
10+
pop() {
11+
return this.items.pop();
12+
}
13+
14+
peek() {
15+
return this.items[this.items.length - 1];
16+
}
17+
18+
isEmpty() {
19+
return this.items.length === 0;
20+
}
21+
22+
size() {
23+
return this.items.length;
24+
}
25+
26+
print() {
27+
console.log(this.items.toString());
28+
}
29+
}
30+
31+
const stack = new Stack();
32+
console.log(stack.isEmpty());
33+
stack.push(20);
34+
stack.push(10);
35+
stack.push(30);
36+
console.log(stack.size());
37+
stack.print();
38+
console.log(stack.pop());
39+
console.log(stack.peek());
40+
stack.print();

0 commit comments

Comments
 (0)