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

Enter employee ID: 
Enter employee weekly wage: 

Would you like to add another employee? (y/n): 

Enter employee ID: 
Enter employee weekly wage: 

Would you like to add another employee? (y/n): 

Enter employee ID: 
Enter employee weekly wage: 

Would you like to add another employee? (y/n): 
Employee ID:  98405, Wage:    20.00

Employee ID:  23125, Wage:    21.00

Employee ID:   2185, Wage:     2.00


Total Number of Employees = 3


End of program