Update Q1.c
This commit is contained in:
18
Q1.c
18
Q1.c
@@ -53,25 +53,33 @@ void print_students(Student *head) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Student* find_highest(Student *head) {
|
Student* find_highest(Student *head) {
|
||||||
|
if(head == NULL) { return NULL; } // Just exit if the list doesnt exist
|
||||||
|
|
||||||
|
Student *highest = head;
|
||||||
Student *temp = head;
|
Student *temp = head;
|
||||||
|
|
||||||
while(temp != NULL) {
|
while(temp != NULL) {
|
||||||
if(temp->grades > temp->next->grades) {
|
if(temp->grades > highest->grades) {
|
||||||
|
highest = temp;
|
||||||
|
}
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
}
|
}
|
||||||
else {
|
return highest;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Student *head = NULL; // Initialize the start of the linked list
|
Student *head = NULL; // Initialize the start of the linked list
|
||||||
|
|
||||||
head = insert_student(head, 1234, "John Doe", 88.5);
|
head = insert_student(head, 1234, "John Doe", 88.5);
|
||||||
head = insert_student(head, 2468, "Jammie R.", 33);
|
|
||||||
head = insert_student(head, 1357, "Robert W.", 91);
|
head = insert_student(head, 1357, "Robert W.", 91);
|
||||||
|
head = insert_student(head, 2468, "Jammie R.", 33);
|
||||||
|
|
||||||
print_students(head);
|
print_students(head);
|
||||||
|
|
||||||
|
Student *highest_student = find_highest(head);
|
||||||
|
printf("The highest student is %s, with a grade of: %0.02f", highest_student->name, highest_student->grades);
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user