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