/*Program that utilizes a structure and linked list to store*/
/*employee information taken from the keyboard*/
/*Programmer: Class: C*/
/*Instructor: Date: April 23rd, 2007*/
/*Language: C (gcc target)
#include <stdio.h> /*Library to be included*/
#include <stdlib.h> /*Library to be included*/
#include <string.h> /*Library to be included */
#define NO_EMPLOYEES 3/* Number of employees entered */
#define MAXLENGTH 21 /* the maximum length of the names */
/*Define Structure for employee*/
typedef struct employee
{
char firstname[MAXLENGTH];/*Max length of first name*/
char lastname[MAXLENGTH];/*Max length of last name*/
int id; /*ID declared as int*/
struct employee *next;
}
empl;
empl* insert(empl *first, empl *new);
*/Main declared*/
int main(int argc, char **argv)
{
empl *head, *temp;
int i;
head=NULL;
*/ Information will be entered here using keyboard*/
*/ It will loop 3 times */
for(i=0; i<NO_EMPLOYEES; i++){
temp = (empl*)calloc(1, sizeof(empl));
printf("Please enter the first name, the last name and the user id:\n");
scanf("%s %s %d", temp->firstname, temp->lastname, &temp->id);
temp->next = NULL;
head = insert(head, temp);
}
/* All data will printed out here that was entered*/
/*Lowest to highest ID number*/
printf("The employee with the lowest id number is\n");
printf("First name: %s\n", head->firstname);
printf("Last name: %s\n", head->lastname);
printf("User ID: %d\n", head->id);
temp = head;
printf("Here's a list with all employees\n\n");
while(temp != NULL){
printf("First name: %s\n", temp->firstname);
printf("Last name: %s\n", temp->lastname);
printf("User ID: %d\n", temp->id);
temp = temp->next;
}
return 0;
}
empl* insert(empl *first, empl *new)
{
empl *cur, *prev;
if(first == NULL)
{
return new;
}
cur = first;
prev = first;
while(cur != NULL)
{
if(cur->id < new->id)
{
prev = cur;
cur = cur->next;
}
else
{
break;
}
}
if(first==cur)
{
first = new;
new->next = cur;
}
else
{
prev->next = new;
new->next = cur;
}
return first;
}