C - deleting node in doubly linked list -


when try delete every other element in list using deleteinst method method nothing linked list , there no errors. i'm not sure why it's not working i've seen same deleteinst method used in different program. maybe has pointers. if run deleteinst(track.prev, &head); without while loop list still remains unchanged.

please let me know if have idea or need more info. thank time.

int main() { node *head; node *track; head = readnodelist(stdin);  track = lastnode(head); //goes last node.  while(track != null){      int i=0;             //delete every other node     if(i%2 == 1){         deleteinst(track, &head);     } i++; track = track->prev;     } }  void deleteinst(instruction *victim, instruction **head){    if(*head == null || victim == null)     return;    if(*head == victim)     *head = victim->next;    if(victim->next != null)     victim->next->prev = victim->prev;    if(victim->prev != null)     victim->prev->next = victim->next;         free(victim);   return; } 

one obvious glaring problem: don't want this:

 deleteinst(track, &head);  track = track->prev; 

the instant free node, lose right access members. save first recover after delete:

 node *save_prev = track->prev;  deleteinst(track, &head);  track = save_prev; 

another thing i'd check list structure correct, (only during debug):

static void checklist (node *curr) {     int count = 0;      // precon: head->prev must null.      if (curr != null) {         if (curr->prev != null) {             puts ("linked list structure error a!");             exit (1);         }     }      // check nodes.      while (curr != null) {         // precon: curr->prev->next must curr.          if (curr->prev != null) {             if (curr->prev->next != curr) {                 puts ("linked list structure error b!");                 exit (1);             }         }          // precon: curr->next->prev must curr.          if (curr->next != null) {             if (curr->next->prev != curr) {                 puts ("linked list structure error c!");                 exit (1);             }         }          // move next , keep count.         curr = curr->next;         count++;     }      // okay, output success message size.      printf ("linked list structure okay, size = %d\n", count); } 

calling checklist (head) validate linked list meets validity preconditions, on off-chance may have buggy code elsewhere, such when creating list in readnodelist().


beyond that, suggest single stepping code in ide or debugger after readnodelist() see it's doing. and, if don't have ide/debugger, pepper source code lots of lines like:

printf ("debug %d: track = %p\n", __line__, track); 

and examine output of debug statements analyse flow through program.


now, if debugging exercise, may surprise find out deleteinst never appears called, because i seems set 0.

and fault of little problem lies here:

while (track != null) {      int = 0; // <<<<<<<      //delete every other node      if (i%2 == 1) {         deleteinst (track, &head);     }     i++;     track = track->prev; } 

yes, that's right, setting i 0 every single time through loop, hence i % 2 never equal 1. need initialise i before loop (and undefined behaviour of accessing freed memory removed well):

int = 0; while (track != null) {      node *save_prev = track->prev;      //delete every other node      if (i%2 == 1)         deleteinst (track, &head);     i++;     track = save_prev; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -