i did the following code in array of structures. how do i modify it to linked list.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct emp
{
int id;
char name[40],password[10];
int age;
int bs;
int nop;
char pn1[10];
int ph1;
char pn2[10];
int ph2;
char pn3[10];
int ph3;
}e[10];
static int id=1000;
void add()
{
FILE *fp;
int a,m,i=0;
char pword[10],another='y';
fp=fopen("c:\\emp\\empdetails.lst","a+");
if(fp==NULL)
{
puts("Cannot open the file\n");
exit(1);
}
do
{
e[i].id=id++;
printf("\nThe ID of the employee is: %d",e[i].id);
printf("\nEnter name\n");
scanf("%s",e[i].name);
o:
printf("\nEnter the password with minimum 4 characters\n");
scanf("%s",e[i].password);
if(strlen(e[i].password)<4)
{
printf("\nPassword strength is too weak\n");
goto o;
}
else
{
do
{
printf("\nRetype the password\n");
scanf("%s",pword);
if(strcmp(e[i].password,pword)==0)
{
break;
}
else
{
printf("\nEnter the correct password\n");
m++;
}
}while(m>=1);
}
printf("\nEnter the age\n");
scanf("%d",&e[i].age);
printf("\nEnter the salary\n");
scanf("%d",&e[i].bs);
p:
printf("\nEnter the no of projects\n");
scanf("%d",&e[i].nop);
if(e[i].nop==1)
{
printf("\nEnter name of project1\n");
scanf("%s",e[i].pn1);
printf("\nEnter the no of hrs for project1\n");
scanf("%d",&e[i].ph1);
}
else if(e[i].nop==2)
{
printf("\nEnter name of project1\n");
scanf("%s",e[i].pn1);
printf("\nEnter the no of hrs for project1\n");
scanf("%d",&e[i].ph1);
printf("\nEnter name of project2\n");
scanf("%s",e[i].pn2);
printf("\nEnter the no of hrs for project2\n");
scanf("%d",&e[i].ph2);
}
else if(e[i].nop==3)
{
printf("\nEnter name of project1\n");
scanf("%s",e[i].pn1);
printf("\nEnter the no of hrs for project1\n");
scanf("%d",&e[i].ph1);
printf("\nEnter name of project2\n");
scanf("%s",e[i].pn2);
printf("\nEnter the no of hrs for project2\n");
scanf("%d",&e[i].ph2);
printf("\nEnter name of project3\n");
scanf("%s",e[i].pn3);
printf("\nEnter the no of hrs for project3\n");
scanf("%d",&e[i].ph3);
}
else
{
printf("\nEnter the no of projects not more than 3\n");
goto p;
}
fwrite(&e[i],sizeof(e[i]),1,fp);
printf("\nAdd another record \tY/N\n");
fflush(stdin);
another=getch();
i++;
}while(another=='y');
fclose(fp);
}
void display()
{
FILE *fp;
long int recsize;
int i=0;
fp=fopen("c:\\emp\\empdetails.lst","rb");
if (fp==NULL)
{
puts("Cannot open the file\n");
getch();
exit(0);
}
recsize=sizeof(e[i]);
printf("ID\tName\tPwrd\tAge\tSal\tPrjt1\tHr1\tPrjt2\tHr2\tPrjt3\tHr3\n\n");
while(fread(&e[i],sizeof(e[i]),1,fp)==1)
{
printf("%d\t%s\t%s\t%d\t%d\t%s\t%d\t%s\t%d\t%s\t%d\n",e[i].id,e[i].name,e[i].password,e[i].age,e[i].bs,e[i].pn1,e[i].ph1,e[i].pn2,e[i].ph2,e[i].pn3,e[i].ph3);
i++;
}
fclose(fp);
}
void del ()
{
FILE *fp,*ft;
int b,h,id,i=0,m=0;
long int recsize;
char another='y';
recsize=sizeof(e[i]);
do
{
i=0;
printf("\nDo u want to delete? Y/N\n");
fflush(stdin);
another=getch();
if(another=='y')
{
fp=fopen("c:\\emp\\empdetails.lst","rb+");
if(fp==NULL)
{
printf("\nFile not found\n");
}
fclose(fp);
printf("\nEnter th id of the employee to be deleted\n");
scanf("%d",&id);
ft=fopen("c:\\emp\\temp.lst","wb");
fp=fopen("c:\\emp\\empdetails.lst","rb+");
rewind(fp);
while(fread(&e[i],recsize,1,fp)==1)
{
if(e[i].id!=id)
{
fwrite(&e[i],recsize,1,ft);
}
else
{
m++;
printf("\nEmployee deleted from FILE\n");
}
i++;
if(m==0)
{
printf("\nEmployee not found\n");
}
}
fclose(fp);
fclose(ft);
remove("c:\\emp\\empdetails.lst");
rename("c:\\emp\\temp.lst","c:\\emp\\empdetails.lst");
fp=fopen("c:\\emp\\1.lst","rb+");
fclose(fp);
printf("\nDo u want to delete another record? Y/N\n");
fflush(stdin);
another=getch();
}
i++;
}while(another=='y');
}
void bonus()
{
FILE *fp;
char another;
int id,i=0;
another='y';
fp=fopen("c:\\emp\\empdetails.lst","rb+");
while(another=='y')
{
printf("\nEnter the id of the employee\n");
scanf("%d",&id);
rewind(fp);
while(fread(&e[i],sizeof(e[i]),1,fp)==1)
{
if(id==e[i].id)
{
if((e[i].ph1+e[i].ph2+e[i].ph3)>30)
{
printf("Incrementing sal by 10 percent\n");
e[i].bs=e[i].bs+e[i].bs*0.1;
}
else
{
printf("Incrementing sal by 500\n");
e[i].bs=e[i].bs+500;
}
fseek(fp,-sizeof(e[i]),SEEK_CUR);
fwrite(&e[i],sizeof(e[i]),1,fp);
break;
}
}
printf("Modify another record Y/N\n");
fflush(stdin);
another=getch();
i++;
}
fclose(fp);
}
void adprjct()
{
FILE *fp;
char another;
int id,n,i=0;
another='y';
fp=fopen("c:\\emp\\empdetails.lst","rb+");
while(another=='y')
{
printf("\nEnter the id of the employee\n");
scanf("%d",&id);
rewind(fp);
while(fread(&e[i],sizeof(e[i]),1,fp)==1)
{
if(id==e[i].id)
{
a:
printf("Enter the no of projects to be added to the employee\n");
scanf("%d",&n);
if((n==1)&&(e[i].nop<=3))
{
printf("Enter name of project2\n");
scanf("%s",e[i].pn2);
printf("Enter the no of hrs for project2\n");
scanf("%d",&e[i].ph2);
e[i].nop=e[i].nop+1;
}
else if((n==2)&&(e[i].nop<=3))
{
printf("Enter name of project2\n");
scanf("%s",e[i].pn2);
printf("Enter the no of hrs for project2\n");
scanf("%d",&e[i].ph2);
printf("Enter name of project3\n");
scanf("%s",e[i].pn3);
printf("Enter the no of hrs for project3\n");
scanf("%d",&e[i].ph3);
e[i].nop=e[i].nop+2;
}
else
{
printf("Max no of projects exceeds the limit\n");
goto a;
}
fseek(fp,-sizeof(e[i]),SEEK_CUR);
fwrite(&e[i],sizeof(e[i]),1,fp);
break;
}
}
printf("Modify record Y/N\t");
fflush(stdin);
another=getch();
i++;
}
fclose(fp);
}
int main()
{
char choice;
int a,i;
char another;
another='y';
while(1)
{
sw:
printf("\n\tEmployee Management Menu\n1.Add\n2.Delete\n3.Display\n4.Bonus\n5.Add Project\n6.Exit\n");
printf("\nEnter your choice\t");
fflush(stdin);
scanf("%c",&choice);
switch(choice)
{
case '1':
add();
break;
case '2':
del ();
break;
case '3':
display();
break;
case '4':
bonus();
break;
case '5':
adprjct();
break;
case '6':
printf("Quitting...");
getch();
exit(0);
default:
printf("\nInvalid option. Enter correct option\n");
break;
}
}
}