Last active
December 4, 2018 21:01
-
-
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.
This file contains hidden or 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
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