Linked List Question Create a function called make_copy which returns an exact c
ID: 3690834 • Letter: L
Question
Linked List Question Create a function called make_copy which returns an exact copy of the linked list which is pointed to by the pointer variable 'list'. The function protoype for make_copy is: *Copylist make_copy (NodePointer *list); You must write the code to create a new node as well. Below is your structure definition for 'node'. typedef struct node { int value; struct node *next; } Node, *NodePointer, *CopyList; Write a function to combine two linked list named 'list1' and 'list2' which combines two linked lists containing numbers in ascending order, into one linked list called 'combined_list'. If the same value appears in both of the linked list only copy it once into the linked list. If your function is passed list1 containing values (0,1,3,4,5,6)and Iist2 containing values (0,1,3,4,5,6) the combined-list should contain (0,1, 2, 3,4, 5, 6,8,10,12,13). The function should alter the original lists. If both lists are empty the function should return an empty list and print the message "both lists are empty" to the console. The node structure definition is found below, typedef struct node { int info; struct node *link; } Node, * NodePointer;Explanation / Answer
Solution: See the code below:
1. make_copy() function:
----------------------------------------------------------
typedef struct node{
int value;
struct node *next;
} Node, NodePointer, CopyList;
CopyList* make_copy(NodePointer *list)
{
NodePointer *curr;
CopyList *copylist=NULL,*copy_curr;
if(list==NULL) //check if list is empty
return NULL;
curr=list; //initialized with head node of list
CopyList *copylist=NULL; //head node of copylist
copylist=(CopyList*)malloc(sizeof(CopyList)); //allocate memory for head node in copy list
copylist->value= list->value; //copy value of head node of list to head node of copylist
copylist->next=NULL;
//current node in copylist
copy_curr=copylist;
//copy values from list to
while(curr->next!=NULL)
{
CopyList* new_node=(CopyList*)malloc(sizeof(CopyList)); //allocate memory for new node in copylist
new_node->value= curr->next->value; //copy value of next node of list to new node of copylist
new_node->next=NULL;
//attach new node to copylist
copy_curr->next=new_node;
copy_curr=copy_curr->next; //sets to next node in copylist
curr=curr->next; //set to next node in list
}
return copylist;
}
-------------------------------------------------------------
2. combine_list() function:
--------------------------------------------------------------
typedef struct linkednode {
int info;
struct node *link;
} Node1, NodePointer1;
NodePointer1* combine_lists(NodePointer1 *list1, NodePointer1 *list2)
{
NodePointer1* combine_list = NULL; //head node of combined list
//check if any of list1 or list2 is empty.
if (list1 == NULL)
return(list2);
else if (list2==NULL)
return(list1);
//combine lists
if (list1->info <= list2->info)
{
combine_list = list1;
combine_list->link = combine_lists(list1->link, list2);
}
else
{
combine_list = list2;
combine_list->link = combine_lists(list1, list2->link);
}
return combine_list; //returns combine_list
}
-----------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.