Skip to content

Instantly share code, notes, and snippets.

@abinavseelan
Last active August 26, 2017 05:04
Show Gist options
  • Save abinavseelan/f528e02ff2006605c346b0af9d643ddd to your computer and use it in GitHub Desktop.
Save abinavseelan/f528e02ff2006605c346b0af9d643ddd to your computer and use it in GitHub Desktop.
Medium - DS in Javascript - Queue - Length Method
function Queue() {
this.values = [];
this.count = 0;
}
Queue.prototype.enqueue = (newValue) => {
this.values[this.count] = newValue;
this.count++;
}
Queue.prototype.dequeue = () => {
if (this.count === 0) {
return null;
}
const value = this.values[0];
const arrayAfterDeletion = this.values.slice(1, this.count - 1);
this.values = [...arrayAfterDeletion];
return value;
}
Queue.prototype.length = () => {
return this.count; // Return the count, which holds the length of the Queue.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment