#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/* types */
struct employee
{
int id;
float wage;
struct employee *next;
};
/* function prototypes */
struct employee * findEntry (struct employee * list_ptr, int id_number);
void print_list (struct employee *emp1);
/*-----------------------------------------------------------
**
** FUNCTION: main
**
** DESCRIPTION: This function will prompt the user for an
** employee id and wage until the user indicates they are
** finished. At that point, a list of id and wages will be
** generated.
**
** PARAMETERS: None
**
** OUTPUTS: None
**
** CALLS: print_list
**
**-----------------------------------------------------------*/
int main ()
{
char answer[80]; /* holds user reply to prompt */
int id_value; /* ID to search for */
int more_data = 1; /* flag to continue processing */
char value; /* holds first character of user reply */
struct employee *current_ptr; /* pointer to current node*/
struct employee *head_ptr; /* points to first node */
/* Set up storage for first node */
head_ptr
= (struct employee
*) malloc (sizeof(struct employee
)); current_ptr = head_ptr;
while (more_data)
{
/* Read in Employee ID and Hourly Wage */
printf("\n\nEnter employee ID: "); scanf("%i", & current_ptr
-> id
);
printf("\nEnter employee weekly wage: "); scanf("%f", & current_ptr
-> wage
);
printf("\n\nWould you like to add another employee? (y/n): ");
/* add another employee? */
if ((value
= toupper(answer
[0])) != 'Y') {
current_ptr->next = (struct employee *) NULL;
more_data = 0;
}
else
{
/* create new node, set next pointer to it */
current_ptr
->next
= (struct employee
*) malloc (sizeof(struct employee
));
/* point to the new node */
current_ptr = current_ptr->next;
}
} /* while */
/* print out current contents of linked list */
print_list(head_ptr);
/* Enter an ID to search for in our list */
printf ("Enter an ID to search for in our list: ");
/* Search for it */
if (findEntry (head_ptr, id_value) != (struct employee *) NULL)
{
printf ("\n\nID is in the list \n"); }
else /* NULL Ptr returned */
{
printf ("\n\nID is NOT in the list \n"); }
printf("\n\nEnd of program\n");
return (0);
}
/***********************************************************************************
**
** Function: findEntry
**
** Description: Using list_ptr as a starting point, examine every entry in
** the list until an entry is found that has an id_number which
** matches the one passed to the function. If found, return a pointer
** to the node, otherwise, return NULL.
**
** Parameters:
**
** list_ptr - pointer to the beginning of the link list
** id_number - employee identifier to search
**
** Returns: Pointer to linked list node with the ID (otherwise NULL returned)
**
************************************************************************************/
struct employee * findEntry (struct employee * list_ptr, int id_number)
{
struct employee * found_it_ptr = list_ptr; /* set to beginning of list */
/* search through linked list, return a pointer to the node if the ID found in it */
for ( ; found_it_ptr; found_it_ptr = found_it_ptr->next)
{
if ( found_it_ptr -> id == id_number )
return ( found_it_ptr ); /* found */
}
return ( (struct employee *) NULL ); /* not found, return NULL pointer */
} /* find entry */
/*---------------------------------------------------------------
**
** FUNCTION: print_list
**
** DESCRIPTION: This function will print the contents of a linked
** list. It will traverse the list from beginning to the
** end, printing the contents at each node.
**
** PARAMETERS: emp1 - pointer to a linked list
**
** OUTPUTS: None
**
** CALLS: None
**
**---------------------------------------------------------------*/
void print_list (struct employee *emp1)
{
struct employee *tmp; /* tmp pointer */
int i = 0; /* counts the nodes printed */
/* Start a beginning of list and print out each value */
/* loop until tmp points to null (0 or false) */
for(tmp = emp1; tmp ; tmp = tmp->next)
{
i++;
printf("\nEmployee ID: %6i, Wage: %8.2f\n",tmp
->id
, tmp
->wage
);
}
printf("\n\nTotal Number of Employees = %d\n", i
);
}