class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.first = null;
this.last = null;
this.length = 0;
}
enqueue(value) {
const newNode = new Node(value);
if(this.length === 0){
this.first = newNode;
this.last = newNode;
}else{
// How these two lines can create nested object?
this.last.next = newNode;
this.last = newNode;
}
this.length++;
}
}
const myQueue = new Queue();
myQueue.enqueue('Joy');
...
myQueue.enqueue('Samir');
// Output this.first
{
value: "Joy",
next: {
value: "Matt",
next: {
value: "Pavel",
next: {
value: "Samir",
next: null,
},
},
},
};
Those two lines confused me a lot and I didn't understand them. Since I understand dequeue method, I have removed its code. I would appreciate it if someone could explain this to me.
Because I had put my whole code for better understanding, I have repeated these lines. I had put my whole code for better understanding, I have repeated these lines. Because I had put my whole code for better understanding, I have repeated these lines. Because I had put my whole code for better understanding, I have repeated these lines