xii777x

Untitled

Jun 11th, 2022 (edited)
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5.  
  6. // structure of a node in the list
  7. struct node
  8. {
  9.     int id;
  10.     char name[50];
  11.     long phone;
  12.     char date[10];
  13.     struct node *next;
  14. };
  15.  
  16. // function to insert a new node at the appropriate position
  17. void insert(struct node **head_ref, int new_id, char new_name[], long new_phone, char new_date[])
  18. {
  19.     struct node *new_node = (struct node *)malloc(sizeof(struct node));
  20.     new_node->id = new_id;
  21.     strcpy(new_node->name, new_name);
  22.     new_node->phone = new_phone;
  23.     strcpy(new_node->date, new_date);
  24.     struct node *current;
  25.     // if list is empty
  26.     if (*head_ref == NULL)
  27.     {
  28.         new_node->next = *head_ref;
  29.         *head_ref = new_node;
  30.     }
  31.     // if the id of new node is less than the id of first node, insert new node at the start
  32.     else if (new_node->id < (*head_ref)->id)
  33.     {
  34.         new_node->next = *head_ref;
  35.         *head_ref = new_node;
  36.     }
  37.     else
  38.     {
  39.         // else traverse the list and insert the new node at the appropriate position
  40.         current = *head_ref;
  41.         while (current->next != NULL && current->next->id < new_node->id)
  42.         {
  43.             current = current->next;
  44.         }
  45.         new_node->next = current->next;
  46.         current->next = new_node;
  47.     }
  48. }
  49.  
  50. // function to remove a node from the list
  51. void removee(struct node **head_ref, int id)
  52. {
  53.     // if list is empty
  54.     if (*head_ref == NULL)
  55.         return;
  56.     // store head node
  57.     struct node *temp = *head_ref, *prev;
  58.     // if head needs to be removed
  59.     if (temp != NULL && temp->id == id)
  60.     {
  61.         *head_ref = temp->next;
  62.         free(temp);
  63.         return;
  64.     }
  65.     // search for the id to be deleted and keep track of previous node
  66.     while (temp != NULL && temp->id != id)
  67.     {
  68.         prev = temp;
  69.         temp = temp->next;
  70.     }
  71.     // if id is not present in the list
  72.     if (temp == NULL)
  73.         return;
  74.     // unlink the node from the list
  75.     prev->next = temp->next;
  76.     free(temp);
  77. }
  78.  
  79. // function to update a node
  80. void update(struct node *head_ref, int id, char new_name[], long new_phone, char new_date[])
  81. {
  82.     // if list is empty
  83.     if (head_ref == NULL)
  84.         return;
  85.     // traverse the list and search for the id to be updated
  86.     while (head_ref != NULL)
  87.     {
  88.         if (head_ref->id == id)
  89.         {
  90.             strcpy(head_ref->name, new_name);
  91.             head_ref->phone = new_phone;
  92.             strcpy(head_ref->date, new_date);
  93.         }
  94.         head_ref = head_ref->next;
  95.     }
  96. }
  97.  
  98. // function to search for a node
  99. void search(struct node *head_ref, int id)
  100. {
  101.     // if list is empty
  102.     if (head_ref == NULL)
  103.         return;
  104.     // traverse the list and search for the id
  105.     while (head_ref != NULL)
  106.     {
  107.         if (head_ref->id == id)
  108.         {
  109.             printf("\nPatient ID\tPatient Name\tPhone number\tAppointment date\n");
  110.             printf("%d\t\t%s\t\t%ld\t\t%s\n", head_ref->id, head_ref->name, head_ref->phone, head_ref->date);
  111.         }
  112.         head_ref = head_ref->next;
  113.     }
  114. }
  115.  
  116. // function to print the list
  117. void printList(struct node *node)
  118. {
  119.     // if list is empty
  120.     if (node == NULL)
  121.     {
  122.         printf("List is empty");
  123.         return;
  124.     }
  125.     // traverse the list and print the content of each node
  126.     printf("Patient ID\tPatient Name\tPhone number\tAppointment date\n");
  127.     while (node != NULL)
  128.     {
  129.         printf("%d\t\t%s\t\t%ld\t\t%s\n", node->id, node->name, node->phone, node->date);
  130.         node = node->next;
  131.     }
  132. }
  133.  
  134. int main()
  135. {
  136.     struct node *head = NULL;
  137.     int choice, id;
  138.     char name[50];
  139.     long phone;
  140.     char date[10];
  141.  
  142.     while (1)
  143.     {
  144.         printf("\n*** Medical Appointment System for Pusat Kesihatan Universiti UTHM ***\n\n");
  145.         printf("1. Insert\n");
  146.         printf("2. Remove\n");
  147.         printf("3. Update\n");
  148.         printf("4. Find\n");
  149.         printf("5. List All Records\n");
  150.         printf("6. Exit\n\n");
  151.         printf("Enter your choice: ");
  152.         scanf("%d", &choice);
  153.         // perform the appropriate operation based on the choice
  154.         switch (choice)
  155.         {
  156.         case 1:
  157.             printf("Enter the patient id: ");
  158.             scanf("%d", &id);
  159.             printf("Enter the patient name: ");
  160.             scanf("%s", name);
  161.             printf("Enter the patient phone number: ");
  162.             scanf("%ld", &phone);
  163.             printf("Enter the patient date: ");
  164.             scanf("%s", date);
  165.             insert(&head, id, name, phone, date);
  166.             printf("\nInserted Sccessfully\n\n");
  167.             break;
  168.         case 2:
  169.             printf("Enter the patient id to be deleted: ");
  170.             scanf("%d", &id);
  171.             removee(&head, id);
  172.             printf("\nDeleted Sccessfully\n\n");
  173.             break;
  174.         case 3:
  175.             printf("Enter the patient id to be updated: ");
  176.             scanf("%d", &id);
  177.             printf("Enter the patient name: ");
  178.             scanf("%s", name);
  179.             printf("Enter the patient phone number: ");
  180.             scanf("%ld", &phone);
  181.             printf("Enter the patient date: ");
  182.             scanf("%s", date);
  183.             update(head, id, name, phone, date);
  184.             printf("\nUpdated Sccessfully\n\n");
  185.             break;
  186.         case 4:
  187.             printf("Enter the patient id to be searched: ");
  188.             scanf("%d", &id);
  189.             search(head, id);
  190.             printf("\n\n");
  191.             break;
  192.         case 5:
  193.             printList(head);
  194.             printf("\n\n");
  195.             break;
  196.         case 6:
  197.             exit(0);
  198.         default:
  199.             printf("Wrong input, please try again..\n");
  200.         }
  201.     }
  202.     return 0;
  203. }
Add Comment
Please, Sign In to add comment