Created
October 3, 2013 09:45
-
-
Save Bjacksonshorts/6807569 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.tail = null; | |
this.head = null; | |
} | |
Queue.prototype.enqueue = function(n) { | |
if(this.head == null){ | |
this.head = n; | |
this.tail = n; | |
} | |
else{ | |
this.tail.setNextNode(n); | |
this.tail = this.tail.getNextNode(); | |
} | |
} | |
Queue.prototype.dequeue = function() { | |
if(this.tail != null){ | |
this.head = this.head.getNextNode(); | |
return this.head; | |
} | |
} | |
Queue.prototype.peek = function(n) { | |
return this.head; | |
} | |
Queue.prototype.getLength = function() { | |
if(this.head == null){ | |
return 0; | |
} | |
else{ | |
var counter = 1; | |
while(this.head.getNextNode() != null){ | |
counter++; | |
this.head = this.head.getNextNode(); | |
} | |
return counter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#19: This line is problematic: since we have just updated the value of this.head in the previous line, we have lost access to the node that we are trying to return