Last active
December 24, 2015 08:29
-
-
Save GrantSchiller/6770503 to your computer and use it in GitHub Desktop.
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.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