We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
for this function:
List::Node *List::make_copy(List::Node *p) { return p ? new Node(p->data, p->prev, make_copy(p->next)) : NULL;}
consider we have a linked list like this in heap: `1 0. address: prev | value | next
by calling function:
so in third stack frame : p = X003
return : new Node(p->data, p->prev, (return of fourth stack frame [null]) )
-->consider new memory block is : X103 : X002 | 30 | null
in second stack frame : p = X002
return : new Node(p->data, p->prev, (return of third stack frame [X103]) )
->consider new memory block is : X102 : X001 | 20 | X103
in first stack frame : p = X002
return : new Node(p->data, p->prev, (return of second stack frame [X102]) )
->consider new memory block is : X101 : null | 10 | X102
now heap is:
and as you see prev pointers of any node in second list are pointing to first list elements!!
I guess this can solve problem:
List::Node *List::make_copy(List::Node *p) { if( p == nullptr ) return nullptr; Node* tail = make_copy(p->next); Node* head = new Node(p->data, nullptr, tail); if(tail != nullptr) tail->prev = head; return head; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
for this function:
consider we have a linked list like this in heap:
`1
0. address: prev | value | next
by calling function:
so in third stack frame : p = X003
return : new Node(p->data, p->prev, (return of fourth stack frame [null]) )
in second stack frame : p = X002
return : new Node(p->data, p->prev, (return of third stack frame [X103]) )
in first stack frame : p = X002
return : new Node(p->data, p->prev, (return of second stack frame [X102]) )
now heap is:
and as you see prev pointers of any node in second list are pointing to first list elements!!
I guess this can solve problem:
The text was updated successfully, but these errors were encountered: