user3323775
user3323775

Reputation: 3

Removing node from middle of linked list

I am trying to create a linked list. Each node will hold a struct, and a pointer to the next node. When trying to remove a node from the middle of the list, the program stops due to a segmentation fault. I've tried going about this a few different ways. Here's the algorythms I've tried using , after iterating to the node I wish to delete.

1.set the previous node's 'next' pointer to the node after the node to be deleted.

// example
node_t *current = head;
while(current->next != NULL) {
    if(current->next->b.y <= 5) {
        current->next = current->next->next; // first idea, didn't work
    }
    current = current->next;
}

This, did not work. So I adjusted it to

1.create a pointer to a node named temp.

2.copy the node to be deleted into temp.

3.set the previous node's 'next' pointer to temp's 'next' pointer.

4.free temp

// example
node_t *current = head;
while(current->next != NULL) {
    if(current->next->b.y <= 5) {
        node_t *temp;
        temp = current->next;
        current->next = temp->next;
        free(temp);
    }
    current = current->next;
}

It still does not work. I really have no idea what is wrong, since to me it seems pretty syntaticly correct. I know I must have messed up somewhere with how I either initialized the pointers, or how im deleting the node. I would really apreciate if someone could tell me why the code isn't working so I could fix it.

Upvotes: 0

Views: 330

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754710

As noted in a comment, you just have to check that current is not null as well as current->next.

#include <stdio.h>
#include <stdlib.h>

typedef struct node_t
{
    struct node_t *next;
    int data;
} node_t;

static void add_node(node_t **head, int value);
static void free_list(node_t **head);
static void dump_list(node_t *head);

int main(void)
{
    node_t *head = 0;
    add_node(&head, 3);
    add_node(&head, 6);
    add_node(&head, 9);
    add_node(&head, 4);
    add_node(&head, 8);
    add_node(&head, 2);
    dump_list(head);

    node_t *current = head;
    while (current != NULL && current->next != NULL)
    {
        if (current->next->data <= 5)
        {
            current->next = current->next->next;
        }
        current = current->next;
    }
    dump_list(head);
    free_list(&head);
    dump_list(head);

    return 0;
}

static void add_node(node_t **head, int value)
{
    node_t *node = malloc(sizeof(*node));
    node->data = value;
    node->next = *head;
    *head = node;
}

static void dump_list(node_t *head)
{
    char const *pad = "";
    while (head != 0)
    {
        printf("%s %d", pad, head->data);
        pad = " ->";
        head = head->next;
    }
    putchar('\n');
}

static void free_list(node_t **head)
{
    while (*head != 0)
    {
        node_t *next = (*head)->next;
        free(*head);
        *head = next;
    }
}

This crashed until the while loop was changed to check both current and current->next. The trouble is that if you delete the last node, current gets assigned NULL, which you can't then dereference.

NB: the code above doesn't check the return from malloc(), but not doing so is both lazy and bad.

Upvotes: 0

Related Questions