My Queue looks like this.
class Queue{
constructor(){
this.head = this.tail = undefined //empty
this.length = 0
}
enqueue(item){
const node = {value: item} //create a new node
this.length++ //increase length since we are adding an element
if (!this.tail){
this.tail = this.head = {value: item}
}
this.tail.next = node //add the new node to the end of the queue
this.tail = node //move the tail to point to the new node last node.
}
deque(){ //we always deque from the head in queues
if (!this.head){
return undefined //if no head there is nothing to deque so undefined
}
this.length-- //decrease length since we are dequeuing
const head = this.head //save current head in the variable
this.head = this.head.next //move head to point to the next element
head.next = undefined //remove the link to the dequeued element
if (this.length === 0){
this.tail = undefined
}
return head.value
}
peek(){
return this.head?.value //just return the value of the head
}
}
Then I call following methods:
let newQ = new Queue()
newQ.enqueue('rawr')
newQ.enqueue('meow')
//queue looks like this rawr => meow because we add from the tail.
newQ.deque()
//now it looks like this: meow (because rawr was first in and now it's first out)
console.log(newQ.peek())//outputs rawr
Why is the output of my console.log
"rawr" instead of "meow"???
I was expecting a meow instead of a rawr. When I implement the queue using stardard shift()
and push()
methods instead of linked lists, it works as expected. I need to implement this using linked lists because I want to get a better understanding of linked lists.
2
Answers
The main issue is that in
enqueue
, theif
block does not have areturn
statement. As a consequence execution continues to setthis.tail.next
tonode
, so now you have a list with two nodes instead of one, and they both have the same value: "rawr". This explains that after dequeuing one item, the output ofpeek()
is still "rawr".Secondly, there should be no need to create
{value: item}
twice.So change this:
to this:
Not a problem, but:
head.next = undefined;
becausehead
is local variable and will not be accessed afterdeque
has run to completion. In fact, you could actually save the value in a local variable instead of the node reference.deque
has a different meaning and pronunciation thandequeue
. You want the second one.Corrected Snippet:
When you make your initial tail, you are setting its
.next
to itself also. Your codethis.tail.next = node
should be in anelse
block of the aboveif
block, and when you setthis.tail = this.head = {value: item}
, try setting that equal to node instead so you can setthis.head
indirectly the second time you call this method.