Skip to content

Instantly share code, notes, and snippets.

@abinavseelan
Last active August 26, 2017 05:05
Show Gist options
  • Save abinavseelan/106570ffb111887f50c13d2cef1fa2fb to your computer and use it in GitHub Desktop.
Save abinavseelan/106570ffb111887f50c13d2cef1fa2fb to your computer and use it in GitHub Desktop.
Medium - DS in Javascript - Queue - pop()
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; // If the list is empty, you can't pop
}
const value = this.values[0]; // Store the first value in the queue in a temporary variable
const arrayAfterDeletion = this.values.slice(1, this.count - 1); // Store everything after the first value in a temporary array.
this.values = [...arrayAfterDeletion]; // Re-write our list of values
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment