1

I am currently learning Java and data structures, and I am trying to use insertion to insert the values in a double array to a LinkedList by only inserting each element at the end of the list. I've seen many samples online but the structure I am working with is different from what I've seen which is throwing me off and I tried implementing it in the same way. I have three Java files in the same folder: Project.java, LinkedNode.java, WA7_List.java.

Project.java

public class Project{

     public static void main(String[], args) {
     double[] all_inserts = [0.76697, 0.08368, 0.37785, 0.07386, 0.77287]
     }

     WA7_List linked_list = new WA7_List(M); // M is an int variable used later but can be ignored for this post.
     for (int i=0; i<all_inserts.length; i++)
        {
            linked_list.add_simple(all_inserts[i]);
        }
}

LinkedNode.java

public class LinkedNode {
    public LinkedNode next;
    public double item;

    public LinkedNode(LinkedNode next, double item) {
        this.next = next;
        this.item = item;
    }
}

WA7_List

public class WA7_List {
    private LinkedNode first_node;
    private LinkedNode last_node;
    private LinkedNode[] shortcuts;
    private int count_shortcuts;
    private int count_nodes;
    private int M;

    public WA7_List(int M) {
        this.first_node = null;
        this.last_node = null;
        this.shortcuts = null;
        this.count_nodes = 0;
        this.count_shortcuts = 0;
        this.M = M;
    }

    public void add_simple(double item) {
        LinkedNode newNode = new LinkedNode(null, item);
        if (first_node==null)
        {
            first_node=newNode;
            count_nodes+=1;
        }
        last_node = first_node;
        while (null != last_node.next)
        {
            last_node = last_node.next;
        }
        last_node.next = newNode;
    }

     // There are more methods after but just focused on this first method.
}

Currently, the code is stuck in a forever loop because of the while loop in the add_simple method. I'm not sure what I am doing wrong here.

6
  • Do you realy need last_node and first_node? You could succeed by use only first_node.
    – Grim
    Commented Feb 26 at 1:38
  • If you executefirstNode = newNode you’re done. Don’t proceed to the loop below in that case. You are creating a data loop.
    – user207421
    Commented Feb 26 at 1:38
  • @Grim I assume I dont really need it for this method, it was part of the skeleton code and was approaching it where the last_node would keep track of most recent insertion because the linkedlist should be in the same order as all_inserts.
    – rzan1
    Commented Feb 26 at 1:44
  • Actually you shouldn’t have the loop at all. The idea is to maintain lastNode such that it always refers to the most recently added entry, so that you don’t have to loop at all.
    – user207421
    Commented Feb 26 at 1:46
  • @user207421 Ill have to read up some more on how this way works. The skeleton code had the all those variables in the constructor of WA7_List so I was thinking that I had to keep track of the first_node and last_node which I assume would be the last element inserted into the linked list. But correct me if im wrong!
    – rzan1
    Commented Feb 26 at 1:47

2 Answers 2

1

Try this. Read comments for explanation.

public void add_simple(double item) {
    LinkedNode newNode = new LinkedNode(null, item);
    if (first_node == null) {
        first_node = newNode;// init first element
        last_node = newNode;// if first_node==null, the last_node is null too
        count_nodes += 1;
        return; //do nothing else if first element
    }
    last_node.next = newNode;// link new element to last element in list
    last_node = newNode; // item now is the last element
    count_nodes += 1;
}
-3

Try

public void add_simple(double item) {
    LinkedNode newNode = new LinkedNode(null, item);
    if (first_node == null) { // if list is empty and count_nodes is 0
        first_node = newNode; // we set the first node only on first insert
    } else {
        // list is not empty what automatically means we must have a last node
        last_node.next = newNode; // the new is the next of the last node
    }
    count_nodes ++;
    last_node = newNode; //always: the new is the last
}
8
  • You could add synchronized between public and void to surprise your teacher.
    – Grim
    Commented Feb 26 at 1:57
  • Oh I would love to try that, but this professor would try to fail me in this class for changing his skeleton code. Ha!
    – rzan1
    Commented Feb 26 at 2:00
  • @rzan1 the skeleton code remains untouched. Oh you mean the synchronized-thing? yea, correcting the teacher is a very bad and offensive thing, even if it is professional.
    – Grim
    Commented Feb 26 at 2:02
  • 2
    Grim you should not do people’s homework for them. You should tell them how to do it, not show it. Unless you get the course credit yourself ;-)
    – user207421
    Commented Feb 26 at 2:03
  • I've tried what you have written and it seems to compile and run without errors. When I have WA7_List linked_list = new WA7_List(M);, does this create a linked list or would I have to create and add to a LinkedList<WA7_List> ? That also throws me off a bit because at first I tried something like LinkedList<WA7_List> linked_list = new LinkedList<>(); but no one online seems to be doing that.
    – rzan1
    Commented Feb 26 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.