Question: Make a memory diagram. Copying lists ` ` ` The following code tries to copy a list: struct llist * list _ cp _ bad

Make a memory diagram. Copying lists
```
The following code tries to copy a list:
struct llist *list_cp_bad(struct llist *ll){
struct llist *newlist =
malloc(sizeof(struct llist));
newlist->front = ll->front;
return newlist;
}
Our attempt to use it seems to work:
struct llist *ll = list_create();
list_insert_front(ll,7);
list_insert_front(ll,5);
list_insert_front(ll,3);
list_insert_front(ll,10);
struct llist *ll_copy = list_cp_bad(ll);
list_print(ll);
list_print(ll_copy);
->(cons 10(cons 3(cons 5(cons 7 empty))))
->(cons 10(cons 3(cons 5(cons 7 empty))))
```
Here is the memory diagram for llist *ll:
Draw a memory diagram after
list_cp_bad executes.
We see that these lists share nodes. If we mutate nodes in ll , we also mutate nodes in ul_copy; they are the same nodes.
list_cp_bad makes a shallow copy; it copies the values of the fields of tlist . What we need is a deep copy.
Make a memory diagram. Copying lists ` ` ` The

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!