944,076 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 14000
  • Java RSS
Oct 2nd, 2005
0

help with adding items to a linked list.

Expand Post »
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!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
verbena1 is offline Offline
1 posts
since Oct 2005
Oct 3rd, 2005
0

Re: help with adding items to a linked list.

Java Syntax (Toggle Plain Text)
  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

Quote ...
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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Oct 5th, 2005
0

Re: help with adding items to a linked list.

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

Java Syntax (Toggle Plain Text)
  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
NPH
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NPH is offline Offline
55 posts
since May 2005
9 Days Ago
0
Re: help with adding items to a linked list.
here is my node class:
Java Syntax (Toggle Plain Text)
  1. public class Node {
  2. private Object element; // we assume elements are character strings
  3. private Node next;
  4. /** Creates a node with the given element and next node. */
  5. public Node(Object s, Node n) {
  6. element = s;
  7. next = n;
  8. }
  9. public void addNodeAfter(Object newElem){
  10. next = new Node(newElem, next);
  11. }
  12. /** Returns the element of this node. */
  13. public Object getElement() { return element; }
  14. /** Returns the next node of this node. */
  15. public Node getNext() { return next; }
  16. // Modifier methods:
  17. /** Sets the element of this node. */
  18. public void setElement(Object newElem) { element = newElem; }
  19. /** Sets the next node of this node. */
  20. public void setNext(Node newNext) { next = newNext; }
  21. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Gsterminator is offline Offline
26 posts
since Jan 2011
Message:
Previous Thread in Java Forum Timeline: Getting mouse coordinates from MouseListener
Next Thread in Java Forum Timeline: add method for linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC