recursion - C++ deep copy a linked list resursively -
i c++ beginner trying write function create deep copy of linked list in c++. function calls until @ last node in source list, copies node. when run segmentation fault or exc_bad_access error. here have far:
struct node { int data; node* next; }; void copy_list(const node*& source_ptr, node*& dest_ptr) { if (dest_ptr != nullptr){ clear_list(dest_ptr); } if (source_ptr == nullptr) return; //we cleared dest_ptr if (source_ptr->next == nullptr) // last node { dest_ptr = new node(); //initialize in memory dest_ptr->data = source_ptr->data; //copy last datum dest_ptr->next = nullptr; //since end return; } const node* cursor = source_ptr->next; // happens if source not yet @ end copy_list(cursor, dest_ptr->next); }
i know there other questions similar this, haven't helped me. have tried using other methods recursion example while loop looks like:
dest_ptr = new node(); dest_ptr->data = source_ptr->data; node* dest = dest_ptr->next; const node* cursor = source_ptr->next; while(cursor != nullptr) { dest = new() node; dest-> data = cursor->data; //dest->next = nullptr; dest = dest->next; cursor = cursor->next; }
the while loop doesn't give errors copy blank (except first node copied outside while loop).
any appreciated. thanks!
if you're beginner, start simple things: try avoid recursion until understand loops. comment on loop version (recursion bad approach particular problem anyway).
if code doesn't want, should try stepping through in debugger note does, or try explain list of instructions (a rubber duck ideal this, it's patient).
you can approach reasoning code:
each variable should have defined purpose, ideally reflected in name. can see purpose of source_ptr
point source list. , purpose of cursor
traverse source list.
dest_ptr
intended hold newly created copy. you're taking start copying first data
it.
what purpose of dest
, however? start copy value of dest_ptr->next
(which null) it. then, in loop, overwrite dest
newly created node. copy cursor->data
new node, , copy (uninitialised time) pointer dest->next
dest
. however, note never read value of dest
, overwrite in next iteration.
i suspect intended dest
pointer pointer node
, , intention this:
dest_ptr = new node(); dest_ptr->data = source_ptr->data; node **dest = &dest_ptr->next; const node *cursor = source->ptr->next; while (cursor) { *dest = new node(); (*dest)->data = cursor->data; dest = &((*dest)->next); cursor = cursor->next; }
this want, pointers pointers ugly. better use dest
second cursor traversing destination list:
dest_ptr = new node(); dest_ptr->data = source_ptr->data; node *dest = dest_ptr; const node *cursor = source_ptr->next; while (cursor) { dest->next = new node(); dest = dest->next; dest->data = cursor->data; cursor = cursor->next; }
Characteristics Of A Good Programming Languages
ReplyDeleteSubmit form without page reloading
Split database results into 3 equal columns - PHP
Dynamically Add/Delete HTML Table Rows
PHP Create Word Document from HTML
Retrieve Data From DB Without Page refresh
PHP Leap Year Program