C - Linked List Concatenation Runs into malloc Error -


i have been having trouble creating concat(); function, works, when go ahead , free lists, malloc error... positive freeing list correctly, when not run concat(); function list gets freed correctly. reason believe doing wrong in function...

typedef struct node {     elemtype val;     struct node *next; } node;   struct list_struct {     node *front;     node *back; };      void lst_concat(list *a, list *b) { if( !( a->front && b->front)) return ;  if (a->front == null && b->back == null) {      return; }  if (a->front == null && b->front != null) {     a->front = b->front;     return; } else{ node *tmp = a->front;     if( a->back == null )return;     if( a->front == null )return;     if( b->back == null )return;     if( b->front == null )return;     else{     node *tempptr = a->back;      tempptr->next=b->front;     tempptr = b->front;     a->back = b->back;     } }} void lst_free(list *l) { node *p = l->front; node *pnext;    while(p!= null) {     pnext = p->next;   // keeps de-referencing freed ptr     free(p);     p = pnext;   }   // free list    free(l); } 

**update, updates list correctly, however, can still free both lists, or can free 1 since joined? lst_free() above.

this rational way can imagine (so far) ...

typedef struct node         node; typedef struct list_struct  list;   typedef struct node {     elemtype    val;     node        *next; } node;   struct list_struct {     node        *front;     node        *back; };   // ending node node *lastnode(node *root){     node *tmp = null;     while(root){         tmp  = root;         root = root->next;    }    return tmp; }   // move node pnode // , put @ end of proot nodes void move_node(node **proot,node **pnode){      if(! (pnode && proot) )         return; // no comments      if(pnode == proot)         return; // no comments      node *node = *pnode;     node *root = *proot;     node *tmp;      if(!node) return;      if(!root){         // node becomes first 1         *proot      = node;     }else{         // node becomes last node         lastnode(root)->next = node;     }      // nodes blongs other node     // set null     *pnode = null;  }   // move nodes b; //  void lst_concat(list *a, list *b) {     move_node(&a->front, &b->front);     move_node(&a->back, &b->back);     // @ point b has no front, , no     // has been moved  }   void free_nodes(node **proot){     if ( !(proot && *proot) )         return;      node *root = *proot;      if( root->next)         free_nodes(&root->next);      free(root);     *proot=null; }  void lst_free(list *l) {     if( l ) {         free_nodes( &l->front);         free_nodes( &l->back);         free(l);     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -