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


i want delete node in linked list doesn't work expected. i'm not sure how free deleted node @ points. here's source:

typedef struct node {     struct node *next;     //there more values here take 1 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 remove 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 nodes goes here     //for lets assume have 3 nodes values [1, 2, 3]     printallnode(newschedule);     validatenodes(&newschedule, &mainschedule);     printallnode(newschedule); } 

the function printallnode loops through nodes , print value. output of code like:

1 2 3 head element detected 1 2 3 

as can see nothing gone. how can fix that? i'm unscure how handle pointers 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; } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -