Last active
August 26, 2017 05:05
-
-
Save abinavseelan/106570ffb111887f50c13d2cef1fa2fb to your computer and use it in GitHub Desktop.
Medium - DS in Javascript - Queue - pop()
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
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