Here is my problem: Write a program that utilizes a structure and linked list to store employee information taken from the keyboard (first name, last name, three digit employee id number). The program should traverse the linked list and display the information for the employee with the highest id number.
I'm having troubles with the output and getting this to work in general. I'm not great with structures or linked lists so any help would be appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct employee
{char fname[22];
char lname[22];
int id;
struct employee*next;
}WORK;
WORK* insertit(WORK*first, char empFNAME[], char empLNAME[], int idnum);
int main (void)
{ WORK *head,
*p;
char namef[22],
namel[22];
int empID,
j,
k;
char ch,
nl;
head=NULL;
for(k=0; k<21; k++)
{ namef[k]=' ';
namel[k]=' ';
}
for(j=0; j<4; j++)
{printf("Enter the first name of the employee: ");
scanf("%s", &namef);
printf("\nEnter the last name of the employee: ");
scanf("%s", &namel);
printf("\nEnter the three digit ID number: ");
scanf("%d", &empID);
printf("\n");
head=insertit (head, namef, namel, empID);
for(k=0; k<21; k++)
namef[k]=' ';
namel[k]=' ';
}
p=head;
while (p!=NULL)
{puts(head->fname);
puts(head->lname);
printf("The ID number is: ", head->id);
p=p->next;
}
printf("Hit any character to continue");
scanf("%c", &ch);
}
WORK* insertit (WORK *first, char empFNAME[], char empLNAME[], int idnum)
{WORK *p,
*q,
*newp;
int found,
len,
i;
found=0;
q=first;
p=first;
while ((p!=NULL) && (!found))
{if (p->id<idnum)
{q=p;
p=p->next;
}
else
found=1;
}
newp=(WORK*) malloc(sizeof(WORK));
newp->id=idnum;
strncpy(newp->fname, empLNAME, 21);
newp->next=p;
if(q!=p)
q->next=newp;
else
first=newp;
return (first);
}