Facebook
From Rubayad, 2 Months ago, written in C.
Embed
Download Paste or View Raw
Hits: 201
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5.     int val;
  6.     struct node *next;
  7. } node_t;
  8.  
  9. // Function declarations
  10. void print_list(node_t *head);
  11. void push(node_t **head, int val);
  12. int pop(node_t **head);
  13. int remove_last(node_t *head);
  14. int remove_by_index(node_t **head, int n);
  15. void insert_at_index(node_t **head, int val, int index);
  16. int remove_by_value(node_t **head, int val);
  17. void free_list(node_t **head);
  18.  
  19. // Function implementations
  20. // (Implementations for print_list, push, pop, remove_last, and remove_by_index go here)
  21.  
  22. void insert_at_index(node_t **head, int val, int index) {
  23.     if (index == 0) {
  24.         push(head, val); // Reuse the push function to add at the beginning
  25.         return;
  26.     }
  27.  
  28.     node_t *current = *head;
  29.     for (int i = 0; current != NULL && i < index - 1; i++) {
  30.         current = current->next;
  31.     }
  32.  
  33.     if (current == NULL) {
  34.         // Index is greater than the number of nodes
  35.         return;
  36.     }
  37.  
  38.     node_t *new_node = malloc(sizeof(node_t));
  39.     if (new_node == NULL) {
  40.         // Memory allocation failed
  41.         return;
  42.     }
  43.  
  44.     new_node->val = val;
  45.     new_node->next = current->next;
  46.     current->next = new_node;
  47. }
  48.  
  49. int remove_by_value(node_t **head, int val) {
  50.     node_t *current = *head, *prev = NULL;
  51.  
  52.     if (current != NULL && current->val == val) {
  53.         // The head node holds the value to be removed
  54.         *head = current->next;
  55.         free(current);
  56.         return 0;
  57.     }
  58.  
  59.     while (current != NULL && current->val != val) {
  60.         prev = current;
  61.         current = current->next;
  62.     }
  63.  
  64.     if (current == NULL) {
  65.         // Value not found
  66.         return -1;
  67.     }
  68.  
  69.     prev->next = current->next;
  70.     free(current);
  71.     return 0;
  72. }
  73.  
  74. void free_list(node_t **head) {
  75.     node_t *current = *head;
  76.     node_t *next_node;
  77.  
  78.     while (current != NULL) {
  79.         next_node = current->next;
  80.         free(current);
  81.         current = next_node;
  82.     }
  83.  
  84.     *head = NULL;
  85. }
  86.  
  87. void push(node_t **head, int val) {
  88.     node_t *new_node = malloc(sizeof(node_t));
  89.     if (new_node == NULL) {
  90.         // Handle memory allocation failure
  91.         exit(1);
  92.     }
  93.  
  94.     new_node->val = val;
  95.     new_node->next = NULL;
  96.  
  97.     if (*head == NULL) {
  98.         *head = new_node;
  99.     } else {
  100.         node_t *current = *head;
  101.         while (current->next != NULL) {
  102.             current = current->next;
  103.         }
  104.         current->next = new_node;
  105.     }
  106. }
  107.  
  108. void print_list(node_t *head) {
  109.     node_t *current = head;
  110.     while (current != NULL) {
  111.         printf("%d -> ", current->val);
  112.         current = current->next;
  113.     }
  114.     printf("NULL\n");
  115. }
  116.  
  117.  
  118.  
  119.  
  120. int main() {
  121.     node_t *head = NULL;
  122.     int choice, value, index;
  123.  
  124.     while (1) {
  125.         printf("Choose an option:\n");
  126.         printf("1) Insert a value at the end\n");
  127.         printf("2) Delete a value\n");
  128.         printf("3) Empty list\n");
  129.         printf("4) Show list\n");
  130.         printf("5) Insert a value at a specific index\n");
  131.         printf("6) Exit\n");
  132.         scanf("%d", &choice;);
  133.  
  134.         switch (choice) {
  135.             case 1:
  136.                 printf("Enter the value to insert: ");
  137.                 scanf("%d", &value;);
  138.                 push(&head;, value);
  139.                 break;
  140.             case 2:
  141.                 printf("Enter the value to delete: ");
  142.                 scanf("%d", &value;);
  143.                 if (!remove_by_value(&head;, value)) {
  144.                     printf("Value deleted successfully.\n");
  145.                 } else {
  146.                     printf("Value not found.\n");
  147.                 }
  148.                 break;
  149.             case 3:
  150.                 free_list(&head;);
  151.                 printf("List emptied.\n");
  152.                 break;
  153.             case 4:
  154.                 printf("Current list:\n");
  155.                 print_list(head);
  156.                 break;
  157.             case 5:
  158.                 printf("Enter the value to insert: ");
  159.                 scanf("%d", &value;);
  160.                 printf("Enter the index at which to insert: ");
  161.                 scanf("%d", &index;);
  162.                 insert_at_index(&head;, value, index);
  163.                 break;
  164.             case 6:
  165.                 free_list(&head;);  // Clean up before exiting
  166.                 return 0;
  167.             default:
  168.                 printf("Invalid option. Please try again.\n");
  169.         }
  170.     }
  171.  
  172.     return 0;
  173. }