I am having a hella a time with this and I can not figure out why.
Here is what I am trying to do:
I have a C struct that has a pointer to the next node and then all the data types it holds. I am trying to do an insertion sort based on the snew->total.
I have this:
struct student *head is the first element in the linked list and struct student *snew is what I am trying to insert into the linked list.
The student struct looks like this:
I am also trying to print the linked list after it has been created and I think I have that right but might as well put it up while I am doing this:
Any guidance is appreciated!!
Here is what I am trying to do:
I have a C struct that has a pointer to the next node and then all the data types it holds. I am trying to do an insertion sort based on the snew->total.
I have this:
Code:
void insert_student (struct student * head, struct student * snew)
{
struct student * child;
struct student * parent;
child = (struct student *) malloc(sizeof(struct student));
parent = (struct student *) malloc(sizeof(struct student));
if(head->next != NULL)
{
for(parent=head,child=head->next; child!=NULL && child->total<snew->total; parent=child,child=child->next);
parent->next = snew;
snew->next = temp;
}
else
{
head->next = snew;
}
}
The student struct looks like this:
Code:
struct student {struct student * next; char name[20]; int midterm; int final; int total; char grade; };
I am also trying to print the linked list after it has been created and I think I have that right but might as well put it up while I am doing this:
Code:
void report_grades(struct student * head)
{
struct student *temp;
temp = (struct student *) malloc(sizeof(struct student));
printf("Name\tMidterm\tFinal\tTotal\tGrade\n");
for(temp=head; temp!=NULL; temp=temp->next)
{
printf("%s \t%i \t%i \t%i \t%c", temp->name, temp->midterm, temp->final, temp->total, temp->grade);
}
}
Any guidance is appreciated!!