Last active
August 26, 2017 05:04
-
-
Save abinavseelan/f528e02ff2006605c346b0af9d643ddd to your computer and use it in GitHub Desktop.
Medium - DS in Javascript - Queue - Length Method
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; | |
} | |
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