Ended the memory leak

This commit is contained in:
2024-11-01 09:44:39 -07:00
parent 50d36d4950
commit c01c467096

108
Q1.c
View File

@@ -3,83 +3,89 @@
#include <string.h> #include <string.h>
typedef struct Student { typedef struct Student {
int id; int id;
char name[30]; char name[30];
double grades; double grades;
struct Student *next; struct Student *next;
} Student; } Student;
// Create a student node // Create a student node
Student *create_student(int id, char *name, double grades) { Student *create_student(int id, char *name, double grades) {
Student *student = (Student*)malloc(sizeof(Student)); Student *student = (Student*)malloc(sizeof(Student));
student->id = id; student->id = id;
strcpy(student->name, name); strcpy(student->name, name);
student->grades = grades; student->grades = grades;
student->next = NULL; student->next = NULL;
return student; return student;
} }
// Insert student at end of the list // Insert student at end of the list
Student* insert_student(Student *head, int id, char *name, double grades) { Student* insert_student(Student *head, int id, char *name, double grades) {
Student *new_student = create_student(id, name, grades); Student *new_student = create_student(id, name, grades);
if(new_student == NULL) {
printf("Creation of Student failed\n");
return new_student;
}
// If the list is empty, the new student we are inserting becomes the start
if(head == NULL) {
return new_student;
}
// If the list is empty, the new student we are inserting becomes the start Student *temp = head;
if(head == NULL) { while(temp->next != NULL) {
return new_student; temp = temp->next;
} }
Student *temp = head; temp->next = new_student;
while(temp->next != NULL) { return head;
temp = temp->next;
}
temp->next = new_student;
return head;
} }
void print_students(Student *head) { void print_students(Student *head) {
Student *temp = head; Student *temp = head;
while(temp != NULL) { while(temp != NULL) {
printf("Student ID: %d\n", temp->id); printf("Student ID: %d\n", temp->id);
printf("Student Name: %s\n", temp->name); printf("Student Name: %s\n", temp->name);
printf("Student Grades: %0.02f\n\n", temp->grades); printf("Student Grades: %0.02f\n\n", temp->grades);
temp = temp->next; temp = temp->next;
} }
} }
Student* find_highest(Student *head) { Student* find_highest(Student *head) {
if(head == NULL) { return NULL; } // Just exit if the list doesnt exist if(head == NULL) { return NULL; } // Just exit if the list doesn't exist
Student *highest = head; Student *highest = head;
Student *temp = head; Student *temp = head;
while(temp != NULL) { while(temp != NULL) {
if(temp->grades > highest->grades) { if(temp->grades > highest->grades) {
highest = temp; highest = temp;
} }
temp = temp->next; temp = temp->next;
} }
return highest; return highest;
}
// To end the memory leaks
void free_student(Student *head) {
Student *temp;
while(head != NULL) {
temp = head;
head = head->next;
free(temp);
}
} }
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, 1357, "Robert W.", 91); head = insert_student(head, 1357, "Robert W.", 91);
head = insert_student(head, 2468, "Jammie R.", 33); head = insert_student(head, 2468, "Jammie R.", 33);
print_students(head); print_students(head);
Student *highest_student = find_highest(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); printf("The highest student is %s, with a grade of: %0.02f", highest_student->name, highest_student->grades);
free_student(head);
return 0; return 0;
} }