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
Post a Comment