Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdbool.h>
- // structure of a node in the list
- struct node
- {
- int id;
- char name[50];
- long phone;
- char date[10];
- struct node *next;
- };
- // function to insert a new node at the appropriate position
- void insert(struct node **head_ref, int new_id, char new_name[], long new_phone, char new_date[])
- {
- struct node *new_node = (struct node *)malloc(sizeof(struct node));
- new_node->id = new_id;
- strcpy(new_node->name, new_name);
- new_node->phone = new_phone;
- strcpy(new_node->date, new_date);
- struct node *current;
- // if list is empty
- if (*head_ref == NULL)
- {
- new_node->next = *head_ref;
- *head_ref = new_node;
- }
- // if the id of new node is less than the id of first node, insert new node at the start
- else if (new_node->id < (*head_ref)->id)
- {
- new_node->next = *head_ref;
- *head_ref = new_node;
- }
- else
- {
- // else traverse the list and insert the new node at the appropriate position
- current = *head_ref;
- while (current->next != NULL && current->next->id < new_node->id)
- {
- current = current->next;
- }
- new_node->next = current->next;
- current->next = new_node;
- }
- }
- // function to remove a node from the list
- void removee(struct node **head_ref, int id)
- {
- // if list is empty
- if (*head_ref == NULL)
- return;
- // store head node
- struct node *temp = *head_ref, *prev;
- // if head needs to be removed
- if (temp != NULL && temp->id == id)
- {
- *head_ref = temp->next;
- free(temp);
- return;
- }
- // search for the id to be deleted and keep track of previous node
- while (temp != NULL && temp->id != id)
- {
- prev = temp;
- temp = temp->next;
- }
- // if id is not present in the list
- if (temp == NULL)
- return;
- // unlink the node from the list
- prev->next = temp->next;
- free(temp);
- }
- // function to update a node
- void update(struct node *head_ref, int id, char new_name[], long new_phone, char new_date[])
- {
- // if list is empty
- if (head_ref == NULL)
- return;
- // traverse the list and search for the id to be updated
- while (head_ref != NULL)
- {
- if (head_ref->id == id)
- {
- strcpy(head_ref->name, new_name);
- head_ref->phone = new_phone;
- strcpy(head_ref->date, new_date);
- }
- head_ref = head_ref->next;
- }
- }
- // function to search for a node
- void search(struct node *head_ref, int id)
- {
- // if list is empty
- if (head_ref == NULL)
- return;
- // traverse the list and search for the id
- while (head_ref != NULL)
- {
- if (head_ref->id == id)
- {
- printf("\nPatient ID\tPatient Name\tPhone number\tAppointment date\n");
- printf("%d\t\t%s\t\t%ld\t\t%s\n", head_ref->id, head_ref->name, head_ref->phone, head_ref->date);
- }
- head_ref = head_ref->next;
- }
- }
- // function to print the list
- void printList(struct node *node)
- {
- // if list is empty
- if (node == NULL)
- {
- printf("List is empty");
- return;
- }
- // traverse the list and print the content of each node
- printf("Patient ID\tPatient Name\tPhone number\tAppointment date\n");
- while (node != NULL)
- {
- printf("%d\t\t%s\t\t%ld\t\t%s\n", node->id, node->name, node->phone, node->date);
- node = node->next;
- }
- }
- int main()
- {
- struct node *head = NULL;
- int choice, id;
- char name[50];
- long phone;
- char date[10];
- while (1)
- {
- printf("\n*** Medical Appointment System for Pusat Kesihatan Universiti UTHM ***\n\n");
- printf("1. Insert\n");
- printf("2. Remove\n");
- printf("3. Update\n");
- printf("4. Find\n");
- printf("5. List All Records\n");
- printf("6. Exit\n\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- // perform the appropriate operation based on the choice
- switch (choice)
- {
- case 1:
- printf("Enter the patient id: ");
- scanf("%d", &id);
- printf("Enter the patient name: ");
- scanf("%s", name);
- printf("Enter the patient phone number: ");
- scanf("%ld", &phone);
- printf("Enter the patient date: ");
- scanf("%s", date);
- insert(&head, id, name, phone, date);
- printf("\nInserted Sccessfully\n\n");
- break;
- case 2:
- printf("Enter the patient id to be deleted: ");
- scanf("%d", &id);
- removee(&head, id);
- printf("\nDeleted Sccessfully\n\n");
- break;
- case 3:
- printf("Enter the patient id to be updated: ");
- scanf("%d", &id);
- printf("Enter the patient name: ");
- scanf("%s", name);
- printf("Enter the patient phone number: ");
- scanf("%ld", &phone);
- printf("Enter the patient date: ");
- scanf("%s", date);
- update(head, id, name, phone, date);
- printf("\nUpdated Sccessfully\n\n");
- break;
- case 4:
- printf("Enter the patient id to be searched: ");
- scanf("%d", &id);
- search(head, id);
- printf("\n\n");
- break;
- case 5:
- printList(head);
- printf("\n\n");
- break;
- case 6:
- exit(0);
- default:
- printf("Wrong input, please try again..\n");
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment