How to remove element from linked list in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
 
typedef struct Node {
    int x;
    struct Node *next;
} Node;
  
void remove_element(Node **root, int value) {
    if (*root == NULL) {
        return;
    }
    
    if ((*root)->x == value) {
        Node* element_to_remove = *root;
        *root = (*root)->next;
        free(element_to_remove);
        return;
    }
    
    for (Node* current = *root; current->next != NULL; current = current->next) {
        if (current->next->x == value) {
            Node* element_to_remove = current->next;
            current->next = current->next->next;
            free(element_to_remove);
            return;
        }
    }
}

void add_first(Node **root, int value) {
    Node* new_node = malloc(sizeof(int));
    if (new_node == NULL) {
        puts("malloc error");
        exit(1);
    }
     
    new_node->x = value;
    new_node->next = *root;
     
    *root = new_node;
} 

void add_after(Node *node, int value) {
    Node* new_node = malloc(sizeof(Node));
     
    if (new_node == NULL) {
        puts("malloc error");
        exit(1);
    }
     
    new_node->x = value;
    new_node->next = node->next;
    node->next = new_node;
}

void add_sorted(Node **root, int value) {
    if (*root == NULL || (*root)->x >= value) {
        add_first(root, value);
        return;
    }
    
    Node *current = *root;
    while (current->next != NULL) {
        if (current->next->x >= value) {
            add_after(current, value);
            return;
        }
        current = current->next;
    }
    add_after(current, value);
}
  
void add_node(Node **root, int value) {
    Node *new_node = malloc(sizeof(Node));
      
    if (new_node == NULL) {
        puts("malloc error");
        exit(1);
    }
      
    new_node->next = NULL;
    new_node->x = value;
      
    if (*root == NULL) {
        *root = new_node;
        return;
    }
      
    Node* current = *root;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = new_node;
}
  
void free_LinkedList(Node *root) {
    Node *next;
    while (root != NULL)  {  
        next = root->next;  
        free(root);  
        root = next;  
    }  
}
  
int main() {
    Node *root = NULL;
      
    add_sorted(&root, 7);
    add_sorted(&root, 2000);
    add_sorted(&root, 88);
    add_sorted(&root, 3);
    
    for (Node *current = root; current != NULL; current = current->next) {
        printf("%d\n", current->x);
    }
    
    remove_element(&root, 88);
    
    puts("\n");
    for (Node *current = root; current != NULL; current = current->next) {
        printf("%d\n", current->x);
    }

    free_LinkedList(root);
  
    return 0;
}
  
  
  
/*
run:
  
3
7
88
2000


3
7
2000

*/

 



answered Jan 1, 2021 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
 
typedef struct Node {
    int x;
    struct Node *next;
} Node;

void remove_element(Node **root, int value) {
    if (*root == NULL) {
        return;
    }
    
    if ((*root)->x == value) {
        Node* element_to_remove = *root;
        *root = (*root)->next;
        free(element_to_remove);
        return;
    }
    
    for (Node *current = *root; current->next != NULL; current = current->next) {
        if (current->next->x == value) {
            Node *element_to_remove = current->next;
            current->next = current->next->next;
            free(element_to_remove);
            return;
        }
    }
}
  
void add_node(Node **root, int value) {
    Node *new_node = malloc(sizeof(Node));
      
    if (new_node == NULL) {
        puts("malloc error");
        exit(1);
    }
      
    new_node->next = NULL;
    new_node->x = value;
      
    if (*root == NULL) {
        *root = new_node;
        return;
    }
      
    Node *current = *root;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = new_node;
}
  
void free_LinkedList(Node *root) {
    Node *next;
    while (root != NULL)  {  
        next = root->next;  
        free(root);  
        root = next;  
    }  
}
  
int main() {
    Node *root = NULL;
      
    add_node(&root, 7);
    add_node(&root, 300);
    add_node(&root, 88);
    add_node(&root, 9900);
    add_node(&root, 20);
      
    for (Node *current = root; current != NULL; current = current->next) {
        printf("%d\n", current->x);
    }
    
    remove_element(&root, 88);
    
    puts("\n");
    for (Node *current = root; current != NULL; current = current->next) {
        printf("%d\n", current->x);
    }
      
    free_LinkedList(root);
  
    return 0;
}
  
  
  
  
/*
run:
  
7
300
88
9900
20


7
300
9900
20
  
*/

 



answered Jan 1, 2021 by avibootz

Related questions

1 answer 187 views
1 answer 178 views
1 answer 155 views
1 answer 165 views
1 answer 156 views
1 answer 185 views
...