fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. struct employee
  6. {
  7. int id;
  8. float wage;
  9. struct employee *next;
  10. };
  11.  
  12. //***********************************************************************
  13. // Function: print_list
  14. //
  15. // Desription: This function will print the contents of a linked list.
  16. // It will traverse the list from beginning to the end, printing the
  17. // contents at each node.
  18. //
  19. // Parameters: emp1 - pointer to a linked list
  20. //
  21. // Outputs: none
  22. //
  23. // Calls: none
  24. //
  25. //************************************************************************
  26.  
  27. void print_list (struct employee *emp1)
  28. {
  29. struct employee *tmp; // tmp pointer
  30. int i = 0; // counts the nodes printed
  31.  
  32. // Start a beginning of list and print out each value
  33. // loop until tmp points to null (o or false)
  34.  
  35. for(tmp = emp1; tmp; tmp = tmp->next)
  36. {
  37. i++;
  38. printf("\nEmployee ID: %6i, Wages: %8.2f\n", tmp->id, tmp->wage);
  39. }
  40.  
  41. printf("\n\nTotal Number of Employees = %d\n", i);
  42.  
  43. }
  44.  
  45. //**************************************************************************
  46. // Function: main
  47. //
  48. // Description: This function will prompt the user for an employee id and
  49. // wage until the user indicates they are finished. At that point, a list
  50. // of id and wages will be generated.
  51. //
  52. // Parameters: None
  53. //
  54. // Output: None
  55. //
  56. // Calls: print_list
  57. //
  58. //**************************************************************************
  59.  
  60. int main ()
  61. {
  62. char answer[80]; // holds user reply to prompt
  63. int more_data = 1; // flag to continue processing
  64. char value; // holds first character of user reply
  65.  
  66. struct employee *current_ptr; // pointer to current node
  67. struct employee *head_ptr; // points to first node
  68.  
  69. // set up storage for first node
  70. head_ptr = (struct employee *) malloc (sizeof (struct employee));
  71. current_ptr = head_ptr;
  72.  
  73. while (more_data)
  74. {
  75.  
  76. // Read in employee ID and Hourly wage
  77. printf("\n\nEnter employee ID: ");
  78. scanf("%i", & current_ptr -> id);
  79.  
  80. printf("\nEnter employee weekly wage: ");
  81. scanf("%f", & current_ptr -> wage);
  82.  
  83. printf("\n\nWould you like to add another employee? (y/n): ");
  84. scanf("%s", answer);
  85.  
  86. // add another employee?
  87. if ((value = toupper(answer[0])) != 'Y')
  88. {
  89. current_ptr->next = (struct employee *) NULL;
  90. more_data = 0;
  91.  
  92. }
  93. else
  94. {
  95. // create new node, set next pointer to it
  96. current_ptr->next = (struct employee *) malloc (sizeof(struct employee));
  97.  
  98. // point to the new node
  99. current_ptr = current_ptr->next;
  100. }
  101. } // while
  102.  
  103. // print out current contents of linked list
  104. print_list(head_ptr);
  105.  
  106. printf("\n\nEnd of program\n");
  107.  
  108. return (0);
  109. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout

Enter employee ID: 
Enter employee weekly wage: 

Would you like to add another employee? (y/n): 
Employee ID:      0, Wages:     0.00


Total Number of Employees = 1


End of program