Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a function called copylist(), which makes a \"deep\" copy or complete copy

ID: 3790148 • Letter: W

Question

write a function called copylist(), which makes a "deep" copy or complete copy of one singly linked list of one singly linked list.the function should accept two parameters: a pointer to the source list and a pointer to the new list. you need to decide if the pointer to the new list is single indirection (*) or double indirection(**). the function should not return a value. note: a "deep" copy implies that new dynamic memory is allocated for each node in the copy list. you should not do the following:

newlist = sourcelist; //pseudcode

you may not assume that makeNode() function exists. A Node is defined as follows:

typedef struct node

{

char data[100];

struct node *pNext;

} Node;

be sure to fill in the function header!

void copylist (Node *pSourceList,

{

Explanation / Answer

HI, Please find my code.

Please let me know in case of any issue.


typedef struct node
{
char data[100];
struct node *pNext;
} Node;


be sure to fill in the function header!

void copylist (Node *pSourceList, Node **newlist)
{
   // base case
   if(pSourceList == NULL)
       return;

   Node *sTemp = pSourceList;
   Node *nTemp = NULL;
   while(sTemp != NULL){
       // creating new node
       Node* newNode = (Node *)malloc(sizeof(Node));
       strcpy(sTemp->data, newNode->data); // copying data value
       newNode->pNext = NULL;
       // first node from source
       if(*newlist == NULL){  
           *newlist = newNode; // initializing newlist with first node of sourcelist
           nTemp = *newlist; // pointing nTemp to first node of newlist
       }
       else{
           nTemp->pNext = newNode;
           nTemp = nTemp->pNext;// making nTemp point to last node in newlist
       }

       // forwarding sTemp
       sTemp = sTemp->pNext;
   }
}