Created
February 6, 2019 00:28
-
-
Save Damimd10/cd9a4a4ad0bbfed17dc6946d6e189a72 to your computer and use it in GitHub Desktop.
JavaScript Queue
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() { | |
| const queue = []; | |
| return { | |
| enqueue(item) { | |
| queue.unshift(item) | |
| }, | |
| dequeue() { | |
| return queue.pop() | |
| }, | |
| get length() { | |
| return queue.length | |
| }, | |
| isEmpty() { | |
| return queue.length == 0 | |
| } | |
| } | |
| } | |
| const myQueue = createQueue(); | |
| myQueue.isEmpty() // true | |
| myQueue.enqueue('Make a sandwich') | |
| myQueue.enqueue('Help others learn queue') | |
| myQueue.enqueue('Be happy') | |
| myQueue.length() // 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment