Skip to content

Commit

Permalink
Fixed make_copy of the OO Linked List implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ramtung committed Jan 11, 2025
1 parent e5ab470 commit d389f32
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion 19_ImplementingContainers/02_OOLinkedListWithIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,16 @@ void List::delete_all() {
}

List::Node *List::make_copy(List::Node *p) {
return p ? new Node(p->data, p->prev, make_copy(p->next)) : NULL;
if (p != nullptr) {
Node *new_node = new Node(p->data);
new_node->next = make_copy(p->next);
if (new_node->next != NULL)
new_node->next->prev = new_node;
return new_node;
} else {
return NULL;
}
// return p ? new Node(p->data, p->prev, make_copy(p->next)) : NULL;
}

List::Iterator List::find(int x) {
Expand All @@ -159,6 +168,10 @@ int main() {
l1.push_front(43);
l1.push_front(12);

List copy = l1;
copy.print();
cout << endl;

List l2;
l2.push_back(-8);
l2.push_front(-56);
Expand Down

0 comments on commit d389f32

Please sign in to comment.