c - Infinite Loop with Pointers - Why? -


i relatively new c & pointers. trying sort , print linked list of structs. either missing logical error or not understanding pointers. can please explain me missing in code? thank kindly in advance!

// *** sort linked list ( merge sort ) ***  struct address_node *newroot;  // ptr, rearptr, , temproot struct pointers newroot = root; root = root->next;  while (root != null) {     temproot = root;     ptr = newroot;     rearptr = newroot;      while (ptr != null)     {         printf("here");         if ((root->frequency) == (ptr->frequency))         {                       // special case: determine read hierarchy repeated                                 // entries             if ((root->read_order) < (ptr->read_order))             {                 if (ptr == newroot)                 {                     root = root->next;                     temproot->next = newroot;                     newroot = temproot;                     ptr = null;                 }                 else                 {                     root = root->next;                     temproot->next = ptr;                     rearptr->next = temproot;                     ptr = null;                 }             }         }         else if ((root->frequency) > ptr->frequency)         {                       // add before entry             if (ptr == newroot)             {                 root = root->next;                 temproot->next = newroot;                 newroot = temproot;                 ptr = null;             }             else             {                 root = root->next;                 temproot->next = ptr;                 rearptr->next = temproot;                 ptr = null;             }         }         else if (ptr->next == null)         {                       // if end of list             root = root->next;             ptr->next = temproot;             ptr = null;         }         else         {                       // spot not found yet< move forward through list             rearptr = ptr;             ptr = ptr->next;         }     } }  // *** print *** ptr = newroot; if (numout == 0) {     while (ptr != null)     {         printf("0x%zx :%d\n", ptr->address, ptr->frequency);         ptr = ptr->next;     } } else {     while (ptr != null && numout > 0)     {         printf("0x%zx :%d\n", ptr->address, ptr->frequency);         numout--;         ptr = ptr->next;     } } 

all pointers seem pointing same thing, root. in 1 instance root gets moved forward, point root->next points whats behind root. imagine root points bob , root->next points bill, assume first nest of ifs turn true, root = bill root->next = bob. no forward movement being made.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -