My second assignment about Linked Lists

Hey there,

I have wrote code for my second assignment. There is txt file with employe department code number name and age. I need to read data from txt file and create linked list in order, calculate each department average age and show persons who has greater age than average. There is no error but program is terminating in the middle. Could anyone tell me what is wrong. I am new so On code there could be many little mistakes or long ways...


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


#define N 3

struct employeeInfo
{
char deptCode[3];
char empNr[5];
char DE[8];
char firstName[13];
char lastName[13];
int age;
};

struct node
{
struct employeeInfo info;
struct node *link;
};

typedef struct node *NODEPTR;



NODEPTR insert(NODEPTR *, struct employeeInfo );
void dlt (NODEPTR *, char);
NODEPTR getnode(void);
float avr (NODEPTR);
void average(NODEPTR);
void menu (void);
float avr(NODEPTR);


int main()
{
NODEPTR headdept[N],head0,head1,head2;
struct employeeInfo one;
char opcode;
int i, j=0, flag0=0,flag1=0,flag2=0,r;
float s;

do
{

menu();
scanf("%c",&opcode);

if (opcode=='1')
{
FILE *employee=fopen ("employee.txt","r");
if(employee == NULL) printf("File Error\n");
else
do{
fscanf(employee,"%s %s %s %s %d"
,&one.deptCode,&one.empNr,&one.firstName,&one.lastName,&one.age);

strcpy(one.DE,"");
strcat(one.DE,one.deptCode);
strcat(one.DE,one.empNr);

if (strcmp(one.deptCode,"AA")==0)
{
head0=insert(headdept+0,one);
flag0=1;}
if (strcmp(one.deptCode,"AB")==0)
{
head1=insert(headdept+1,one);
flag1=1;}
else
{
head2=insert(headdept+2,one);
flag2=1;}
r=flag0*flag1*flag2;
}while(r!=1);

for(i=0;i<150;i++)
{
fscanf(employee,"%s %s %s %s %d"
,&one.deptCode,&one.empNr,&one.firstName,&one.lastName,&one.age);

strcpy(one.DE,"");
strcat(one.DE,one.deptCode);
strcat(one.DE,one.empNr);

if (strcmp(one.deptCode,"AA")==0)
insert(&head0->link,one);
if (strcmp(one.deptCode,"AB")==0)
insert(&head1->link,one);
else
insert(&head2->link,one);
}


for(*headdept[0]=*head0;headdept[0]!=NULL;headdept[0]=headdept[0]->link)
printf("%s %s %s %s %d\n"
,headdept[0]->info.deptCode,headdept[0]->info.empNr,headdept[0]->info.firstName,headdept[0]->info.lastName,headdept[0]->info.age);

for(*headdept[1]=*head1;headdept[1]!=NULL;headdept[1]=headdept[1]->link)
printf("%s %s %s %s %d\n"
,headdept[1]->info.deptCode,headdept[1]->info.empNr,headdept[1]->info.firstName,headdept[1]->info.lastName,headdept[1]->info.age);

for(*headdept[2]=*head2;headdept[2]!=NULL;headdept[2]=headdept[2]->link)
printf("%s %s %s %s %d\n"
,headdept[2]->info.deptCode,headdept[2]->info.empNr,headdept[2]->info.firstName,headdept[2]->info.lastName,headdept[2]->info.age);


}


if (opcode=='2')
{
average(headdept[0]);
average(headdept[1]);
average(headdept[2]);


}

if (opcode=='3')
{
s=(avr(headdept[0])+avr(headdept[1])+avr(headdept[2]))/3;

}

if (opcode=='4')
{
printf("Enter new employee department code employe nr, first name, last name and age:\n");
scanf("%s %s %s %s %d",&one.deptCode,&one.empNr,&one.firstName,&one.lastName,&one.age);

strcpy(one.DE,"");
strcat(one.DE,one.deptCode);
strcat(one.DE,one.empNr);

if (strcmp(one.deptCode,"AA")==0)
insert(&head0,one);
if (strcmp(one.deptCode,"AB")==0)
insert(&head1,one);
else
insert(&head2,one);

}


if (opcode=='5')
{
printf("Enter departman code and employee number for deletion");
scanf("%s %s",one.deptCode, one.empNr);

strcpy(one.DE,"");
strcat(one.DE,one.deptCode);
strcat(one.DE,one.empNr);

//if (strcmp(one.deptCode,"AA")==0)
// dlt(&head0,one.DE);
//if (strcmp(one.deptCode,"AB")==0)
// dlt(&head1,one.DE);
//else
// dlt(&head2,one.DE);

}

// else printf("WRONG OPERATION CODE\n");

}
while(opcode!='9');
return 0;
}


void menu (void)
{
printf ("1. Batch Operation and Insertion sort process and List\n");
printf ("2. Traversing1\n");
printf ("3. Traversing2\n");
printf ("4. New Employee Insertion \n");
printf ("5. Search and Delete \n");
}


NODEPTR getnode(void)
{
NODEPTR p;
p = (NODEPTR) malloc(sizeof(struct node));
return p;
}

//NODEPTR insert(NODEPTR *np,struct employeeInfo x)
//{
// (*np)=getnode ();
// (*np)->info=x;
// (*np)->link=NULL;
//
// return (*np);
//}

NODEPTR insert(NODEPTR *head, struct employeeInfo x)
{
NODEPTR p,q,save;
q= NULL;
for(p=*head; p!=NULL && x.empNr>p->info.empNr; p=p->link)
q = p;

if (q == NULL) /* insert x at the head of the list */
{
p=getnode();
p->info=x;
p->link = *head;
*head=p;
}
else /* inserts after q */
{
save=p;
p=getnode();
p->info=x;
q->link=p;
p->link=save;
}
return *head;
}

void average(NODEPTR ps)
{
float avrg, sum=0, cnt=0;
NODEPTR save;

save=ps;
while(save!=NULL)
{
sum+=save->info.age;
save=save->link;
cnt++;
}
avrg=sum/cnt;
while(save!=NULL)
{
if (save->info.age>avrg)
{
printf("%s %s %s %s %d",save->info.deptCode,save->info.empNr,save->info.firstName,save->info.lastName,save->info.age);
save=save->link;
}
}
}

float avr(NODEPTR ps)
{
float avrg, sum=0, cnt=0;
NODEPTR save;

save=ps;
while(save!=NULL)
{
sum+=save->info.age;
save=save->link;
cnt++;
}
avrg=sum/cnt;
return avrg;
}

void dlt (NODEPTR *plist, char DE[])
{
NODEPTR p,q,save;

q=NULL;
p=*plist;

while(p!=NULL)
if (strcmp(p->info.DE,DE)==0)
{
save=p;
p=p->link;
if (q==NULL){
free(*plist);
*plist=p;}
else {
free(save);
q->link=p;
}
}
else {
q=p;
p=p->link;
}
}
Can you please format your code. It's impossible to read otherwise.
Topic archived. No new replies allowed.