Skip to content

Instantly share code, notes, and snippets.

@devpeds
Last active May 26, 2017 14:45
Show Gist options
  • Save devpeds/1b71fe7ae1d4b65cd40e0c27518a5d7a to your computer and use it in GitHub Desktop.
Save devpeds/1b71fe7ae1d4b65cd40e0c27518a5d7a to your computer and use it in GitHub Desktop.
JavaScript Data Structure - Queue
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