linked list insert function recursively c++ -


i'm writing insert function recursively adding element list.the problem when run program , trying insert, inserts once, @ second time breaks , has bug. suggestions, thanx

helper function:

 void list::inserthelper(node* list, int number)         {             if(list->next != null)             {                 inserthelper(list->next, number);             }             else             {                 list->next = new node;                 list->next->data = number;             }          } 

this function when call recursive one:

void list::insert( int d)     {          if( head == null)         {             head = new node;             head->data = d;         }         else         {         inserthelper(head, d);         }         } 

you problem absence of following:

list->next->next = null; 

in else part of inserthelper. "suggestions" part, avoid processing lists recursively if can it. (future) coworkers won't appreciate it.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -