Skip to content

Instantly share code, notes, and snippets.

@bluurn
Last active January 30, 2019 08:51
Show Gist options
  • Save bluurn/469fb193a939671d1e84bbf62a766431 to your computer and use it in GitHub Desktop.
Save bluurn/469fb193a939671d1e84bbf62a766431 to your computer and use it in GitHub Desktop.
JS: Queue Implementation
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