Search code examples
c++linked-listnodessingly-linked-list

Having trouble with inserting nodes in a sorted fashion


I have a linked list that I have to insert objects into, based on one of the fields of the object I must insert nodes into the linked list in correct order.

I have had the sort working perfectly when using arrays and vectors but am having trouble simply with the insertion aspect of the linked list. my getLink() call is for a function that gets my link which is = next.

void sortedInsert(DomNodePtr& head, string fName, string lName, float CGPA, int rScore,string prov){
     DomNodePtr here = head;
     DomNodePtr tempPtr;
     tempPtr = new DomNode(fName, lName, CGPA, rScore, prov, head);

     while (here->getCumGPA() > CGPA && here->getLink() != NULL){
     here = here->getLink();

     }
     if (here->getCumGPA() < CGPA){
         tempPtr->setLink(here->getLink());
         here->setLink(tempPtr);
     }
     else if (here->getCumGPA() > CGPA){
         here = tempPtr;
     }
}

Basically I want the students with the highest Cumulative GPA to be sorted higher than the ones with lower CGPA. I know part of my problem is I do not insert the students with lower CGPAs but I am struggling with that part. It also is outputting about 10 students when I print the linked list when in fact their are about 100 and they are not in correct order.


Solution

  • When inserting the new student to the list there are three cases:

    • The CGPA for the new element is smaller than the CPGA values of all elemnts in the list. In this case the student has to be attached to the end of the list.
    • The student has a CPGA larger than all elements of the list: The new element has to be added at the head of the list
    • The student has a CPGA between two existing elements. Here the new element has to be inserted between those to elements. Therefor, you have to keep track of the previous element which has a CPGA larger than the CPGA of the new element.

      void sortedInsert(DomNodePtr& head, string fName, string lName, float CGPA, int rScore,string prov){
          DomNodePtr here = head;
          DomNodePtr tempPtr;
          tempPtr = new DomNode(fName, lName, CGPA, rScore, prov, head);
      
          DomNodePtr previous = NULL; // keeping track of the previous element
      
          while (here->getCumGPA() >= CGPA && here->getLink() != NULL){
              previous = here;
              here = here->getLink();
          }
          if (here->getLink() == NULL){
              // Insert new student at the end of the list
              // If CPGA is larger for all students in the list
      
              here->setLink(tempPtr);
          }
          else if (previous = NULL) {
              // The new student has the highest CGPA and
              // has to be added at the head of the list
              tempPtr->setLink(here);
          }
          else{
              // Insert the student between the current
              // and the previous elements of the list
      
              previous->setLink(tempPtr);
              tempPtr->setLink(here);
          }
      }