I am learning data structures and below is my implementation of a stack in JavaScript. I didn't want to use built in functions for an array because in JavaScript that would be too easy, so I made my own.
class Stack {
constructor(data=null) {
this.items = [data]
this.length = 0
}
push(data) {
this.items[this.length] = data
this.length++
}
pop() {
delete this.items[this.length-1]
this.length--
}
printStack() {
for(let i = 0; i<this.length; i++)
console.log(this.items[i])
}
isEmpty() {
return (this.length) > 0 ? false : true
}
}
let myStack = new Stack();
myStack.push(123)
myStack.push(456)
myStack.pop()
myStack.printStack()
console.log(myStack.isEmpty())