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 listcpbadstruct llist ll
struct llist newlist
mallocsizeofstruct llist;
newlistfront llfront;
return newlist;
Our attempt to use it seems to work:
struct llist ll listcreate;
listinsertfrontll;
listinsertfrontll;
listinsertfrontll;
listinsertfrontll;
struct llist llcopy listcpbadll;
listprintll;
listprintllcopy;
cons cons cons cons empty
cons cons cons cons empty
Here is the memory diagram for llist ll:
Draw a memory diagram after
listcpbad executes.
We see that these lists share nodes. If we mutate nodes in ll we also mutate nodes in ulcopy; they are the same nodes.
listcpbad makes a shallow copy; it copies the values of the fields of tlist What we need is a deep copy.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
