Comp Sci CS: linked list confusion (Java)

AI Thread Summary
The discussion centers around confusion regarding the implementation of a linked list in Java, specifically how the `tail` and `tail.next` references are manipulated in the `bar` method. The first line sets the `next` pointer of the current `tail` node to the new node, effectively linking it to the new node. The second line updates `tail` to point to the new node, making it the new end of the list. The third line sets the `next` pointer of the new node to null, indicating it is the last node in the list. Clarification is provided that while `tail` and `tail.next` are related, they are distinct references, and understanding their roles is crucial for grasping linked list operations.
n00neimp0rtnt
Messages
15
Reaction score
0
I just took my final for Data Structures and there was a question on it dealing with linked lists. A part of the question really threw me off. It looked something like this:

Code:
public class Node
{
     int contents;
     Node next;
}

public class LinkedList
{
     Node head, tail;
}

public void bar(int c)
{
     newNode = new Node();
     newNode.contents = c;
     [B]tail.next = newNode;
     tail = newNode;
     newNode.next = null;[/B]
}

The part of concern is in bold. When I read this, it looks like in the first line, tail.next is getting set to the Node that the reference "newNode" is pointing to. In the SECOND line, however, tail is getting set to the Node that "newNode" points to. If I'm not mistaken, doesn't that just nullify the first statement as if it was never executed? What happens to tail.next? After the second statement, isn't tail.next pointing to the same Node that newNode.next points to? I thought it was a typo so I asked my professor and he simply said that "tail.next is not 'inside' tail; they are two separate things," which didn't really help much.
 
Physics news on Phys.org
If the precondition for the method is that tail points to the last Node on the list, then this method appends a new node to the end of a single-linked list without iterating through the whole list. Regarding your 3 highlighted lines:

Line 1: The previous tail node (pointed to by tail) is set to point to the new node, that is, the new node will now come after the previous tail node.
Line 2: The new node is then set as the new tail.
Line 3: The next node for the new node is set to null since this it is currently the tail (with the given code this line is for better readability rather than for necessity; the next field already has the value null).
 
Hmm...so line 2 kinda "pushes back" the old tail and sets newNode as the NEW tail?
 
Thats one way of putting it. If you have trouble visualize it then make (or search for) a diagram that show what is pointing to what before and after the two lines.
 
Ok, I think I get it now. I'm pretty sure I got the questions relating to it right because I had a hunch what the methods were supposed to do, it just bothered me that I didn't know how it worked.
 

Similar threads

Replies
3
Views
2K
Replies
9
Views
3K
Replies
5
Views
3K
Replies
2
Views
4K
Replies
1
Views
10K
Replies
2
Views
1K
Replies
11
Views
12K
Replies
10
Views
2K
Replies
2
Views
1K
Back
Top