Advertisement
King_Kuro

Link List

Oct 21st, 2023
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 KB | None | 0 0
  1. // Online C compiler to run C program online
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct {
  6.     void *next;
  7.     int data;
  8. } Node;
  9.  
  10. Node *head = NULL;
  11.  
  12. //add
  13. Node *addNode(int data) {
  14.     Node *new = NULL;
  15.     //2 cases
  16.    
  17.     //if head is empty/null
  18.     if (head == NULL) {
  19.         new = malloc(sizeof(Node));
  20.         if (new == NULL)
  21.             return NULL;
  22.        
  23.         new->data = data;
  24.         head = new;
  25.         new->next = NULL;
  26.     }
  27.     else {
  28.         new = malloc(sizeof(Node));
  29.         if (new == NULL)
  30.             return NULL;
  31.        
  32.         new->data = data;
  33.         new->next = head;
  34.         head = new;
  35.     }
  36.    
  37.     return new;
  38. }
  39.  
  40.  
  41. //delete
  42. int removeNode(int data) {
  43.     Node *current = head;
  44.     Node *prev = head;
  45.     while (current != NULL) {
  46.         if (current->data == data) {
  47.             //if current is head
  48.             if (current == head) {
  49.                 head = current->next;
  50.             }
  51.             else {
  52.                 prev->next = current->next;
  53.                 free(current);
  54.                 current = NULL;
  55.             }
  56.            
  57.             return 1;
  58.         }
  59.         prev = current;
  60.         current = current->next;
  61.     }
  62.    
  63.     return 0;
  64. }
  65.  
  66. //insert
  67. Node *insertNode(int data, int position) {
  68.     Node *current = head;
  69.     while (current != NULL && position != 0) {
  70.         position--;
  71.     }
  72.     if (position != 0) {
  73.         printf("Request position too far into list\n");
  74.         return NULL;
  75.     }
  76.    
  77.     Node *new = malloc(sizeof(Node));
  78.     if (new == NULL) {
  79.         return NULL;
  80.     }
  81.     new->data = data;
  82.     new->next = current->next;
  83.     current->next = new;
  84.    
  85.     return new;
  86. }
  87.  
  88.  
  89. //print node
  90. void printList() {
  91.     Node *current = head;
  92.     while (current != NULL) {
  93.         printf("%d->", current->data);
  94.         current = current->next;
  95.     }
  96.     printf("\n");
  97.     return;
  98. }
  99.  
  100. //menu options
  101. void printMenu() {
  102.     printf("1. Add node\n");
  103.     printf("2. Delete node\n");
  104.     printf("3. Insert node\n");
  105.     printf("4. Print node\n");
  106.     printf("5. Quit\n");
  107.     printf(">>");
  108.     return;
  109.    
  110. }
  111.  
  112. int main() {
  113.     int option = 1;
  114.     int arg1 = 0;
  115.     int arg2 = 0;
  116.    
  117.     while (option != 5) {
  118.         printMenu();
  119.         int num_received = scanf("%d", &option);
  120.         if (num_received == 1 && option > 0 && option <= 5)
  121.         {
  122.             switch (option) {
  123.                 case 1:
  124.                     //add
  125.                     printf("Enter data to insert: ");
  126.                     scanf("%d", &arg1);
  127.                     Node *new = addNode(arg1);
  128.                     break;
  129.                 case 2:
  130.                     //delete
  131.                     printf("Enter data to delete: ");
  132.                     scanf("%d", &arg1);
  133.                     int success = removeNode(arg1);
  134.                     if (!success) {
  135.                         printf("Element not found!");
  136.                     }
  137.                     break;
  138.                 case 3:
  139.                     int option2 = 0;
  140.                     //insert
  141.                     printf("Enter data to insert: ");
  142.                     scanf("%d", &arg1);
  143.                     printf("Enter Position: ");
  144.                     scanf("%d", &arg2);
  145.                     Node *new2 = insertNode(arg1, arg2);
  146.                     if (new2 == NULL) {
  147.                         printf("Insert Failed!\n");
  148.                     }
  149.                     break;
  150.                 case 4:
  151.                     //print
  152.                     printList();
  153.                     break;
  154.                 case 5:
  155.                     break;
  156.             }
  157.         }
  158.     }
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement