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:
head
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:
/** * 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; } }