-
-
Save Jarvis1010/7a1b5df2ff83f0ea9f40322482103fe3 to your computer and use it in GitHub Desktop.
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
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