-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathreverse_stack.js
More file actions
51 lines (40 loc) · 856 Bytes
/
Copy pathreverse_stack.js
File metadata and controls
51 lines (40 loc) · 856 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
46
47
48
49
50
51
/**
* Part of Cosmos by OpenGenus Foundation
*/
function Stack() {
this.stack = [];
this.pop = function () {
this.stack.pop();
};
this.push = function (el) {
this.stack.push(el);
};
this.peek = function () {
return this.stack[this.stack.length - 1];
};
this.isEmpty = function () {
return this.stack.length == 0;
};
this.reverse = function () {
if (this.isEmpty()) {
console.log("The stack is empty");
return;
}
this.stack.reverse();
};
this.print = function () {
console.log(this.stack);
};
}
const stack = new Stack();
const items = [1, 2, 3, 4];
for (let i of items) {
stack.push(i);
}
console.log("\nCurrent Stack");
console.log("===============");
stack.print();
console.log("\nReversed Stack");
console.log("===============");
stack.reverse();
stack.print();