Sierra_ONE

ADT Linked List

Jun 7th, 2024
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | Source Code | 0 0
  1. /*  Author:     Fabiola C. Villanueva
  2.     Project:    Linked List
  3.     Time:       July 9, 2023 (5:00pm)
  4.     End:        July 9, 2023 (7:42pm)
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #define MENU 5
  10.  
  11. typedef char STRING[50];
  12.  
  13. //structure definitions
  14. typedef struct node {
  15.     int data;
  16.     struct node *next;
  17. } NodeType, *NodePtr;
  18.  
  19. //function prototypes
  20. void insertSorted (NodePtr *list);
  21. int searchData (NodePtr list);
  22. void deleteAllData (NodePtr *list);
  23. void display (NodePtr list);
  24.  
  25. int main() {
  26.     system("CLS");
  27.  
  28.     NodePtr head = NULL;
  29.     int i, item, choice, pos;
  30.     STRING menu[MENU] = {  "Exit",
  31.                             "Insert Sorted",
  32.                             "Search Data",
  33.                             "Delete All",
  34.                             "Display List" };
  35.  
  36.     do {
  37.         printf("MENU\nKindly select a number:\n\n");
  38.         for (i = 0; i < MENU; ++i) {
  39.             printf("    [%d] %s\n", i, menu[i]);
  40.         }
  41.         printf("\nChoice: ");
  42.         scanf("%d", &choice);
  43.  
  44.         system("CLS");
  45.  
  46.         switch (choice) {
  47.             case 0:
  48.                 printf("\n\"EXIT\"\n\n");
  49.                 display(head);
  50.                 printf("\n\nTerminating program...");
  51.  
  52.                 break;
  53.            
  54.             case 1:
  55.                 printf("\n\"INSERT SORTED\"\n\n");
  56.                 display(head);
  57.                 insertSorted (&head);
  58.                 printf("\n");
  59.                 display(head);
  60.  
  61.                 break;
  62.            
  63.             case 2:
  64.                 printf("\n\"SEARCH DATA\"\n\n");
  65.                 display(head);
  66.                 pos = searchData (head);
  67.                 printf("Data is at position = [%d]\n\n", pos);
  68.                 display(head);
  69.  
  70.                 break;
  71.  
  72.             case 3:
  73.                 printf("\n\"DELETE ALL\"\n\n");
  74.                 display(head);
  75.                 deleteAllData (&head);
  76.                 display(head);
  77.  
  78.                 break;
  79.  
  80.             case 4:
  81.                 printf("\n\"DISPLAY LIST\"\n\n");
  82.                 display(head);
  83.  
  84.                 break;
  85.         }
  86.        
  87.         printf("\n\n");
  88.         system("PAUSE");
  89.         system("CLS");
  90.  
  91.     } while (choice != 0);
  92.  
  93.     printf("\n");
  94.     return 0;
  95. }
  96.  
  97. //function defenitions
  98. void insertSorted (NodePtr *list) {                             //a list from main will never enter this function unsorted
  99.     NodePtr temp = (NodePtr) malloc (sizeof(NodeType));         //make temp node
  100.     NodePtr *trav;
  101.     int item;
  102.  
  103.     printf("\nEnter item to insert: ");         //user input for data to insert
  104.     scanf("%d", &item);
  105.  
  106.     if (temp != NULL) {                         //if temp is allocated
  107.         temp->data = item;                      //populate temp data
  108.         temp->next = NULL;                      //pre assign to NULL, overwrite later
  109.     }                                           //traverse until in right position
  110.  
  111.     for (trav = list; (*trav) != NULL && (*trav)->data < temp->data; trav = &(*trav)->next) {}
  112.  
  113.     temp->next = (*trav);                       //exits loop once item is in right position
  114.     (*trav) = temp;                             //insert the temp node in place
  115. }
  116.  
  117. int searchData (NodePtr list) {     //return pos, if pos not found return -1
  118.     NodePtr trav;
  119.     int i, item, ret = -1;
  120.  
  121.     printf("\nEnter data to search: ");
  122.     scanf("%d", &item);
  123.  
  124.     for (trav = list, i = 0; trav != NULL && trav->data != item; trav = trav->next, ++i) {
  125.         if (trav->data == item) {       //traverse and checking if item is found,
  126.             ret = i;                    //loop automatically exits then assigns i to ret
  127.         }
  128.     }                                   //if not found, ret stays -1
  129.  
  130.     return ret;
  131. }
  132.  
  133. void deleteAllData (NodePtr *list) {        //delete all occurences of user input item
  134.     NodePtr *trav, temp;
  135.     int item;
  136.  
  137.     printf("\nEnter item to delete: ");    
  138.     scanf("%d", &item);
  139.  
  140.     for (trav = list; (*trav) != NULL;) {
  141.         if ((*trav)->data == item) {        //if data in a node is found,
  142.             temp = (*trav);                 //temp will get whole node to delete
  143.             (*trav) = temp->next;           //update for conditon when item found
  144.             free(temp);                     //deletion of temp
  145.         } else {
  146.             trav = &(*trav)->next;          //update to continue if not item
  147.         }
  148.     }
  149. }
  150.  
  151. void display (NodePtr list) {       //"pass by copy of list"; accessing via single pointer direct to first node
  152.     printf("List = { ");
  153.     for (; list != NULL; list = list->next) {
  154.         printf("[%d]", list->data);
  155.         if (list->next != NULL) {
  156.             printf(" -> ");
  157.         }
  158.     }
  159.     printf(" }");
  160. }
Advertisement
Add Comment
Please, Sign In to add comment