Skip to content

Instantly share code, notes, and snippets.

@anthonybrown
Last active December 4, 2018 21:01
Show Gist options
  • Save anthonybrown/23f68acbfb6f4ba1e031e7bed41dbb6f to your computer and use it in GitHub Desktop.
Save anthonybrown/23f68acbfb6f4ba1e031e7bed41dbb6f to your computer and use it in GitHub Desktop.
Using a Queue to contain our data structure, use a queue. Create a function that returns our data in a plain old Object. A queue is a collection of items that obey's the principle of FIFO, first in, first out.
function createQueue() {
// store our queue
const queue = []
return {
// add or enqueue
enqueue(item) {
queue.unshift(item)
},
// remove or dequeue
dequeue() {
if (queue.length === 0) {
return undefined
}
return queue.pop()
},
// look or peek
peek() {
if (queue.length === 0) {
return undefined
}
return queue[queue.length - 1]
},
// use a getter function to get the length of the queue
get length() {
return queue.length
},
// check to see if queue is empty
isEmpty() {
return queue.length === 0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment