Sie sind auf Seite 1von 24

#include <stdio.

h>
#include <conio.h>
#include <malloc.h>
#include <string.h>
typedef struct student_record
{
char name[40];
int roll_no;
int sub_code;
int marks;
int key;
struct student_record *next;
}std_rec;
typedef struct nt1
{
std_rec *hdr1;
}list1;
typedef struct nt2
{
std_rec *hdr2;
}list2;
typedef struct nt3
{
std_rec *hdr3;
}list3;
list1 *l1;
list2 *l2;
list3 *l3;
int count_1=0;
int count_2=0;
int count_3=0;

int add_student(void);
int del_student(void);
int rec_active(void);
int rec_empty(int);
void rec_max_sub(void);
void rec_union(void);
void rec_intsec(void);
void rec_diff(void);
void rec_sym_diff(void);
void sort_list(int);
void print_list(int);
void intialize_all();
void unique(int i);

int main()
{
int i,menu,a,b,c;
intialize_all();
while(1)
{
printf("_______________________MENU___________________________");
printf("\n1.Add Student\n2.Delete Student\n3.Display Number of active rec
ords\n4.Check list if it is empty");
printf("\n5.Print records of a list \n6.Find maximum marks obtained in a
particular subject\n");
printf("7.Get union of two lists\n8.Get intersection of two lists\n9.Get
differece between two lists\n");
printf("10.Get symmetric difference of two lists\n11.To remove duplicates
from the list\n12.Sort List\n0.EXIT\n");
printf("______________________________________________________");
printf("\nYOUR CHOICE: ");
scanf("%d",&i);
printf("______________________________________________________");
if(i==0) break;
switch(i)
{
case 1:add_student();
break;
case 2:del_student();
break;
case 3:rec_active();
break;
case 4:printf("\nEnter the List number: ");
scanf("%d",&b);
c=rec_empty(b);
if(c)
{
printf("\nList is Empty\n");
}
else
{
printf("\nList is not Empty\n");
}
break;
case 5:printf("\nEnter the list no: ");
scanf("%d",&a);
printf("Details of students in this list:\n");
print_list(a);
break;
case 6:rec_max_sub();
break;
case 7:rec_union();
break;
case 8:rec_intsec();
break;
case 9:rec_diff();
break;
case 10:rec_sym_diff();
break;
case 11:printf("\nEnter the list no: ");
scanf("%d",&a);
printf("Removing Duplicates...");
unique(a);
printf("\nDuplicates Removed use print list to print the resul
tant list\n");
break;
case 12:printf("\nEnter the list no: ");
scanf("%d",&a);
printf("\nSorted use Print List to print the sorted list\n");
sort_list(a);
break;
default: printf("Error not a valid choice\n");
}
}
return 1;
}

void intialize_all()
{
l1=(list1 *)malloc(sizeof(list1));
l2=(list2 *)malloc(sizeof(list2));
l3=(list3 *)malloc(sizeof(list3));
l1->hdr1=NULL;
l2->hdr2=NULL;
l3->hdr3=NULL;
}

int rec_empty(int a)/*returns 1 if the list is empty or returns 0 if not*/


{
switch(a)
{
case 1:if(l1->hdr1==NULL)
{
return 1;
}
else
{
return 0;
}
break;
case 2:if(l2->hdr2==NULL)
{
return 1;
}
else
{
return 0;
}
break;
case 3:if(l3->hdr3==NULL)
{
return 1;
}
else
{
return 0;
}
break;
}
}

int add_student()/*Adds or updates a student record*/


{
int i,z,j;
int a,b,c;
char temp[40];
std_rec *p,*q,*t;
printf("\nEnter 1 to add a student or 2 to update a student record: ");
scanf("%d",&j);
printf("\nEnter the list number: ");
scanf("%d",&i);
p=(std_rec *)malloc(sizeof(std_rec));
fflush(stdin);
printf("\nEnter the name of the student: ");
fgets(temp,39,stdin);
temp[strlen(temp)-1]='\0';
printf("Enter the roll no. of the student: ");
scanf("%d",&a);
printf("Enter the subject code of the student: ");
scanf("%d",&b);
printf("Enter the marks obtained by the student: ");
scanf("%d",&c);
strcpy(p->name,temp);
p->roll_no=a;
p->sub_code=b;
p->marks=c;
p->key= p->roll_no*100 + p->sub_code;
if (i==1)
{
if(rec_empty(1))/*list is empty so the record is inserted into h
eader of the linked list*/
{
count_1=count_1+1;
l1->hdr1=p;
l1->hdr1->next=NULL;
printf("\nAdded a Student record\n");
return 1;
}
else /* list is not empty so q is the pointer which takes to the
last element or the element which needs
to be updated or to be added */
{
q=l1->hdr1;
if(j==2)
{
while((q->key!=p->key) && (q->ne
xt!=NULL))
{
q=q->next;
}
if (q->key==p->key)/*updating th
e record*/
{
strcpy(q->name,p->name);
q->roll_no=p->roll_no;
q->sub_code=p->sub_code;
q->marks=p->marks;
printf("\nUpdated a Student record\n");
free(p);
return 1;
}
}
else/*adding a record*/
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
p->next=NULL;
count_1=count_1+1;
printf("\nAdded a Student record\n");
return 1;
}
}
}
else if (i==2)/*list is empty so the record is inserted into header of t
he linked list*/
{
if(rec_empty(2))
{
count_2=count_2+1;
l2->hdr2=p;
l2->hdr2->next=NULL;
printf("\nAdded a Student record\n");
}
else
{
q=l2->hdr2;
if(j==2)
{
while((q->key!=p->key) && (q->ne
xt!=NULL))
{
q=q->next;
}
if (q->key==p->key)/*updating th
e record*/
{
strcpy(q->name,p->name);
q->roll_no=p->roll_no;
q->sub_code=p->sub_code;
q->marks=p->marks;
printf("\nUpdated a Student record\n");
free(p);
return 1;
}
}
else/*adding a record*/
{
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
p->next=NULL;
count_2=count_2+1;
printf("\nAdded a Student record\n");
return 1;
}
}
}
else
{
printf("invalid list number");
}
}

int del_student()/*deletes a particular record*/


{
int i;
int r,s;
std_rec *p,*q,*pre;
p=(std_rec *)malloc(sizeof(std_rec));
printf("\nEnter the list number in which you want to delete a studentrec
ord: ");
scanf("%d",&i);
if (i==1)/*for deleting a student in list number 1*/
{
if(rec_empty(1))
{
printf("\nlist is empty\n");
return 0;
free(p);
}
else
{
printf("Enter the roll no. of the studen
t: ");
scanf("%d",&r);
p->roll_no=r;
printf("Enter the subject code of the student: ");
scanf("%d",&s);
p->sub_code=s;
p->key = p->roll_no*100 + p->sub_code;
q=l1->hdr1;
while( !(q->key==p->key) && (q->next!=NU
LL))
{
pre=q;
q=q->next;
}
if((l1->hdr1->next==NULL)&&(l1->hdr1->ke
y==p->key))
{
count_1=count_1-1;
printf("\nSTUDENT DELETED\n");
free(l1->hdr1);
l1->hdr1=NULL;
free(p);
return 1;
}
else
{
if(q->key==p->key)
{
count_1=count_1-1;
printf("\nSTUDENT DELETED\n");
pre->next=q->next;
free(q);
q=NULL;
free(p);
return 1;
}
else if (q->next==NULL)
{
printf("\nEntry Not Foun
d\n");
free(p);
return 0;
}
}
}
}
else if (i==2)/*for deleting a record in list number 2*/
{
if(rec_empty(2))
{
printf("\nlist is empty\n");
return 0;
free(p);
}
else
{
printf("Enter the roll no. of the studen
t: ");
scanf("%d",&r);
p->roll_no=r;
printf("Enter the subject code of the student: ");
scanf("%d",&s);
p->sub_code=s;
p->key = p->roll_no*100 + p->sub_code;
q=l2->hdr2;
while( !(q->key==p->key) && (q->next!=NU
LL))
{
pre=q;
q=q->next;
}
if((l2->hdr2->next==NULL)&&(l2->hdr2->ke
y==p->key))
{
count_2=count_2-1;
printf("\nSTUDENT DELETED\n");
free(l2->hdr2);
l2->hdr2=NULL;
free(p);
return 1;
}
else
{
if(q->key==p->key)
{
count_2=count_2-1;
printf("\nSTUDENT DELETED\n");
pre->next=q->next;
free(q);
free(p);
q=NULL;
return 1;
}
else if (q->next==NULL)
{
printf("\nEntry Not Foun
d\n");
return 0;
}
}
}
}
else
{
printf("invalid list number");
free(p);
}
}

int rec_active(void)/*returns numbers of active records present in the input lis


t*/
{
int i;
printf("\nEnter the list in which you want to find number of actve recor
ds :");
scanf("%d",&i);
if(i==1)/*For list number 1*/
{
if(rec_empty(1))
{
printf("\nList is empty\n");
}
else
{
printf("\nNumber of active records : %d\n",count_1);
}
}
else if (i==2)/*For list number 2*/
{
if(rec_empty(2))
{
printf("\nList is empty\n");
}
else
{
printf("\nNumber of active records : %d\n",count_2);
}
}
else if (i==3)/*For list number 3*/
{
if(rec_empty(3))
{
printf("\nList is empty\n");
}
else
{
printf("\nNumber of active records : %d\n",count_3);
}
}
else
{
printf("Invalid list number");
}
}

void rec_max_sub(void)
{
int i,a,max;
std_rec *p,*q,*pre,*t;
p=(std_rec *)malloc(sizeof(std_rec));
printf("\nEnter the list in which you want to find the person who got ma
ximum marks : ");
scanf("%d",&i);
if(i==1)/*For list number 1*/
{
if(rec_empty(1))
{
printf("\nlist is empty\n");
}
else
{
printf("\nEnter the subject code :");
scanf("%d",&a);
p->sub_code=a;
p->marks=0;
q=l1->hdr1;
while(q!=NULL)
{
if((q->marks)>(p->marks) && (q->sub_code==a))
{
p->marks=q->marks;
t=q;
}
q=q->next;
}
printf("\nMaximum Marks : %d",t->marks);
printf("\nRoll No :%d",t->roll_no);
printf("\nName : %s",t->name);
printf("\nSubject Code :%d\n",t->sub_code);
}
}
else if(i==2)/*For list number 2*/
{
if(rec_empty(2))
{
printf("\nlist is empty\n");
}
else
{
printf("\nEnter the subject code\n");
scanf("%d",&a);
p->sub_code=a;
p->marks=0;
q=l2->hdr2;
while((q->marks)>(p->marks) && (q->sub_code==a))
{
if(q->marks>p->marks)
{
p->marks=q->marks;
t=q;
}
q=q->next;
}
printf("\nMaximum Marks : %d",t->marks);
printf("\nRoll No :%d",t->roll_no);
printf("\nName : %s",t->name);
printf("\nSubject Code :%d\n",t->sub_code);
}
}
else
{
printf("invalid list number");
}
}
void rec_union(void)
{
int check;
std_rec *p,*q,*z,*t,*m;
if(l3->hdr3==NULL) ;
else
{
std_rec *tem,*prev;
tem=l3->hdr3;
while(tem->next!=NULL)
{
prev=tem;
tem=tem->next;
free(prev);
}
l3->hdr3=NULL;
}
printf("\nUNION DONE\n");
if(rec_empty(1) && rec_empty(2))
{
printf("\nboth the list are empty\n");
}
else if(rec_empty(1))/*list 1 is empty so adding all the records in list
2 to list 3*/
{
z=l2->hdr2;
while(z!=NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,z->name);
p->roll_no=z->roll_no;
p->sub_code=z->sub_code;
p->marks=z->marks;
p->key=z->key;
if(l3->hdr3==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
m->next->next=NULL;
count_3=count_3+1;
}
z=z->next;
}
}
else if(rec_empty(2))/*list 2 is empty so adding all the records in list
1 to list 3*/
{
z=l1->hdr1;
while(z!=NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,z->name);
p->roll_no=z->roll_no;
p->sub_code=z->sub_code;
p->marks=z->marks;
p->key=z->key;
if(l3->hdr3==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
m->next->next=NULL;
count_3=count_3+1;
}
z=z->next;
}
}
else/*none of the lists are empty so putting (A U B) into list 3*/
{
q=l1->hdr1;
z=l2->hdr2;
t=z;
while(q!=NULL)
{
z=l2->hdr2;
check=1;
while(z!=NULL)
{
if(z->key==q->key)
{
check=0;
}
z=z->next;
}
if(check==1 && z==NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,q->name);
p->roll_no=q->roll_no;
p->sub_code=q->sub_code;
p->marks=q->marks;
p->key=q->key;
}
if((l3->hdr3==NULL && check==1) && z==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else if((l3->hdr3!=NULL && check==1) && z==NULL)
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
p->next=NULL;
count_3=count_3+1;
}
q=q->next;
}
z=t;
while(z!=NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,z->name);
p->roll_no=z->roll_no;
p->sub_code=z->sub_code;
p->marks=z->marks;
p->key=z->key;
if(l3->hdr3==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
if(m->next==NULL)
{
m->next=p;
m->next->next=NULL;
count_3=count_3+1;
}
}
z=z->next;
}
p=NULL;
free(p);
}
printf("\n");
sort_list(3);
printf("\n");
print_list(3);
printf("\n");
}

void print_list(int a)
{
std_rec *q;
printf("\n");
printf("Key\t");
printf("Name\t");
printf("Roll No\t");
printf("Subject Code\t");
printf("Marks Obtained\t");
printf("\n");
switch(a)
{
case 1:q=l1->hdr1;
while(q!=NULL)
{
printf("\n");
printf("%d\t",q->sub_code*100+q->roll_no);
printf("%s\t",q->name);
printf("%d\t",q->roll_no);
printf("%d\t\t",q->sub_code);
printf("%d\t",q->marks);
printf("\n");
q=q->next;
}
break;
case 2:q=l2->hdr2;
while(q!=NULL)
{
printf("\n");
printf("%d\t",q->sub_code*100+q->roll_no);
printf("%s\t",q->name);
printf("%d\t",q->roll_no);
printf("%d\t\t",q->sub_code);
printf("%d\t",q->marks);
printf("\n");
q=q->next;
}
break;
case 3:q=l3->hdr3;
while(q!=NULL)
{
printf("\n");
printf("%d\t",q->sub_code*100+q->roll_no);
printf("%s\t",q->name);
printf("%d\t",q->roll_no);
printf("%d\t\t",q->sub_code);
printf("%d\t",q->marks);
printf("\n");
q=q->next;
}
break;
}
}
void unique(int i)
{
int check,a;
std_rec *p,*q,*t,*pre;
if(i==1)
{
if(rec_empty(1))
{
printf("\nList is empty\n");
}
else
{
q=l1->hdr1;
pre=q;
while(q->next!=NULL)
{
p=q->next;
while(p!=NULL)
{
if(p->key==q->key)
{
pre->next=p->next;
count_1=count_1-1;
p=NULL;
break;
}
if(p!=NULL)
{
pre=p;
p=p->next;
}
}
q=q->next;
}
}
}
else if(i==2)
{
if(rec_empty(2))
{
printf("\nList is empty\n");
}
else
{
q=l2->hdr2;
pre=q;
while(q->next!=NULL)
{
p=q->next;
while(p!=NULL)
{
if(p->key==q->key)
{
pre->next=p->next;
count_2=count_2-1;
p=NULL;
break;
}
if(p!=NULL)
{
pre=p;
p=p->next;
}
}
q=q->next;
}
}
}
else if(i==3)
{
if(rec_empty(1))
{
printf("\nList is empty\n");
}
else
{
q=l3->hdr3;
pre=q;
while(q->next!=NULL)
{
p=q->next;
while(p!=NULL)
{
if(p->key==q->key)
{
pre->next=p->next;
count_3=count_3-1;
p=NULL;
break;
}
if(p!=NULL)
{
pre=p;
p=p->next;
}
}
q=q->next;
}
}
}
else
{
printf("\nInvalid List Number\n");
}
}

void rec_intsec(void)
{
int check;
std_rec *p,*q,*z,*t,*m;
if(l3->hdr3==NULL) ;
else
{
std_rec *tem,*prev;
tem=l3->hdr3;
while(tem->next!=NULL)
{
prev=tem;
tem=tem->next;
free(prev);
}
l3->hdr3=NULL;
}
printf("\nIntersection DONE\n");
if(rec_empty(1) && rec_empty(2))
{
printf("\nboth the list are empty\n");
}
else if(rec_empty(1))
{
printf("\nlist1 is empty\n");
}
else if(rec_empty(2))
{
printf("\nlist2 is empty\n");
}
else/*when none of the lists are empty*/
{
q=l1->hdr1;
z=l2->hdr2;
t=z;
while(q!=NULL)
{
z=t;
check=0;
while(z!=NULL)
{
if(z->key==q->key)
{
check=1;
break;
}
z=z->next;
}
if(check==1 && z!=NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,q->name);
p->roll_no=q->roll_no;
p->sub_code=q->sub_code;
p->marks=q->marks;
p->key=q->key;
}
if((l3->hdr3==NULL && check==1) && z!=NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else if((l3->hdr3!=NULL && check==1) && z!=NULL)
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
p->next=NULL;
count_3=count_3+1;
}
q=q->next;
}
p=NULL;
free(p);
}
printf("\n");
sort_list(3);
printf("\n");
print_list(3);
printf("\n");
}
void rec_diff(void)
{
int check;
std_rec *p,*q,*z,*t,*m;
if(l3->hdr3==NULL) ;
else
{
std_rec *tem,*prev;
tem=l3->hdr3;
while(tem->next!=NULL)
{
prev=tem;
tem=tem->next;
free(prev);
}
l3->hdr3=NULL;
}
printf("\nDifference DONE\n");
if(rec_empty(1) && rec_empty(2))
{
printf("\nboth the list are empty\n");
}
else if(rec_empty(1))
{
printf("\nlist1 is empty\n");
}
else if(rec_empty(2))/*list 2 is empty so adding all the records in list
1 to list 3*/
{
z=l1->hdr1;
while(z!=NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,z->name);
p->roll_no=z->roll_no;
p->sub_code=z->sub_code;
p->marks=z->marks;
p->key=z->key;
if(l3->hdr3==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
m->next->next=NULL;
count_3=count_3+1;
}
z=z->next;
}
}
else/*when none of the lists are empty*/
{
q=l1->hdr1;
z=l2->hdr2;
t=z;
while(q!=NULL)
{
z=t;
check=1;
while(z!=NULL)
{
if(z->key==q->key)
{
check=0;
}
z=z->next;
}
if(check==1 && z==NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,q->name);
p->roll_no=q->roll_no;
p->sub_code=q->sub_code;
p->marks=q->marks;
p->key=q->key;
}
if((l3->hdr3==NULL && check==1) && z==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else if((l3->hdr3!=NULL && check==1) && z==NULL)
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
p->next=NULL;
count_3=count_3+1;
}
q=q->next;
}
p=NULL;
free(p);
}
printf("\n");
sort_list(3);
printf("\n");
print_list(3);
printf("\n");
}
void rec_sym_diff(void)
{
int check;
std_rec *p,*q,*z,*t,*m;
if(l3->hdr3==NULL) ;
else
{
std_rec *tem,*prev;
tem=l3->hdr3;
while(tem->next!=NULL)
{
prev=tem;
tem=tem->next;
free(prev);
}
l3->hdr3=NULL;
}
printf("\nSymetric Difference DONE\n");
if(rec_empty(1) && rec_empty(2))
{
printf("\nboth the list are empty\n");
}
else if(rec_empty(1))/*list 1 is empty so adding all the records in list
2*/
{
z=l2->hdr2;
while(z!=NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,z->name);
p->roll_no=z->roll_no;
p->sub_code=z->sub_code;
p->marks=z->marks;
p->key=z->key;
if(l3->hdr3==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
m->next->next=NULL;
count_3=count_3+1;
}
z=z->next;
}
}
else if(rec_empty(2))/*list 2 is empty so adding all the records in list
1*/
{
z=l1->hdr1;
while(z!=NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,z->name);
p->roll_no=z->roll_no;
p->sub_code=z->sub_code;
p->marks=z->marks;
p->key=z->key;
if(l3->hdr3==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
m->next->next=NULL;
count_3=count_3+1;
}
z=z->next;
}
}
else/*none of the lists are empty so adding first (A-B) then adding (B-A
)*/
{
q=l1->hdr1;
z=l2->hdr2;
t=z;
while(q!=NULL)
{
z=t;
check=1;
while(z!=NULL)
{
if(z->key==q->key)
{
check=0;
}
z=z->next;
}
if(check==1 && z==NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,q->name);
p->roll_no=q->roll_no;
p->sub_code=q->sub_code;
p->marks=q->marks;
p->key=q->key;
}
if((l3->hdr3==NULL && check==1) && z==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else if((l3->hdr3!=NULL && check==1) && z==NULL)
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
p->next=NULL;
count_3=count_3+1;
}
q=q->next;
}
z=l1->hdr1;
q=l2->hdr2;
t=z;
while(q!=NULL)
{
z=t;
check=1;
while(z!=NULL)
{
if(z->key==q->key)
{
check=0;
}
z=z->next;
}
if(check==1 && z==NULL)
{
p=(std_rec *)malloc(sizeof(std_rec));
strcpy(p->name,q->name);
p->roll_no=q->roll_no;
p->sub_code=q->sub_code;
p->marks=q->marks;
p->key=q->key;
}
if((l3->hdr3==NULL && check==1) && z==NULL)
{
l3->hdr3=p;
l3->hdr3->next=NULL;
count_3=count_3+1;
}
else if((l3->hdr3!=NULL && check==1) && z==NULL)
{
m=l3->hdr3;
while(m->next!=NULL)
{
m=m->next;
}
m->next=p;
p->next=NULL;
count_3=count_3+1;
}
q=q->next;
}
}
printf("\n");
sort_list(3);
printf("\n");
print_list(3);
printf("\n");
}
void sort_list(int i)/* sorting on key where key is rollno*100 + subcode*/
{
std_rec *p,*r,*sorted,*prev;
if(i==1)
{
sorted=l1->hdr1;
p=sorted->next;
while(p!=NULL)
{
r=l1->hdr1;
prev=NULL;
while(r->key<p->key)
{
prev=r;
r=r->next;
}
if(prev==NULL)
{
sorted->next=p->next;
p->next=l1->hdr1;
l1->hdr1=p;
}
else if(r==p)
{
sorted=p;
}
else
{
sorted->next=p->next;
p->next=r;
prev->next=p;
}
p=sorted->next;
}
}
else if(i==2)
{
sorted=l2->hdr2;
p=sorted->next;
while(p!=NULL)
{
r=l2->hdr2;
prev=NULL;
while(r->key<p->key)
{
prev=r;
r=r->next;
}
if(prev==NULL)
{
sorted->next=p->next;
p->next=l2->hdr2;
l2->hdr2=p;
}
else if(r==p)
{
sorted=p;
}
else
{
sorted->next=p->next;
p->next=r;
prev->next=p;
}
p=sorted->next;
}
}
else if(i==3)
{
sorted=l3->hdr3;
p=sorted->next;
while(p!=NULL)
{
r=l3->hdr3;
prev=NULL;
while(r->key<p->key)
{
prev=r;
r=r->next;
}
if(prev==NULL)
{
sorted->next=p->next;
p->next=l3->hdr3;
l3->hdr3=p;
}
else if(r==p)
{
sorted=p;
}
else
{
sorted->next=p->next;
p->next=r;
prev->next=p;
}
p=sorted->next;
}
}
}

Das könnte Ihnen auch gefallen