Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Created May 29, 2020 14:35
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save bradtraversy/6386eac2ab15842d1e8e0424a727a81d to your computer and use it in GitHub Desktop.
Save bradtraversy/6386eac2ab15842d1e8e0424a727a81d to your computer and use it in GitHub Desktop.
Stack data structure
class Stack {
constructor() {
this.items = []
this.count = 0
}
// Add element to top of stack
push(element) {
this.items[this.count] = element
console.log(`${element} added to ${this.count}`)
this.count += 1
return this.count - 1
}
// Return and remove top element in stack
// Return undefined if stack is empty
pop() {
if(this.count == 0) return undefined
let deleteItem = this.items[this.count - 1]
this.count -= 1
console.log(`${deleteItem} removed`)
return deleteItem
}
// Check top element in stack
peek() {
console.log(`Top element is ${this.items[this.count - 1]}`)
return this.items[this.count - 1]
}
// Check if stack is empty
isEmpty() {
console.log(this.count == 0 ? 'Stack is empty' : 'Stack is NOT empty')
return this.count == 0
}
// Check size of stack
size() {
console.log(`${this.count} elements in stack`)
return this.count
}
// Print elements in stack
print() {
let str = ''
for(let i = 0; i < this.count; i++) {
str += this.items[i] + ' '
}
return str
}
// Clear stack
clear() {
this.items = []
this.count = 0
console.log('Stack cleared..')
return this.items
}
}
const stack = new Stack()
stack.isEmpty()
stack.push(100)
stack.push(200)
stack.peek()
stack.push(300)
console.log(stack.print())
stack.pop()
stack.pop()
stack.clear()
console.log(stack.print())
stack.size()
stack.isEmpty()
@vikingviolinist
Copy link

Thanks for all the awesome content, Brad. Quick question, shouldn't the push method return number of all elements in items array (this.count instead of this.count - 1)?

@hmehta051
Copy link

this.count is correct because initially count is zero and we have to push at zero index if you take this.count-1(0-1) which is wrong we can't push in negative index

@Gnwin
Copy link

Gnwin commented Nov 4, 2022

The pop method is actually not removing items. items still remains intact after the pop method.

This is my implementation of the pop method:

pop() {
if(this.count == 0) return undefined

const arr = [];
this.count -= 1;
const deleteItem = this.items[this.count]
let i = 0;
while(i < this.count) {
  arr[i] = this.items[i]
  i += 1
}
this.items = arr;
console.log(`${deleteItem} removed`)
return deleteItem;

}

This does actually pops items from this.items

@Gnwin
Copy link

Gnwin commented Nov 4, 2022

as low level as possible

@PowerLevel9000
Copy link

hi @Gnwin @bradtraversy
i hope you guys are doing well and @Gnwin i agree with you

Consideration Some Case

  • I agree with you it actually doesn't delete data it violates the stack principle but it is a good one, to be honest
  • like we can use some last removed function and previously removed items on the basis of this data structure
  • Also we have to provide a constraint on this. stack something like the max length for the stack
  • Lastly, a function that denotes your stack is full and removes the last remove item when the further insertion is done
  • So this is a great data structure, to be honest

Happy coding

IThinkYouAreIncredibleCanadasGotTalentGIF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment