Nodes contain array data sets in a linked list. Need help iterating

• March 9th, 2014, 10:16 PM
Bawnawgwa
Nodes contain array data sets in a linked list. Need help iterating
TL,DR: observe the nodes just below, that is the data each node in the link list contains. How do I iterate through the entire list to reach the last element (H), so I can add an element right after it?

Ok, this could get complicated so hopefully I can keep it understandable.

So I have 4 nodes doubly linked with two dummy nodes for head and tail:

node1 = {A}
node2 = {null, B}
node3 = {C, null, D, E}
node4 = {null, F, null, G, null, null, H, null}
tail
Ok, so since the list only contains 8 elements that are not null, its size is actually 8 correct? So now lets say I have an add method that has

add(E item) and inserts the item at the end of the list. So I can get to the last node with tail.previous(), but then how do I iterate to the end so I can add the item after the last item in the list (H). I guess I don't know how you only access one nodes data when that data is an array with empty spaces.

Here is the entire Node code:

Also, I can't just iterate through the whole thing because I am not supposed to. I am supposed to just find the right node and iterate through that only.

It's times like these I wonder why I bother going to lecture when we are given absolutely nothing that can help with homework, I have been trying to make sense of this for ages and just don't fully understand how to maneuver around a linked list containing nodes where each node contains an array.

Would LOVE any help I can get with this :)

--- Update ---

Sorry here is Node code if it helps understand anything:

Code :

```/** * Node class that makes up a DoublingList. Feel free to add methods / * constructors / variables you might find useful in here. */ public class Node<E> {   /** * The node that comes after this one in the list */ private Node<E> next;   /** * The node that comes before this one in the list */ private Node<E> prev;   /** * The data held within this node */ private E[] data;     /** * Constructs a new unlinked node with the given data * @param data */ public Node(E[] data) { this(null, null, data); }   /** * Constructs a new Node with the given information * @param next * @param prev * @param data */ public Node(Node<E> next, Node<E> prev, E[] data) { this.next = next; this.prev = prev; this.data = data; }   /** * Returns the node that comes after this node * @return */ public Node<E> getNext() { return next; }   /** * Sets this node's next node to the given value * @param next */ public void setNext(Node<E> next) { this.next = next; }   /** * Returns the node that comes before this node * @return */ public Node<E> getPrev() { return prev; }   /** * Sets this node's previous node to the given value * @param prev */ public void setPrev(Node<E> prev) { this.prev = prev; }   /** * Returns the data held within this node * @return */ public E[] getData() { return data; }   /** * Sets the data held within this node to the given value * @param data */ public void setData(E[] data) { this.data = data; } }```
• March 10th, 2014, 03:27 AM
GregBrannon
Re: Nodes contain array data sets in a linked list. Need help iterating
I really don't understand your description of the nodes. Each node contains a pointer to the previous node, a pointer to the next node, and data. With that, what is this:

node4 = {null, F, null, G, null, null, H, null} ?

To iterate, start at any node, perhaps the head, and follow the 'next' pointer to each successive node. You could also start at the tail and iterate backwards by following the 'prev' pointer.
• March 10th, 2014, 06:18 PM
Bawnawgwa
Re: Nodes contain array data sets in a linked list. Need help iterating
Quote:

Originally Posted by GregBrannon
I really don't understand your description of the nodes. Each node contains a pointer to the previous node, a pointer to the next node, and data. With that, what is this:

node4 = {null, F, null, G, null, null, H, null} ?

To iterate, start at any node, perhaps the head, and follow the 'next' pointer to each successive node. You could also start at the tail and iterate backwards by following the 'prev' pointer.

that information is the data that the nodes are holding. Therefore, I must find the right node, THEN find the last data entry of that node. In this case I need to find node 4, then find location of H, which is last data entry, then put the new item in spot H-index + 1.
• March 10th, 2014, 06:24 PM
Norm
Re: Nodes contain array data sets in a linked list. Need help iterating
Can you post a complete program that compiles, executes and shows the problem?
• March 10th, 2014, 07:11 PM
GregBrannon
Re: Nodes contain array data sets in a linked list. Need help iterating
Ahh, each data package is an E[] array. The answer to your question is:

The last node is the tail's prev (as you already said).
Then iterate the node's data package just as you would any array to find the nulls for available slots. Or, if you're just adding the new item at the end of the existing data package, you can use the length attribute of the data array to find the last index.

--- Update ---

Ahh, each data package is an E[] array. The answer to your question is:

The last node is the tail's prev (as you already said).
Then iterate the node's data package just as you would any array to find the nulls for available slots. Or, if you're just adding the new item at the end of the existing data package, you can use the length attribute of the data array to find the last index.