help with adding items to a linked list.

Reply

Join Date: Oct 2005
Posts: 1
Reputation: verbena1 is an unknown quantity at this point 
Solved Threads: 0
verbena1 verbena1 is offline Offline
Newbie Poster

help with adding items to a linked list.

 
0
  #1
Oct 2nd, 2005
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!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: help with adding items to a linked list.

 
0
  #2
Oct 3rd, 2005
  1. import java.util.LinkedList;
  2.  
  3. .....

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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 55
Reputation: NPH is an unknown quantity at this point 
Solved Threads: 1
NPH NPH is offline Offline
Junior Poster in Training

Re: help with adding items to a linked list.

 
0
  #3
Oct 5th, 2005
To add a new node to the end of the linked list you should try:

  1. Node newNode = new Node(characterObject);
  2. if(length == 0)
  3. {
  4. firstNode = tailNode = newNode;
  5. }
  6. else
  7. {
  8. tailNode.next = newNode;
  9. tailNode = newNode;
  10. }
  11. 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, www.NeedProgrammingHelp.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC