You are on page 1of 6

#include<stdio.

h>
#include <stdlib.h>
#include<string.h>

/*struct to store the date of birth of a student*/


struct DOB_student
{
int date;
int month;
int year;
};

typedef struct DOB_student DOB;

/*struct to store the details of the student*/


struct stud
{
char* name;
DOB dob;
int height;
float weight;

struct stud* next ; /*linked list*/


};

typedef struct stud student;

/*function to print a given struct*/


void printBack( student* head ){

if(head==NULL)
return;

printBack(head->next);
if(head->dob.date<10&&head->dob.month<10){
printf(" %s 0%d0%d%d %d %.2f \n",head->name,head->dob.date,head-
>dob.month,head->dob.year,head->height,head->weight);
}
else if(head->dob.date<10){
printf(" %s 0%d%d%d %d %.2f \n",head->name,head->dob.date,head-
>dob.month,head->dob.year,head->height,head->weight);
}else if(head->dob.month<10){
printf(" %s %d0%d%d %d %.2f \n",head->name,head->dob.date,head-
>dob.month,head->dob.year,head->height,head->weight);
}else

printf(" %s %d%d%d %d %.2f \n",head->name,head->dob.date,head-


>dob.month,head->dob.year,head->height,head->weight);

void print( student* head ){

if(head==NULL)
return;

if(head->dob.month/10 ==0){
char dp[10] = "0";
char* d = (char*)malloc(sizeof(char)*4);
char ch = '0'+head->dob.month;
/*itoa(head->dob.date,d,10);*/
d[0]=ch;
d[1]='\0';
strcat(dp,d);
printf(" %s %d%s%d %d %.2f \n",head->name,head->dob.date,dp,head-
>dob.year,head->height,head->weight);
printBack(head->next);
return;
}

printf(" %s %d%d%d %d %.2f \n",head->name,head->dob.date,head-


>dob.month,head->dob.year,head->height,head->weight);

print(head->next);

/*to compare date of birth two structs*/


int difference(DOB d1,DOB d2){

if(d1.year < d2.year)


return 1;
else if(d1.year> d2.year)
return -1;
else{

if (d1.month < d2.month)


return 1;
else if( d1.month > d2.month)
return -1;
else{

if(d1.date < d2.date)


return 1;
else if (d1.date > d2.date)
return -1;
else
return 0;

/*to swap the structs of two students*/


void swap( student* s1,student* s2){

student* temp = (student*)malloc(sizeof(student));

temp->name = s1->name;
temp->dob= s1->dob;
temp->weight= s1->weight;
temp->height= s1->height;
s1->name = s2->name;
s1->dob= s2->dob;
s1->weight= s2->weight;
s1->height= s2->height;

s2->name = temp->name;
s2->dob= temp->dob;
s2->weight= temp->weight;
s2->height= temp->height;

/*to calculate the length of linked list*/


int length(student* head){
student* cur1 = head;

int len =0 ;
while(cur1 != NULL){
len++;
cur1=cur1->next;
}

return len;
}

/*to copy data of a student to temperary file*/


student* copyData( student* temp,student* s1){

temp->name = s1->name;
temp->dob= s1->dob;
temp->weight= s1->weight;
temp->height= s1->height;

return temp;
}

/*to duplicate the data of a student to a temporary file*/


student* copy( student* s1){

student* temp = (student*)malloc(sizeof(student));

temp->name = s1->name;
temp->dob= s1->dob;
temp->weight= s1->weight;
temp->height= s1->height;

temp->next = NULL;

return temp;
}

/*to get the details of a student at a required index in the linked list*/
student* get( student* head,int index){
student* cur = (student*)malloc(sizeof(student));
cur = head;
if (index >= length(head))
return NULL;

while(index>0){
cur = cur->next;
index--;
}

return cur;

/*to partion the given list to perform sort*/


student* partL(student* head,int s,int e){

if(s>e) return NULL;

student* st = get(head,s);
student* temp = copy(st);

student* cur = st->next;


student* t = temp;
s++;

while(s<=e && t!=NULL && cur != NULL){


t->next = copy(cur);

t = t->next;
cur = cur->next;
s++;
}

return temp;
}

/*function to sort the list*/


void mergeSort(student* head,int start,int end){

if(start>=end){
return;
}
else if( end-start == 1){

if( difference(head->dob,head->next->dob)==1 )
swap(head,head->next);

return;
}

int mid = (start+end)/2;

/*partitin*/
student* left = partL(head,start,mid);
student* right = partL(head,mid+1,end);

/*recur mergeSort*/
mergeSort(left,0,length(left)-1);
mergeSort(right,0,length(right)-1);

student* Lptr = left;


student* Rptr = right;

student* cur = get(head,start);

while( Lptr!=NULL && Rptr!=NULL){

if( difference(Lptr->dob,Rptr->dob)==-1 || difference(Lptr->dob,Rptr-


>dob)==0 ){

copyData(cur,Lptr);
Lptr= Lptr->next;
cur = cur->next;
}
else if( difference(Lptr->dob,Rptr->dob)==1 ){
copyData(cur,Rptr);
Rptr= Rptr->next;
cur = cur->next;
}
}

while(Lptr!=NULL){
copyData(cur,Lptr);
Lptr= Lptr->next;
cur = cur->next;
}

while(Rptr!=NULL){
copyData(cur,Rptr);
Rptr= Rptr->next;
cur = cur->next;
}

int main(int argc, char const *argv[])


{
int n;
scanf(" %d",&n);

student* head = (student*)malloc(sizeof(student));

student* cur = head;


student* prev=NULL;

while(n>0){

student* newNode ;

if(cur==NULL){
newNode = (student*)malloc(sizeof(student));
cur = newNode;
}
if(prev!=NULL){
prev->next = newNode;
}

char* name = (char*)malloc(sizeof(char)*65);


int date;

fflush(stdin);
scanf("%s",name);
cur->name = name;

scanf("%d",&date);
(cur->dob).year = date%10000;
date=date/10000;
(cur->dob).month = date%100;
date = date/100;
(cur->dob).date = date;

scanf("%d %f",&(cur->height),&(cur->weight));

cur->next = NULL;

prev = cur;
cur = cur->next;

n--;
}
mergeSort(head,0,length(head)-1);
printBack(head);
return 0;
}

You might also like