#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>
#include <math.h>
#include <time.h>

struct st
{
	float CGPA;
	int age;
	char name[100];
	struct st *next;
};

typedef struct st Student;

void printStudentInfo(Student *a)
{
	if (a!=NULL)
	{
		printf("Name : %s\n",(*a).name);
		printf("Age : %d\n",a->age);
		printf("CGPA : %.2f\n",a->CGPA);
	}
}

void insertNewStudent(Student *list)
{
	Student *t;
	
	t = (Student *)malloc(sizeof(Student));
	
	t -> age = rand()%25+18;
	t -> CGPA = (rand()%400)/100.0;
	strcpy(t->name,"random name");
	t->next = NULL;
	
	while (list->next!=NULL)
		list = list->next;
	
	list->next = t;
	
}

void printList(Student *list)
{
    printf("Printing the whole list.....\n");
	while (list!=NULL)
	{
		printStudentInfo(list);
		list = list->next;
	}
}

Student *deleteFirst(Student *list)
{
	Student *nodeToBeDeleted;
	
	nodeToBeDeleted = list;
	
	list = list->next;
	
	free(nodeToBeDeleted);
	
	return list;
	
}

void deleteSecond(Student *list)
{
	Student *nodeToBeDeleted;
	
	if (list!=NULL && list->next!=NULL)
	{
		nodeToBeDeleted = list->next;
	
		list->next = list->next->next;
	
		free(nodeToBeDeleted);
	}
	
}

Student *insertInOrder(Student* newS, Student *list)
{
	Student * currNode;
	currNode = list;
	
	if (list->CGPA > newS->CGPA) // insert as the first item
	{
		//....... to be written ....
		
		return newS;
	}
	
	
	
	while (currNode->next->CGPA < newS->CGPA)
		currNode = currNode->next;
	
	newS -> next = currNode->next;
	currNode->next = newS;
	
		
}

int main()
{   
	
	Student *head;
	
	int i;
	
	Student me;
		
	srand(time(NULL));
	
	head = (Student *)malloc(sizeof(Student));

	head->age = 23;
	strcpy(head->name,"John Doe");
	head->CGPA = 3.04;
	
	head->next = NULL;

	for (i=0;i<5;i++) 
		insertNewStudent(head);
	
	printList(head);
	
	head = deleteFirst(head);
	
	deleteSecond(head);
	
	deleteSecond(NULL);
	
	printList(head);	
	
}


