Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.57 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int ID = 1;
  6. struct List list;
  7.  
  8. struct Person
  9. {
  10.     // Unique identifier for the person
  11.     int id;
  12.     // Information about person
  13.     char name[20];
  14.     int age;
  15.     // Pointer to next person in list
  16.     struct Person *next;
  17. };
  18.  
  19. struct List
  20. {
  21.     // First person in the list. A value equal to NULL indicates that the
  22.     // list is empty.
  23.     struct Person *head;
  24.     // Current person in the list. A value equal to NULL indicates a
  25.     // past-the-end position.
  26.     struct Person *current;
  27.     // Pointer to the element appearing before 'current'. It can be NULL if
  28.     // 'current' is NULL, or if 'current' is the first element in the list.
  29.     struct Person *previous;
  30.     // Number of persons in the list
  31.     int count;
  32. };
  33.  
  34. // Give an initial value to all the fields in the list.
  35. void ListInitialize(struct List *list)
  36. {
  37.     list->head = NULL;
  38.     list->current = NULL;
  39.     list->previous = NULL;
  40.     list->count = 0;
  41. }
  42.  
  43.  
  44. // Move the current position in the list one element forward. If last element
  45. // is exceeded, the current position is set to a special past-the-end value.
  46. void ListNext(struct List *list)
  47. {
  48.     if (list->current)
  49.     {
  50.         list->previous = list->current;
  51.         list->current = list->current->next;
  52.     }
  53.  
  54. }
  55.  
  56. // Move the current position to the first element in the list.
  57. void ListHead(struct List *list)
  58. {
  59.     list->previous = NULL;
  60.     list->current = list->head;
  61. }
  62.  
  63. // Get the element at the current position, or NULL if the current position is
  64. // past-the-end.
  65. struct Person *ListGet(struct List *list)
  66. {
  67.     return list->current;
  68. }
  69.                                                                                                              
  70. // Set the current position to the person with the given id. If no person
  71. // exists with that id, the current position is set to past-the-end.
  72. void ListFind(struct List *list, int id)
  73. {
  74.     ListHead(list);
  75.     while (list->current && list->current->id != id)
  76.     ListNext(list);
  77. }
  78.  
  79.  
  80. // Insert a person before the element at the current position in the list. If
  81. // the current position is past-the-end, the person is inserted at the end of
  82. // the list. The new person is made the new current element in the list.
  83. void ListInsert(struct List *list, struct Person *person)
  84. {
  85.     // Set 'next' pointer of current element
  86.     person->next = list->current;
  87.     // Set 'next' pointer of previous element. Treat the special case where
  88.     // the current element was the head of the list.
  89.     if (list->current == list->head)
  90.         list->head = person;
  91.     else
  92.         list->previous->next = person;
  93.     // Set the current element to the new person
  94.     list->current = person;
  95. }
  96.  
  97.  
  98. // Remove the current element in the list. The new current element will be the
  99. // element that appeared right after the removed element.
  100. void ListRemove(struct List *list)
  101. {
  102.     // Ignore if current element is past-the-end
  103.     if (!list->current)
  104.         return;
  105.     // Remove element. Consider special case where the current element is
  106.     // in the head of the list.
  107.     if (list->current == list->head)
  108.         list->head = list->current->next;
  109.     else
  110.         list->previous->next = list->current->next;
  111.     // Free element, but save pointer to next element first.
  112.     struct Person *next = list->current->next;
  113.     free(list->current);
  114.     // Set new current element
  115.     list->current = next;
  116. }
  117.  
  118.  
  119. void PrintPerson(struct Person *person)
  120. {
  121.     printf("Person with ID %d:\n", person->id);
  122.     printf("\tName: %s\n", person->name);
  123.     printf("\tAge: %d\n\n", person->age);
  124. }
  125.  
  126.  
  127. struct Person * makePerson()
  128. {
  129.     struct Person *p = malloc(sizeof(struct Person));
  130.     printf("\nYou selected \"Add a Person\".");
  131.         printf("\nEnter name: ");
  132.         char name[20];
  133.         scanf("%s", name);
  134.         printf("\nEnter an age: ");
  135.         int age;
  136.         scanf("%d", &age);
  137.         p->age = age;
  138.         strcpy(p->name, name);
  139.         p->id = ID;
  140.         ID++;
  141.     return p;
  142.  
  143. }
  144.  
  145.  
  146. void findPerson()
  147.  
  148. {
  149.     printf("\nYou selected \"Find a person\".");
  150.     printf("\nEnter an Id:  ");
  151.     int userId;
  152.     scanf("%d", &userId);
  153.     printf("\n");
  154.     ListFind(&list, userId);
  155.     struct Person *p = ListGet(&list);
  156.     if(p)
  157.     {
  158.         PrintPerson(p);
  159.     }
  160.     else
  161.     {
  162.         printf("\nCannot find specified user");
  163.     }
  164. }
  165.  
  166. void removePerson()
  167. {
  168.         printf("\nYou selected \"Remove a person\".");
  169.         printf("\nEnter an Id:  ");
  170.         int userId;
  171.         scanf("%d", &userId);
  172.         printf("\n");
  173.         ListFind(&list, userId);
  174.         struct Person *p = ListGet(&list);
  175.         if(p)
  176.         {
  177.         ListRemove(&list);
  178.                 printf("Removed a person with Id: %d\n", userId);
  179.         }
  180.         else
  181.         {
  182.                 printf("\nCannot find specified user");
  183.         }
  184.  
  185. }
  186.  
  187. void printList()
  188. {
  189.     ListHead(&list);
  190.         while (list.current)
  191.     {
  192.         struct Person *p = ListGet(&list);
  193.         PrintPerson(p);
  194.             ListNext(&list);
  195.     }
  196.  
  197. }
  198.  
  199. int main()
  200. {
  201.     int cond = 1;
  202.     ListInitialize(&list);
  203.         while(cond) { // Loop the menu
  204.         int choice;
  205.         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:");
  206.         scanf("%d", &choice); // Read in user choice
  207.         switch(choice){
  208.             case 1:
  209.             {
  210.                 struct Person *p = makePerson();
  211.                     ListInsert(&list, p);  
  212.                 break;
  213.             }
  214.             case 2:
  215.             {
  216.                 findPerson();
  217.                 break;
  218.             }
  219.             case 3:
  220.                 removePerson();
  221.                 break;
  222.             case 4:
  223.                 printList();
  224.                 break;
  225.             case 5:
  226.                 printf("\nYou selected \"Exit\".\n");
  227.                     cond = 0; // Break out of while loop
  228.                 break;
  229.             default:
  230.                 break; 
  231.          }
  232.         printf("\n\n\n");
  233.     }  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement