Question: Missing Node while Printing a Sorted List (C) I have a linked list that is being sorted alphabetically; however, when printing the list I seem

Missing Node while Printing a Sorted List (C)

I have a linked list that is being sorted alphabetically; however, when printing the list I seem to be missing a node in the output, I believe what is happening is that one of the nodes is being set to NULL while being swapped

Sorting and Swapping:

int compare_by_lastname(node_t *a, node_t *b) // these two functions { return strcmp(a->lastName, b->lastName); } // compare two nodes int compare_by_firstname(node_t *a, node_t *b) { return strcmp(a->firstName, b->firstName); } void swapNode(node_t* a, node_t* b) // helper function for sortList() { node_t temp = *a; *a = *b; *b = temp; } void sortList(node_t **head, int (*comp)(node_t*, node_t*))  // sorting using function pointer { node_t *temp1 = NULL; node_t *temp2 = NULL; int swapped = 0; if (*head == NULL) { return; } do { swapped = 0; temp1 = *head; while (temp1->next != temp2) { if (comp(temp1, temp1->next) > 0) { swapNode(temp1, temp1->next); swapped = 1; } temp1 = temp1->next; } temp2 = temp1; } while (swapped); }

Printing the List:

while (temp != NULL) // node_t* temp = head { // if provided day of the month is invalid, skip to the next node if (checkInvalidDate(temp) == true) { temp = temp->next; } fprintf(out, "Name:\t%s %s ", temp->firstName, temp->lastName); fprintf(out, "Date of Birth:\t%s %s, %s ", temp->bd.month, temp->bd.day, temp->bd.year); fprintf(out, "Major:\t%s ", temp->major); fprintf(out, "Year:\t%s ", temp->classStanding); temp = temp->next; // next element }

Additionally, here is the file that the code reads from to create the list:

Smith,Todd,July,31,2002,CS/BS,Junior Doe,Jane,December,12,2003,CS/BS,Sophomore Doe,John,December,12,2003,CS/BS,Sophomore Joe,Average,February,3,2002,CS/BS,Freshman

The sorting functions do appear to be sorting the list as needed, but I think one of the nodes is NULL since the issue only appeared after the implementation of sortList(), so I would appreciate the corrections wherever they are needed. Thanks!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!