Created
January 31, 2021 14:09
-
-
Save naveenrawat51/631c6245ccb28518eade47fcff3bc2aa to your computer and use it in GitHub Desktop.
Stack implementation Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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