Skip to content

Instantly share code, notes, and snippets.

@Bjacksonshorts
Created October 3, 2013 09:45
Show Gist options
  • Select an option

  • Save Bjacksonshorts/6807569 to your computer and use it in GitHub Desktop.

Select an option

Save Bjacksonshorts/6807569 to your computer and use it in GitHub Desktop.
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;
}
}
@cangevine
Copy link
Copy Markdown

#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment