Last active
January 30, 2019 08:51
-
-
Save bluurn/469fb193a939671d1e84bbf62a766431 to your computer and use it in GitHub Desktop.
JS: Queue Implementation
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
class Queue { | |
constructor() { | |
this.items = []; | |
} | |
enqueue(obj) { | |
this.items.push(obj); | |
} | |
dequeue() { | |
return this.items.shift(); | |
} | |
isEmpty() { | |
return this.items.length === 0; | |
} | |
} | |
var queue = new Queue(); | |
console.assert(queue._items.length == 0) | |
queue.enqueue('foo'); | |
queue.enqueue('bar'); | |
queue.enqueue('baz'); | |
console.assert(queue._items.every((v,i) => | |
v === ['foo', 'bar', 'baz'][i] | |
)) | |
queue.dequeue(); | |
console.assert(queue._items.every((v,i) => | |
v === ['bar', 'baz'][i] | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment