Hi guys I have what I think is a really simple problem but I just don't seem to GET Linked Lists or at least how the nodes work.

I have a class that implements these methods:
public boolean add(Object newEntry);

public boolean add(int newPosition, Object newEntry);

public Object remove(int givenPosition);

public void clear();

public boolean replace(int givenPosition, Object newEntry);

public Object getEntry(int givenPosition);

public boolean contains(Object anEntry);

public int getLength();

public boolean isEmpty();

public boolean isFull();

public void display();

and my Node inner class consists of :

private Object data
private Node next

private void setData(Object dataPortion)

private Object getData()

private void setNextNode(Node nextNode)

private Node getNextNode()

I have a read method that reads in input from a file (its just a small string of characters) and i first call clear to make sure the node's are clear (firstNode is null and length is 0). I call the add method like this:
add(characterObject) and it adds the first character but it will not loop it just exits out. How in the world do I loop this. I have to use several variables to point to this Node's (firstNode, tailNode, newNode) I tried creating a new node:

Node newNode = new Node(characterObject);
tailNode = newNode;
firstNode = newNode;
length++;

then I enter my while loop to read in my characters and try adding them to the nodes:
add(characterObject);
newNode.next = tailNode;
tailNode = newNode;
length++;

All I get is an infinite loop where it adds the first character, an "A".
Obviously I am not incrementing the pointer to the Node's .next property. Hell I'm not even sure that the Node's Data part is working correctly other that reading in the first character. I tried running the program through a debugger and It apparently read in my next character but the character is never assigned. My instructor sent me this but this only confused me more

Before the while loop (the list is empty):
create a node (newNode)
firstNode and tailNode are both reference to the newNode
(now we have a node)

Inside the while loop (keep adding a newNode into the list):
create a node (newNode)
setup the link ("next" data field) of the "old" tailNode
the newNode is the "new" tailNode
(now we have one more node and repeat the same loop)

Any help would be appreciated, I finished all the initial implementation and all my methods work fine its the looping to add part which is confusing me, I can add indivisual items, I can display them, I can even replace items, but I have know idea about the file reading part. HELP!!

Dani AI

Generated

The symptom (only the first character appears and the program hangs when traversing) points to a linking problem and/or node reuse rather than file I/O. Creating one Node and trying to “reuse” it for every character or linking the new node back to the old tail produces a cycle where the list never reaches a null terminator. In a correct append operation the existing last node’s next reference is updated to point at a freshly allocated node, and the new node becomes the last node; when the list is empty both head and tail reference that single new node.

Quick, practical checks that expose the bug:

  • Make sure a new Node object is allocated on every loop iteration; never reuse the same Node instance for multiple entries.
  • After the very first add, verify length == 1, head and tail both reference the same node, and that the tail’s next reference is null.
  • After a later add, verify length increased, head != tail, and the (current) tail’s next is null. If tail.next is non-null or points back into the list, a cycle exists.
  • Protect the display/traverse routine by stopping when the current node is null or after visiting at most length nodes; this avoids infinite printing when a cycle exists and helps locate the faulty step.

About the read loop and method responsibilities: the reading loop should call the list’s add method once per character (or build fresh nodes inside the loop). Either let add(...) do all pointer updates, or do all linking outside it — mixing both often causes errors. Use simple invariants (head==null iff length==0; tail.next==null) as assertions during debugging.

This agrees with ’s observation about the circular reference and follows the insertion-order idea explained by ; ’s node helper illustrates how small Node utilities can simplify insertion logic.

Recommended Answers

All 3 Replies

import java.util.LinkedList;

.....

Couldn't be simpler to use a linked list in Java, one is provided as standard for your enjoyment :)

newNode.next = tailNode;
tailNode = newNode;
length++;

All I get is an infinite loop where it adds the first character, an "A".
Obviously I am not incrementing the pointer to the Node's .next property

Kinda weird as there are no pointers for your manipulation in Java.
I do notice you are creating a circular reference here, probably not what you intended.

To add a new node to the end of the linked list you should try:

Node newNode = new Node(characterObject);
if(length == 0)
{
    firstNode = tailNode = newNode;
}
else
{
    tailNode.next = newNode;
    tailNode = newNode;
}
length++;

The problem with your code is that you update tailnode in your add method and then you try to set it's next to be the newNode. But by this time, the tailNode is already newNode. So you just created a circular reference (newNode == newNode.next)

This problem doesn't occur in the above code because tailNode is modified after we modify tailNode.next.

tailNode.next "links in " the newNode. and then we make newNode the tailNode.

:?: For more help,

Member Avatar for Member #884252

here is my node class:

public class Node {
  private Object element; // we assume elements are character strings
  private Node next;
  /** Creates a node with the given element and next node. */
  public Node(Object s, Node n) {
    element = s;
    next = n;
  }
  public void addNodeAfter(Object newElem){
      next = new Node(newElem, next);
  }
  /** Returns the element of this node. */
  public Object getElement() { return element; }
  /** Returns the next node of this node. */
  public Node getNext() { return next; }
  // Modifier methods:
  /** Sets the element of this node. */
  public void setElement(Object newElem) { element = newElem; }
  /** Sets the next node of this node. */
  public void setNext(Node newNext) { next = newNext; }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.