Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions code/data_structures/src/stack/reverse_stack/reverse_stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();