Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C, create a modular program that creates a linked list of student nodes that

ID: 3826707 • Letter: I

Question

In C, create a modular program that creates a linked list of student nodes that is in ascending order by last names (if last names are same, order by first names) and displays(in an organized manner) the data entered by traversing the list.

The user can enter data for an unspecified number of students.

Here's the structure for the nodes:

typedef struct studNode

{

char firstName[20];

char lastName[20];

unsigned int studID;

char major[30];

double gpa;

struct studNode *nextStuPtr;

} studNode_t;

Heres how it looks:

Enter the first student's first name or -1 to end input:

Enter the student's last name:

Enter the student's ID number:

Enter the student's major:

Enter the student's gpa:

NAME OF STUDENT STUDENT ID MAJOR GPA

( *Names in ascending order by last name. If last names are same, then compar first names )

Explanation / Answer

#include<stdio.h>
#include<string.h>
typedef struct studNode
{
char firstName[20];
char lastName[20];
unsigned int studID;
char major[30];
double gpa;
struct studNode *nextStuPtr;
} studNode_t;

studNode_t *head;


void input() {
   char choice;
   studNode_t *tmp;
   head = NULL;
   do {
       tmp = malloc(sizeof(studNode_t));
       printf("Enter the first student's first name or -1 to end input: ");
       scanf("%s", tmp->firstName);
       if (strcmp(tmp->firstName, "-1") == 0)
           break;
       printf("Enter the student's last name: ");
       scanf("%s", tmp->lastName);
       printf("Enter the student's ID number: ");
       scanf("%ud", &(tmp->studID));
       printf("Enter the student's major: ");
       scanf("%s", tmp->major);
       printf("Enter the student's gpa: ");
       scanf("%lf", &(tmp->gpa));
       tmp -> nextStuPtr = head;
       head = tmp;
   }while (1);
}
int compare(const studNode_t *a, const studNode_t *b) { //compare two nodes and return <0 if node a preceeds b
   int cmp   = strcmp( a->lastName, b->lastName);
   if (cmp == 0)
       return strcmp( a->firstName, b->firstName);
   return cmp;
}

studNode_t* parent(studNode_t *child) { //find the parent of given node
   studNode_t *tmp = head;
   while (tmp != NULL){
       if (tmp->nextStuPtr == child)
           return tmp;
       tmp = tmp->nextStuPtr;
   }
}
void sort(){ //sorts the Linked list
   studNode_t *low, *tmp, *tmpHead=NULL;

   while(head != NULL) {
       low = head;
       tmp = head->nextStuPtr;
       while (tmp!=NULL) { //find the lowest value node
           if (compare(low,tmp) < 0)
               low = tmp;
           tmp = tmp->nextStuPtr;
       }
        //then remove lowest from main LL and prepend it to new LInked Lisr

       if (low == head) {
           tmp = tmpHead;
           tmpHead = head;
           head = head -> nextStuPtr;
           tmpHead -> nextStuPtr = tmp;
       }
       else {
           tmp = tmpHead;
           tmpHead = low;
           parent(low) -> nextStuPtr = low -> nextStuPtr;
           tmpHead -> nextStuPtr = tmp;
       }
   }      
   head = tmpHead;
}

void disp() {
   studNode_t *tmp = head;
   printf("NAME OF STUDENT STUDENT ID MAJOR GPA ");
  
   while (tmp != NULL){
       printf("%s %s %ud %s %lf ",tmp->firstName, tmp->lastName, tmp->studID, tmp->major, tmp->gpa);
       tmp = tmp -> nextStuPtr;
   }
  
}
void main() {
   input();
   sort();
   disp();
}

This code will serve your purpose. Incase you are facing difficulty understanding the code, please let me know in the comment section. I shall comment the code to make things easy.