Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.12 KB | None | 0 0
  1. Jagroop
  2.  
  3.  
  4. Search Drive
  5.  
  6. Drive
  7. .
  8. Folder Path
  9. My Drive
  10. eece 2160
  11. lab 3
  12. NEW
  13. Folders and views
  14. My Drive
  15. Shared with me
  16. Google Photos
  17. Recent
  18. Starred
  19. Trash
  20. 494 MB used
  21. .
  22.  
  23. Image
  24. addPerson.PNG
  25.  
  26. Image
  27. assgn5.PNG
  28.  
  29. Image
  30. breakpoint.PNG
  31.  
  32. Image
  33. findPerson.PNG
  34.  
  35. Image
  36. gdb list.PNG
  37.  
  38. Google Docs
  39. Lab Number: 3
  40.  
  41. Unknown File
  42. person
  43.  
  44. C
  45. person.c
  46.  
  47. C
  48. person.c
  49.  
  50. Unknown File
  51. personDebug
  52.  
  53. C
  54. personDebug.c
  55.  
  56. Image
  57. pre lab.png
  58.  
  59. Google Docs
  60. Pre-Lab
  61.  
  62. Image
  63. printList.PNG
  64.  
  65. Image
  66. removePerson.PNG
  67. All selections cleared
  68.  
  69. #include <stdlib.h>
  70. #include <stdio.h>
  71. #include <string.h>
  72.  
  73. int ID = 1;
  74. struct List list;
  75.  
  76. struct Person
  77. {
  78.     // Unique identifier for the person
  79.     int id;
  80.     // Information about person
  81.     char name[20];
  82.     int age;
  83.     // Pointer to next person in list
  84.     struct Person *next;
  85. };
  86.  
  87. struct List
  88. {
  89.     // First person in the list. A value equal to NULL indicates that the
  90.     // list is empty.
  91.     struct Person *head;
  92.     // Current person in the list. A value equal to NULL indicates a
  93.     // past-the-end position.
  94.     struct Person *current;
  95.     // Pointer to the element appearing before 'current'. It can be NULL if
  96.     // 'current' is NULL, or if 'current' is the first element in the list.
  97.     struct Person *previous;
  98.     // Number of persons in the list
  99.     int count;
  100. };
  101.  
  102. // Give an initial value to all the fields in the list.
  103. void ListInitialize(struct List *list)
  104. {
  105.     list->head = NULL;
  106.     list->current = NULL;
  107.     list->previous = NULL;
  108.     list->count = 0;
  109. }
  110.  
  111.  
  112. // Move the current position in the list one element forward. If last element
  113. // is exceeded, the current position is set to a special past-the-end value.
  114. void ListNext(struct List *list)
  115. {
  116.     if (list->current)
  117.     {
  118.         list->previous = list->current;
  119.         list->current = list->current->next;
  120.     }
  121.  
  122. }
  123.  
  124. // Move the current position to the first element in the list.
  125. void ListHead(struct List *list)
  126. {
  127.     list->previous = NULL;
  128.     list->current = list->head;
  129. }
  130.  
  131. // Get the element at the current position, or NULL if the current position is
  132. // past-the-end.
  133. struct Person *ListGet(struct List *list)
  134. {
  135.     return list->current;
  136. }
  137.                                                                                                              
  138. // Set the current position to the person with the given id. If no person
  139. // exists with that id, the current position is set to past-the-end.
  140. void ListFind(struct List *list, int id)
  141. {
  142.     ListHead(list);
  143.     while (list->current && list->current->id != id)
  144.     ListNext(list);
  145. }
  146.  
  147.  
  148. // Insert a person before the element at the current position in the list. If
  149. // the current position is past-the-end, the person is inserted at the end of
  150. // the list. The new person is made the new current element in the list.
  151. void ListInsert(struct List *list, struct Person *person)
  152. {
  153.     // Set 'next' pointer of current element
  154.     person->next = list->current;
  155.     // Set 'next' pointer of previous element. Treat the special case where
  156.     // the current element was the head of the list.
  157.     if (list->current == list->head)
  158.         list->head = person;
  159.     else
  160.         list->previous->next = person;
  161.     // Set the current element to the new person
  162.     list->current = person;
  163. }
  164.  
  165.  
  166. // Remove the current element in the list. The new current element will be the
  167. // element that appeared right after the removed element.
  168. void ListRemove(struct List *list)
  169. {
  170.     // Ignore if current element is past-the-end
  171.     if (!list->current)
  172.         return;
  173.     // Remove element. Consider special case where the current element is
  174.     // in the head of the list.
  175.     if (list->current == list->head)
  176.         list->head = list->current->next;
  177.     else
  178.         list->previous->next = list->current->next;
  179.     // Free element, but save pointer to next element first.
  180.     struct Person *next = list->current->next;
  181.     free(list->current);
  182.     // Set new current element
  183.     list->current = next;
  184. }
  185.  
  186.  
  187. void PrintPerson(struct Person *person)
  188. {
  189.     printf("Person with ID %d:\n", person->id);
  190.     printf("\tName: %s\n", person->name);
  191.     printf("\tAge: %d\n\n", person->age);
  192. }
  193.  
  194.  
  195. struct Person * makePerson()
  196. {
  197.     struct Person *p = malloc(sizeof(struct Person));
  198.     printf("\nYou selected \"Add a Person\".");
  199.         printf("\nEnter name: ");
  200.         char name[20];
  201.         scanf("%s", name);
  202.         printf("\nEnter an age: ");
  203.         int age;
  204.         scanf("%d", &age);
  205.         p->age = age;
  206.         strcpy(p->name, name);
  207.         p->id = ID;
  208.         ID++;
  209.     return p;
  210.  
  211. }
  212.  
  213.  
  214. void findPerson()
  215.  
  216. {
  217.     printf("\nYou selected \"Find a person\".");
  218.     printf("\nEnter an Id:  ");
  219.     int userId;
  220.     scanf("%d", &userId);
  221.     printf("\n");
  222.     ListFind(&list, userId);
  223.     struct Person *p = ListGet(&list);
  224.     if(p)
  225.     {
  226.         PrintPerson(p);
  227.     }
  228.     else
  229.     {
  230.         printf("\nCannot find specified user");
  231.     }
  232. }
  233.  
  234. void removePerson()
  235. {
  236.         printf("\nYou selected \"Remove a person\".");
  237.         printf("\nEnter an Id:  ");
  238.         int userId;
  239.         scanf("%d", &userId);
  240.         printf("\n");
  241.         ListFind(&list, userId);
  242.         struct Person *p = ListGet(&list);
  243.         if(p)
  244.         {
  245.         ListRemove(&list);
  246.                 printf("Removed a person with Id: %d\n", userId);
  247.         }
  248.         else
  249.         {
  250.                 printf("\nCannot find specified user");
  251.         }
  252.  
  253. }
  254.  
  255. void printList()
  256. {
  257.     ListHead(&list);
  258.         while (list.current)
  259.     {
  260.         struct Person *p = ListGet(&list);
  261.         PrintPerson(p);
  262.             ListNext(&list);
  263.     }
  264.  
  265. }
  266.  
  267. int main()
  268. {
  269.     int cond = 1;
  270.     ListInitialize(&list);
  271.         while(cond) { // Loop the menu
  272.         int choice;
  273.         printf("\n------------------------------------\nMain menu:\n\n1. Add a Person\n2. Find a person\n3. Remove a person\n4. Print the List\n5. Exit\n\nSelect an option:");
  274.         scanf("%d", &choice); // Read in user choice
  275.         switch(choice){
  276.             case 1:
  277.             {
  278.                 struct Person *p = makePerson();
  279.                     ListInsert(&list, p);  
  280.                 break;
  281.             }
  282.             case 2:
  283.             {
  284.                 findPerson();
  285.                 break;
  286.             }
  287.             case 3:
  288.                 removePerson();
  289.                 break;
  290.             case 4:
  291.                 printList();
  292.                 break;
  293.             case 5:
  294.                 printf("\nYou selected \"Exit\".\n");
  295.                     cond = 0; // Break out of while loop
  296.                 break;
  297.             default:
  298.                 break; 
  299.          }
  300.         printf("\n\n\n");
  301.     }  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement