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;

then I enter my while loop to read in my characters and try adding them to the nodes:
newNode.next = tailNode;
tailNode = newNode;

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!!

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;

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;
    tailNode.next = newNode;
    tailNode = newNode;

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, www.NeedProgrammingHelp.com

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.