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. struct employee * findEntry (struct employee * list_ptr, int id_number);
  15. void print_list (struct employee *emp1);
  16.  
  17. /*-----------------------------------------------------------
  18. **
  19. ** FUNCTION: main
  20. **
  21. ** DESCRIPTION: This function will prompt the user for an
  22. ** employee id and wage until the user indicates they are
  23. ** finished. At that point, a list of id and wages will be
  24. ** generated.
  25. **
  26. ** PARAMETERS: None
  27. **
  28. ** OUTPUTS: None
  29. **
  30. ** CALLS: print_list
  31. **
  32. **-----------------------------------------------------------*/
  33.  
  34. int main ()
  35. {
  36.  
  37. char answer[80]; /* holds user reply to prompt */
  38. int id_value; /* ID to search for */
  39. int more_data = 1; /* flag to continue processing */
  40. char value; /* holds first character of user reply */
  41.  
  42. struct employee *current_ptr; /* pointer to current node*/
  43. struct employee *head_ptr; /* points to first node */
  44.  
  45. /* Set up storage for first node */
  46. head_ptr = (struct employee *) malloc (sizeof(struct employee));
  47. current_ptr = head_ptr;
  48.  
  49. while (more_data)
  50. {
  51.  
  52. /* Read in Employee ID and Hourly Wage */
  53. printf("\n\nEnter employee ID: ");
  54. scanf("%i", & current_ptr -> id);
  55.  
  56. printf("\nEnter employee weekly wage: ");
  57. scanf("%f", & current_ptr -> wage);
  58.  
  59. printf("\n\nWould you like to add another employee? (y/n): ");
  60. scanf("%s", answer);
  61.  
  62. /* add another employee? */
  63. if ((value = toupper(answer[0])) != 'Y')
  64. {
  65. current_ptr->next = (struct employee *) NULL;
  66. more_data = 0;
  67. }
  68. else
  69. {
  70. /* create new node, set next pointer to it */
  71. current_ptr->next = (struct employee *) malloc (sizeof(struct employee));
  72.  
  73. /* point to the new node */
  74. current_ptr = current_ptr->next;
  75. }
  76. } /* while */
  77.  
  78. /* print out current contents of linked list */
  79. print_list(head_ptr);
  80.  
  81. /* Enter an ID to search for in our list */
  82. printf ("Enter an ID to search for in our list: ");
  83. scanf ("%i", &id_value);
  84.  
  85. /* Search for it */
  86. if (findEntry (head_ptr, id_value) != (struct employee *) NULL)
  87. {
  88. printf ("\n\nID is in the list \n");
  89. }
  90. else /* NULL Ptr returned */
  91. {
  92. printf ("\n\nID is NOT in the list \n");
  93. }
  94.  
  95. printf("\n\nEnd of program\n");
  96.  
  97. return (0);
  98. }
  99.  
  100. /***********************************************************************************
  101. **
  102. ** Function: findEntry
  103. **
  104. ** Description: Using list_ptr as a starting point, examine every entry in
  105. ** the list until an entry is found that has an id_number which
  106. ** matches the one passed to the function. If found, return a pointer
  107. ** to the node, otherwise, return NULL.
  108. **
  109. ** Parameters:
  110. **
  111. ** list_ptr - pointer to the beginning of the link list
  112. ** id_number - employee identifier to search
  113. **
  114. ** Returns: Pointer to linked list node with the ID (otherwise NULL returned)
  115. **
  116. ************************************************************************************/
  117.  
  118. struct employee * findEntry (struct employee * list_ptr, int id_number)
  119. {
  120.  
  121. struct employee * found_it_ptr = list_ptr; /* set to beginning of list */
  122.  
  123. /* search through linked list, return a pointer to the node if the ID found in it */
  124.  
  125. for ( ; found_it_ptr; found_it_ptr = found_it_ptr->next)
  126. {
  127.  
  128. if ( found_it_ptr -> id == id_number )
  129. return ( found_it_ptr ); /* found */
  130.  
  131. }
  132.  
  133. return ( (struct employee *) NULL ); /* not found, return NULL pointer */
  134.  
  135. } /* find entry */
  136.  
  137.  
  138. /*---------------------------------------------------------------
  139. **
  140. ** FUNCTION: print_list
  141. **
  142. ** DESCRIPTION: This function will print the contents of a linked
  143. ** list. It will traverse the list from beginning to the
  144. ** end, printing the contents at each node.
  145. **
  146. ** PARAMETERS: emp1 - pointer to a linked list
  147. **
  148. ** OUTPUTS: None
  149. **
  150. ** CALLS: None
  151. **
  152. **---------------------------------------------------------------*/
  153.  
  154. void print_list (struct employee *emp1)
  155. {
  156. struct employee *tmp; /* tmp pointer */
  157. int i = 0; /* counts the nodes printed */
  158.  
  159. /* Start a beginning of list and print out each value */
  160. /* loop until tmp points to null (0 or false) */
  161. for(tmp = emp1; tmp ; tmp = tmp->next)
  162. {
  163. i++;
  164. printf("\nEmployee ID: %6i, Wage: %8.2f\n",tmp->id, tmp->wage);
  165.  
  166. }
  167.  
  168. printf("\n\nTotal Number of Employees = %d\n", i);
  169.  
  170. }
  171.  
Success #stdin #stdout 0s 5324KB
stdin
98401
10.60
Y
23123
9.75
N
23514
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): 
Employee ID:  98401, Wage:    10.60

Employee ID:  23123, Wage:     9.75


Total Number of Employees = 2
Enter an ID to search for in our list: 

ID is NOT in the list 


End of program