Created
September 30, 2013 03:47
-
-
Save Bjacksonshorts/6759157 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) { | |
//this.head = n; | |
//n.getNextNode = this.tail; | |
if(this.head == null){ | |
this.head = n; | |
return this.head; | |
} | |
else{ | |
this.tail = n; | |
return this.tail; | |
} | |
} | |
Queue.prototype.dequeue = function(n) { | |
n = this.head; | |
if( n != null){ | |
this.head = null; | |
this.tail = n; | |
} | |
return this.tail;; | |
} | |
Queue.prototype.peek = function(n) { | |
return this.head; | |
} | |
Queue.prototype.getLength = function(n) { | |
var counter = 0; | |
if(this.head != null){ | |
counter++; | |
} | |
return counter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
getLength: there is an infinite loop when you only check the head of the Queue. instead, check each specific node with a local variable (call it n).
only enqueue should take a parameter
enqueue does not need to return anything
More comments from our discussion!