简体   繁体   中英

Deletion of a node in linked list passed to function by reference

I want to delete a node in a linked list but it doesn't work as expected. I'm also not sure how to free the to be deleted node at some points. Here's the source:

typedef struct Node {
    struct Node *next;
    //there are much more values here but just take this one for simplicity
    int value;
} Node;


void removeNode(Node **nH, Node **n, Node **nP){
    Node *current = (*n);
    Node *nodeHead = (*nH);
    Node *nodePrev = (*nP);
    if(nodeHead == current){
        puts("head element detected");
        if(current->next == NULL){
            free(current);
            current = NULL;
            nodeHead = NULL;
        } else{
            current = current->next;
        }
    } else {
        if (current->next == NULL){
            nodePrev->next = NULL;
            free(current);
            current = nodePrev;
        } else{
            current = current->next;
        }
    }
}

void validateNodes(Node **newNodes, Node **mainNodes){
    Node *newHead = (*newNodes);
    Node *newTmp = newHead;
    Node *newPrev = NULL;
    //validation of values here
    //for testing just remove the head element
    removeNode(&newHead, &newTmp, &newPrev);
}
void printAllNodes(Node *s){
    for(;;){
        printf("%d\n", s->value);
        if(s->next != NULL)
            s = s->next;
        else
            break;
    }   
}

int main(int argc, char **argv){
    //population of the nodes goes here
    //for now lets assume we have 3 nodes with the values [1, 2, 3]
    printAllNode(newSchedule);
    validateNodes(&newSchedule, &mainSchedule);
    printAllNode(newSchedule);
}

The function printAllNode just loops through the nodes and print value . The output of the code is like:

1
2
3
head element detected
1
2
3

As you can see nothing is gone. How can I fix that? I'm very unscure how to handle those pointers to pointers.

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

typedef struct Node {
    struct Node *next;
    int value;
} Node;

void removeNode(Node **nH, Node *n, Node *nP){
    if(*nH == n){
        if(nP)
            nP->next = (*nH)->next;
        else
            *nH = (*nH)->next;
        free(n);
    } else {
        if((*nH)->next)
            removeNode(&(*nH)->next, n, *nH);
    }
}

void validateNodes(Node **newNodes, Node **mainNodes){
    removeNode(newNodes, *newNodes, NULL);
}
void printAllNodes(Node *s){
    while(s){
        printf("%d\n", s->value);
        s = s->next;
    }
}

int main(int argc, char **argv){
    Node *newSchedule, *mainSchedule;
    Node **nodes = malloc(3 * sizeof(Node*));
    int i;
    for(i=0;i<3;++i)
        nodes[i]=malloc(sizeof(Node));
    for(i=0;i<3;++i){
        nodes[i]->value = i+1;
        nodes[i]->next = nodes[i+1];
    }
    nodes[2]->next = NULL;
    newSchedule = nodes[0];
    printAllNodes(newSchedule);
    validateNodes(&newSchedule, &mainSchedule);
    printf("\n");
    printAllNodes(newSchedule);
    //deallocate;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM