How do you create a node with fullName and studentID in the "add" method and then put them into the correct positions which the name_first and name_second should move until the name in the name_first is larger than the new_Name and name_second is smaller than the new_Name?

public class NodeDemo{
        private String fullName;
        private int studentID;
        private NodeDemo fullNameLink;
        private NodeDemo studentIDLink;

        public NodeDemo(String newName, int newStudentID, NodeDemo newFullNameLink, NodeDemo newStudentIDLink){
            fullName = newName;
            studentID = newStudentID;
            fullNameLink = newFullNameLink;
            studentIDLink = newStudentIDLink;
        }

        private static NodeDemo startPosition;

        public void add(String new_Name, int newStudentID){
            NodeDemo name_First = startPosition.fullNameLink;
            NodeDemo name_Second = startPosition;
            NodeDemo studentId_First = startPosition.studentIDLink;
            NodeDemo studentId_Second = startPosition;

            NodeDemo node = new NodeDemo(new_Name, newStudentID, null, null);

        }
    }

Is this a multiply-linked list? Do you know how to insert a node into a single-linked list?

Edited 3 Years Ago by JamesCherrill

Yeah. that would be

startPosition = new NodeDemo(newName, newStudentID, startPosition)

which enters a new node into the list.

Edited 3 Years Ago by boiishuvo

That's what the method call could look like, but I meant do you know/understand the algorithm for inserting a node into the correct position in a singly-linked list? The multiply linked list can be confusing, but if you get the simpler single case working first then the multiple case is just an incremental step.

Not really. What I've wrote so far is

if(name_first == null) {
            name_second = new Node(newName, newStudentID, name_first, name_second);
        }
        else {
            if() // if name_first is smaller than the name_second by alphabetical order
        }

This part is what I'm confused with.

Edited 3 Years Ago by boiishuvo

public void addToList(String newName, int newStudentID){
        public void add(String new_Name, int newStudentID){
        NodeDemo name_First = startPosition.fullNameLink;
        NodeDemo name_Second = startPosition;
        NodeDemo studentId_First = startPosition.studentIDLink;
        NodeDemo studentId_Second = startPosition;

        NodeDemo newnode = new NodeDemo(newName, newStudentID, null, null);

        if(name_first == null)
            name_second = new NodeDemo(newName, newStudentID, name_first, name_second);
        else {
            if(newName.compareToIgnoreCase(name_second.fullNameLink.name) < 0) {
                name_second = new NodeDemo(newName, newStudentID, name_first, name_second.fullNameLink);
                name_second = name_second.fullNameLink;
            }
        }

        if(studentID_first == null)
            studentID_second = new NodeDemo(newName, newStudentID, studentID_first, studentID_second);
        else {
            if(newStudentID > studentID_second.studentIDLink.id) {
                studentID_second = new NodeDemo(newName, newStudentID, studentID_first, studentID_second.studentIDLink);
                studentID_second = studentID_second.studentIDLink;
            }
        }
    }

It still does not work. What I expected is to put the new nodes' name in the alphabetical order and studentID in numerical order. Suggestions?

Edited 3 Years Ago by boiishuvo

You can only add one Node, so either add it by name, then fix the ID links afterwards, or collect the info on where it fits into both lists before creating the Node.

This article has been dead for over six months. Start a new discussion instead.