C Remove node from linked list

44,812

Solution 1

My guess:

void RemoveNode(Node * node, Node ** head) {
    if (strcmp(node->state, ((*head)->state) == 0) {
        Node * temp = *head;
        *head = (*head)->next;
        free(temp);
        return;
    }

    Node * current = (*head)->next;
    Node * previous = *head;
    while (current != NULL && previous != NULL) {
        if (strcmp(node->state, current->state) == 0) {
            Node * temp = current;
            previous->next = current->next;
            free(temp);
            return;
        }
        previous = current;
        current = current->next;
    }
    return;
}

Solution 2

I would recommend that you try doing this with recursion, to avoid the need for a "double pointer". It will extremely simplify the logic. This link has a very good explanation and implementation of doing this recursively. This one specifically will even work if you attempt to remove a node from an empty linked list.

Node *ListDelete(Node *currP, State value)
{
  /* See if we are at end of list. */
  if (currP == NULL)
    return NULL;

  /*
   * Check to see if current node is one
   * to be deleted.
   */
  if (currP->state == value) {
    Node *tempNextP;

    /* Save the next pointer in the node. */
    tempNextP = currP->next;

    /* Deallocate the node. */
    free(currP);

    /*
     * Return the NEW pointer to where we
     * were called from.  I.e., the pointer
     * the previous call will use to "skip
     * over" the removed node.
     */
    return tempNextP;
  }

  /*
   * -------------- RECURSION-------------------
   * Check the rest of the list, fixing the next
   * pointer in case the next node is the one
   * removed.
   */
  currP->next = ListDelete(currP->next, value);


  /*
   * Return the pointer to where we were called
   * from.  Since we did not remove this node it
   * will be the same.
   */
  return currP;
}
Share:
44,812
Travv92
Author by

Travv92

Updated on July 11, 2022

Comments

  • Travv92
    Travv92 almost 2 years

    How can I go about removing a node from a linked list?

    Here is my code:

    void RemoveNode(Node * node, Node ** head) {
        if (strcmp(node->state, (*(*head)->next).state) == 0) {
            Node * temp = *head;
            *head = (*head)->next;
            free(temp);
            return;
        }
    
        Node * current = (*head)->next;
        Node * previous = *head;
        while (current != NULL && previous != NULL) {
            if (strcmp(node->state, (*current->next).state) == 0) {
                Node * temp = current;
                previous->next = current->next;
                free(temp);
                return;
            }
            current = current->next;
            previous = previous->next;
        }
        return;
    }
    

    But I keep getting seg faults.

    I feel like I'm doing something stupid.... Any ideas?

  • Some programmer dude
    Some programmer dude over 10 years
    It would be more helpful if you pointed out what you've changed.
  • Jiminion
    Jiminion over 10 years
    I changed comparision with the 'next' value to just the present value, and I changed the update of previous in the while loop.
  • Travv92
    Travv92 over 10 years
    Thanks, this was exactly it!