Skip to content

Instantly share code, notes, and snippets.

@naveenrawat51
Created January 31, 2021 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveenrawat51/631c6245ccb28518eade47fcff3bc2aa to your computer and use it in GitHub Desktop.
Save naveenrawat51/631c6245ccb28518eade47fcff3bc2aa to your computer and use it in GitHub Desktop.
Stack implementation Javascript
class Mystack {
constructor() {
this.count = 0;
this.data = [];
}
push(item) {
this.data[this.count] = item;
this.count += 1;
return this.data;
}
pop() {
if (this.count) {
const deletedItem = this.data[this.count - 1];
this.data.splice(this.count - 1, 1);
this.count -= 1;
return deletedItem;
}
return undefined;
}
peek() {
return this.data[this.count - 1];
}
printStack() {
return this.data;
}
isEmpty() {
return this.count === 0;
}
}
// const stack1 = new Mystack();
// console.log(stack1.push(200));
// console.log(stack1.push(100));
// console.log(stack1.push(500));
// console.log(stack1.pop());
// console.log(stack1.printStack());
// console.log(stack1.peek());
// console.log(stack1.pop());
// console.log(stack1.pop());
// console.log(stack1.peek());
// console.log(stack1.isEmpty());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment