Skip to content

Instantly share code, notes, and snippets.

@EliaECoyote
Last active March 2, 2020 18:26
Show Gist options
  • Save EliaECoyote/6e2886be91c31e4cf4d2f3f0fd426fd9 to your computer and use it in GitHub Desktop.
Save EliaECoyote/6e2886be91c31e4cf4d2f3f0fd426fd9 to your computer and use it in GitHub Desktop.
function makeStack() {
let head = null
function push(item) {
if (head == null) head = { value: item }
else head = { value: item, next: head }
}
function isEmpty() {
return head == null
}
function peek() {
return head ? head.value : null
}
function pop() {
const value = head ? head.value : null
if (value) head = head.next
return value
}
return { isEmpty, push, peek, pop }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment