CS: linked list confusion (Java)

Click For Summary

Discussion Overview

The discussion revolves around a Java implementation of a linked list, specifically focusing on the method for appending a new node to the end of the list. Participants explore the mechanics of pointer manipulation within the linked list structure, addressing confusion regarding the behavior of the 'tail' and 'next' references.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the relationship between 'tail' and 'tail.next', questioning whether setting 'tail.next' to 'newNode' is negated by subsequently setting 'tail' to 'newNode'.
  • Another participant clarifies that if 'tail' points to the last node, the method correctly appends 'newNode' to the list, explaining that 'tail.next' is updated to point to 'newNode' before 'tail' itself is updated.
  • A further participant suggests that the second line effectively makes 'newNode' the new tail, which aligns with the previous explanation.
  • One participant recommends creating or finding a diagram to visualize the changes in pointers before and after the method execution.
  • A final participant expresses relief at understanding the method's functionality, indicating they felt uncertain prior to the discussion.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of the method, but there remains some initial confusion about the implications of pointer assignments, indicating a lack of consensus on the clarity of the explanation.

Contextual Notes

Participants discuss the method under the assumption that 'tail' correctly points to the last node prior to the method call. There is also mention of the redundancy of setting 'newNode.next' to null, suggesting that the code could be clearer.

Who May Find This Useful

This discussion may be useful for students learning about linked lists in Java, particularly those preparing for exams or seeking clarification on pointer manipulation in data structures.

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 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K