/******************************************************************************
Student Management Using Linked Lists

Task Description
In this coding task, you will extend a student management system. The program already creates students and stores them in a sorted linked list. Your task is to implement the functionality of adding additional students into the sorted linked list.  Some parts of the program are already implemented.

Key points:
Already Implemented: The student struct is defined. Students are created in the main function and added manually into a linked list.
Your task: The function insertSorted should insert a student into the existing linked list at the correct position. 
The linked list is sorted alphabetically by student names.

Hints:
How to find the position to insert?
Stop iterating over the linked list at the element before the one you want to insert.
In the visualization above, we insert "Benedikt" after "Andreas" and before "Daniel". Stop at "Andreas" and change the pointers accordingly.
Consider the case of adding the student in the middle of the linked list, but also at the end and at the head (index to insert = 0)
Have a look at the testcases

Implement the following functions:

-> insertSorted(Student *list, Student *new_student)

This function takes two arguments:
i) a pointer to the current list of students (list),
ii) a pointer to the students to insert into the list.
Add the student into the existing linked list at the correct position.
The linked list is sorted alphabetically by name. 
The function should return the head of the linked list.

-> freeStudents(Student *list)

Free all students in the linked list.
This is tested with valgrind checks at testcase 2 and 3.
The function should not return anything.

Specifications
Use dynamic memory allocation
The program must compile and run without memory leaks/errors.
Use the existing Linked List.
You don't need to modify the structs or the print function. 
You don't have to consider the case where no more memory is available. 
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Student
{
  char name_[32];
  struct Student *next_;
} Student;

Student* createStudent(char* name)
{
  Student *s = malloc(sizeof(Student));
  if (!s) return NULL;
  strcpy(s->name_, name);
  s->next_ = NULL;
  return s;
}

void printStudents(Student *list)
{
  Student *cur = list;
  int index = 0;
  while (cur != NULL)
  {
    printf("%d: %s\n", index++, cur->name_);
    cur = cur->next_;
  }
}

// TODO: write your code here
Student *insertSorted(Student *list, Student *new_student)
{
    Student *current = list;
    Student *previous = NULL;
    
    while(current != NULL && strcmp(current->name_, new_student->name_) <= 0)
    {
        previous = current;
        current = current->next_;
    }
    
    if(previous == NULL)
    {
        new_student->next_ = current;
        list = new_student;
    }
    else
    {
        new_student->next_ = current;
        previous->next_ = new_student;
    }
    
    return list;
}

void freeStudents(Student *list)
{
    if (!list) return;
    
    freeStudents(list->next_);
    free(list);
    list = NULL;
}
// TODO: write your code here

int main(void)
{
  int test;
  printf("Enter tc: ");
  scanf("%d", &test);
  if(test == 1)
  {
    // test adding at the second and third position
    Student* s1 = createStudent("Andreas");
    Student* s2 = createStudent("Daniel");
    s1->next_ = s2;

    Student* to_add1 = createStudent("Benedikt");
    Student* to_add2 = createStudent("Clemens");

    s1 = insertSorted(s1, to_add1);
    s1 = insertSorted(s1, to_add2);
    printStudents(s1);
    //freeStudents(s1); // uncomment that after you implemented the function
  }
  else if(test == 2)
  {
    // test adding at same first character (T)
    Student* s1 = createStudent("Benjamin");
    Student* s2 = createStudent("Tanja");
    Student* s3 = createStudent("Thomas");
    s1->next_ = s2;
    s2->next_ = s3;

    Student* to_add1 = createStudent("Toni");
    Student* to_add2 = createStudent("Tamim");

    s1 = insertSorted(s1, to_add1);
    s1 = insertSorted(s1, to_add2);
    printStudents(s1);
    //freeStudents(s1); // uncomment that after you implemented the function
  }
  else if(test == 3)
  {
    // test adding in the beginning and end of the LL
    Student* s1 = createStudent("Lenon");
    Student* s2 = createStudent("Margarita");
    s1->next_ = s2;

    Student* to_add1 = createStudent("Julia");
    Student* to_add2 = createStudent("Kilian");

    s1 = insertSorted(s1, to_add1);
    s1 = insertSorted(s1, to_add2);
    printStudents(s1);
    //freeStudents(s1); // uncomment that after you implemented the function
  }

  return 0;
}
