Skip to content

Instantly share code, notes, and snippets.

@GrantSchiller
Last active December 24, 2015 08:29
Show Gist options
  • Save GrantSchiller/6770503 to your computer and use it in GitHub Desktop.
Save GrantSchiller/6770503 to your computer and use it in GitHub Desktop.
function Queue() {
this.length = 0; // I chose this over a for loop in the method getLength because I thought it might be faster. I think the trade off here is that it adds one extra step every dequeue and enqueue rather than spending n steps every method call for getLength.
this.head = null;
this.tail = null;
}
Queue.prototype.enqueue = function(n) {
if(this.length > 0) {
this.tail.setNextNode(n);
this.tail = n
}
else {
this.head = n;
this.tail = n;
}
this.length++;
}
Queue.prototype.dequeue = function() {
if(this.tail == null){
this.head = null;
}
else {
var pimple = this.head // "pimple" gets popped
this.head = pimple.getNextNode();
pimple.setNextNode(null);
this.length--;
}
return pimple;
}
Queue.prototype.peek = function() {
return this.head;
}
Queue.prototype.getLength = function() {
return this.length; // 1 step rather than many as in a for loop.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment