I have written the following program, but still have a problem understanding the code . why won't this work?
/*This program will organize a linked list of employees
Written by Elizabeth Preuss
January 2005
Adapted from classwork and handouts
Version 2.0
language c(target borland)
*/
#include <stdio.h>
#include <string.h>
typedef struct employee
{char first [21];
char last [21];
int id;
struct employee *next;
} EMPID;
EMPID* insertit(EMPID *first,char firstname[], char lastname[], int idnum);
int main (void)
{
EMPID *head,
*p;
char empfirst[21];
char emplast[21];
int idnumber,
j,
k;
char ch,
n1;
head=NULL;
for(j=0;j<21;j++)
empfirst[j]=' ';
for(k=0;k<NO_EMPID;k++)
{printf("Enter first name of employee\n");
gets(empfirst);
printf("Enter the last name of employee\n");
gets(emplast);
printf("Enter the employee id number: ");
scanf("%d", &idnumber);
scanf("%c", &n1);
head=insertit(head,empfirst, emplast, idnumber);
for(j=0;j<21;j++)
empfirst[j]=' ';
}
p=head;
while(p!=NULL)
{printf("The first name of employee is: %s ",p ->first);
printf("The last name of employee is: %s ",p->last);
printf("The employee id number is: %d\n", p->idnumber);
p=p->next;
}
printf("Hit any character to continue");
scanf("%c", &ch);
}
EMPID* insertit (EMPID *first, char firstname[], char lastname[], int idnum)
/* Function to maintain linked structure
Written by Elizabeth Preuss
January 2005
Adapted from classwork and handouts
version 2.0
language c (target borland)
*/
{
EMPID *p,
*q,
*newp;
int found,
len,
i;
found=0;
q=first;
p=first;
while ((p!=NULL) &&(!found))
{if (p->idnum<idnum)
{q=p;
p=p->next;
}
else
found=1;
}
newp=(EMPID*) malloc(size of (EMPID));
newp->first=firstname;
newp->last=lastname;
strncpy(newp->first,firstname,21);
newp->next=p;
if(q!=p)
q->next=newp;
else
first=newp;
return(first);
}