Last active
May 26, 2017 14:45
-
-
Save devpeds/1b71fe7ae1d4b65cd40e0c27518a5d7a to your computer and use it in GitHub Desktop.
JavaScript Data Structure - Queue
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
var Queue = function() { | |
this.data = []; | |
}; | |
Queue.prototype.size = function() { | |
return this.data.length; | |
} | |
Queue.prototype.empty = function() { | |
return this.size() === 0; | |
} | |
Queue.prototype.enqueue = function(val) { | |
this.data.unshift(val); | |
}; | |
Queue.prototype.dequeue = function() { | |
return !this.empty() ? this.data.pop() : -1; | |
}; | |
Queue.prototype.front = function() { | |
return !this.empty() ? this.data[0] : -1; | |
}; | |
Queue.prototype.back = function() { | |
return !this.empty() ? this.data[this.size()-1] : -1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment