Remove element from linked list - C Data Structure

C examples for Data Structure:Linked List

Description

Remove element from linked list

Demo Code

#include <stdio.h>

struct Node/*from www  . j  av  a2 s . com*/
{
    int value;
    struct Node *next;
};

void removeNode(struct Node *NodeToKeep)
{
    NodeToKeep->next = NodeToKeep->next->next;
}

int main (void)
{
    struct Node head, n1, n2, n3;
    struct Node *listPointer;

    head.next = &n1;
    n1.value = 1;
    n1.next = &n2;
    n2.value = 2;
    n2.next = &n3;
    n3.value = 3;
    n3.next = (struct Node *) 0;   // mark list end with null pointer

    listPointer = head.next;

    printf("Values in the list before removal of second Node: ");

    while ( listPointer != (struct Node *) 0 ){
            printf ("%i ", listPointer->value);
            listPointer = listPointer->next;
    }
    printf("\n");

    removeNode(&n1);

    listPointer = head.next;

    printf("Values in the list after removal of second Node: ");

    while ( listPointer != (struct Node *) 0 ){
        printf ("%i ", listPointer->value);
        listPointer = listPointer->next;
    }
    printf("\n");

    return 0;
}

Result


Related Tutorials