Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added reverse stack implementation in javascript #6814

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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();