#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);
	}
}

Student * createNewStudent()
{
	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;
	
	return t;
	
}

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
	{
		newS->next = list;		
		return newS;
	}
	
	// we are not inserting as the first item
	
	while (currNode->next!=NULL && currNode->next->CGPA < newS->CGPA)
		currNode = currNode->next;
	

	if (currNode->next!=NULL) // we are inserting in the middle
	{
		newS->next = currNode->next;
		currNode->next = newS;
	}
	else // inserting at the end
	{
		currNode->next = newS;
		newS->next = NULL;
	}
	

	return list;
	
		
}

int searchByCGPA_recursive(Student *list, float cgpa)
{
	int tmp;
	if (list==NULL) return -1;
	if (list->CGPA==cgpa) return 1;
	else 
	{
		tmp = searchByCGPA_recursive(list->next,cgpa);
		if (tmp!=-1) return tmp+1;
		else return tmp;
	}
	
}

int searchByCGPA(Student *list, float cgpa)
{
	Student *currNode;
	int order;
	
	currNode = list;
	order = 1;
	
	while (currNode!=NULL && currNode->CGPA != cgpa)
	{
		currNode = currNode->next;
		order++;
	}
	
	
	if (currNode==NULL) // item not found
	{
		return -1;
	}
	else return order;
}

int main()
{   
	
	Student *head;
	Student *tmp;
	
	int i;
			
	srand(time(NULL));
	
	head = (Student *)malloc(sizeof(Student));

	head->age = 23;
	strcpy(head->name,"John Doe");
	head->CGPA = 3.04;
	
	head->next = NULL;

	head = insertInOrder(createNewStudent(),head);
	tmp = head;
	for (i=0;i<10;i++)
		head = insertInOrder(createNewStudent(),head);
	
	
	printList(head);

	printf("The student tmp's order (wrt CPGA) in class is %d.\n",searchByCGPA(head,tmp->CGPA));

	printf("CGPA 2.87 is the cpga of student order %d.\n",searchByCGPA(head,2.87));
	printf("The student tmp's order (wrt CPGA) in class is %d.\n",searchByCGPA_recursive(head,tmp->CGPA));

	printf("CGPA 2.87 is the cpga of student order %d.\n",searchByCGPA_recursive(head,2.87));
}


