Update Q1.c
This commit is contained in:
53
Q1.c
53
Q1.c
@@ -19,9 +19,58 @@ Student *create_student(int id, char *name, double grades) {
|
||||
return student;
|
||||
}
|
||||
|
||||
void insert_student(Student *student) {}
|
||||
// Insert student at end of the list
|
||||
Student* insert_student(Student *head, int id, char *name, double grades) {
|
||||
Student *new_student = create_student(id, name, grades);
|
||||
if(new_student == NULL) {
|
||||
printf("Creation of Student failed\n");
|
||||
return new_student;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
// If the list is empty, the new student we are inserting becomes the start
|
||||
if(head == NULL) {
|
||||
return new_student;
|
||||
}
|
||||
|
||||
Student *temp = head;
|
||||
while(temp->next != NULL) {
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
temp->next = new_student;
|
||||
return head;
|
||||
}
|
||||
|
||||
void print_students(Student *head) {
|
||||
Student *temp = head;
|
||||
while(temp != NULL) {
|
||||
printf("Student ID: %d\n", temp->id);
|
||||
printf("Student Name: %s\n", temp->name);
|
||||
printf("Student Grades: %0.02f\n\n", temp->grades);
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
|
||||
Student* find_highest(Student *head) {
|
||||
Student *temp = head;
|
||||
while(temp != NULL) {
|
||||
if(temp->grades > temp->next->grades) {
|
||||
temp = temp->next;
|
||||
}
|
||||
else {
|
||||
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
Student *head = NULL; // Initialize the start of the linked list
|
||||
|
||||
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);
|
||||
|
||||
print_students(head);
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user