Skip to content

Instantly share code, notes, and snippets.

@Jarvis1010
Last active Nov 29, 2021
Embed
What would you like to do?
const stackUtils = {
push(head, value){
const node = {
value,
next : head
}
return node
},
peak(head){
return head?.value
},
pop(head){
const safeNode = head ?? {}
return safeNode.next;
},
getIterator(head){
return (function*(){
let safeNode = head ?? {}
while(safeNode.value){
yield safeNode.value
safeNode = safeNode.next ?? {}
}
})()
}
}
let stack
stack = stackUtils.push(stack, 1)
stack = stackUtils.push(stack, 2)
stackUtils.peak(stack) // 2
stack = stackUtils.pop(stack)
stackUtils.peak(stack) // 1
stack = stackUtils.push(stack, 3)
stack = stackUtils.push(stack, 4)
stack = stackUtils.push(stack, 2)
stack = stackUtils.push(stack, 2)
console.log([...stackUtils.getIterator(stack)]) // [ 2, 2, 4, 3, 1 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment