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);

        }
    }

Recommended Answers

All 7 Replies

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

Yeah. that would be

startPosition = new NodeDemo(newName, newStudentID, startPosition)

which enters a new node into the list.

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.

You have to loop through the existing list to find the right place to make the insertion. This is probably a good time to revise ypuir course notes, or have alook at some of the material on the web, eg
https://en.wikipedia.org/wiki/Linked_list

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.