Hi, I\'m trying to figure out if the first item in a doubly linked list is store
ID: 3592112 • Letter: H
Question
Hi,
I'm trying to figure out if the first item in a doubly linked list is stored in the head node. I have the below image as a reference. I believe the answer is no and it's filled with "null"
but I'm really not sure based on what I've read. Thanks for any help.
Constructor As with ArrayList, in the LinkedList constructor we call the clear( method. The clear method sets the size of the list to zero, and increments modCount. Since LinkedList uses nodes instead of an array, we have to initialize head and tail. First we instantiate head such that it has null values for the previous and next nodes, which is appropriate since the list is empty. Next we instantiate tail such that its previous node is head, and its next node is null. Finally, we set head to point to tail as its next node. The diagram in Figure 6.3 shows what an empty LinkedList looks like using the implementation discussed above: null prev "HEAD" next prev TAIL" next null Figure 6.3. A diagrammatic representation of an empty, doubly linked list, based on the implementation discussed in this moduleExplanation / Answer
Yes, you're correct. Based on the above diagram, one can figure out that the head and tail are basically pointing to the first and last element in the linked list respectively.
Imagine a scenario when the first element is to be pushed into the linked list, it must come in between head and tail. If for instance, we choose to save it in the head, then the tail has to point to the head to complete and maintain the link!
Now, considering the arrival of second and third and so on elements, we find that head has a value but tail does not. Here during traversal one can find the first element in head but he/she cannot find the last value in tail would be in the tail->prev. So again the purpose is defied.
FEEL FREE TO ASK FURTHER QUERIES IN THE COMMENTS. HOPE THIS HELPS!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.