#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct employee
{
int id;
float wage;
struct employee *next;
};
//***********************************************************************
// Function: print_list
//
// Desription: 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 (o or false)
for(tmp = emp1; tmp; tmp = tmp->next)
{
i++;
printf("\nEmployee ID: %6i, Wages: %8.2f\n", tmp
->id
, tmp
->wage
); }
printf("\n\nTotal Number of Employees = %d\n", i
);
}
//**************************************************************************
// 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
//
// Output: None
//
// Calls: print_list
//
//**************************************************************************
int main ()
{
char answer[80]; // holds user reply to prompt
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);
printf("\n\nEnd of program\n");
return (0);
}