Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

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

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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;
    }
    }
    Last edited by Bawnawgwa; March 9th, 2014 at 11:12 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by GregBrannon View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default 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.

Similar Threads

  1. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 06:52 PM
  2. Linked list is not displaying the nodes correctly
    By Jpopto in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 01:02 PM
  3. swapping nodes in a linked list
    By ueg1990 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 10th, 2012, 02:37 PM
  4. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  5. Spreadsheet data - linked list or hashmap
    By j919 in forum Collections and Generics
    Replies: 2
    Last Post: February 19th, 2011, 05:04 PM