Question
in c++
struct listrec value; int struct listrec next Write a main) routine in which you create the following linked list 4 5 3 NULL and then insert another node (with the value: 6) between the first and second nodes in the above list. Write a function called deepcopy that takes a pointer to the start of a linked list and returns a pointer to the start of another linked list obtained via a deep copy. The inferface of this function is void deepcopy(listrec *oldlinked_list, listrec *&new;_ linked list) /l perform a deep copy from old linked list to new linked list In your main(), you may have void main) listrec *head old, *head new NULL head_old- (beginning memory address of the old linked list) deepcopy(head old, head new) /l print out the information in the linked list pointed by head_nevw
Explanation / Answer
Please find my code.
void deepcopy(listrec *old_linked_list, listrec *&new_linked_list) {
listrec *temp = NULL, *head = NULL;
// iterating old_linked_list
while(old_linked_list != NULL) {
// creating new node
temp = (listrec *)malloc(sizeof(listrec));
temp->value = old_linked_list->value;
temp->next = NULL;
//
if(head == NULL) {
head = temp;
new_linked_list = head;
}else{
head->next = temp;
head = head->next;
}
old_linked_list = old_linked_list->next;
}
}