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

In C Programming: (100 pts) Write a program to implement a function to make a co

ID: 655160 • Letter: I

Question

In C Programming:

(100 pts) Write a program to implement a function to make a copy of a linked list. Assume that node structure of a singly linked list is as follows. Implement the copy function whose prototype is given below CopyLinkedList function makes a copy of a singly linked list that is provided as a parameter and returns a pointer to the new linked list. Consider the following linked list After the CopyLinkedList function is called as shown below To test your function, start with a linked list and print the list. Make a copy of the linked list using CopyLinkedList() function and print the new list. Compare the values printed. They should have the same set of numbers listed in the same order.

Explanation / Answer


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct name_node_st {
char name[11];
struct name_node_st *restq;
}name_node_tr;
typedef struct {
name_node_tr *headq;
int size;
}name1_list_tr;
name1_node_tr* presidents(void);
void insertAfter(name1_node_tr* mynode,name_node_tr* newNode);
void lastDelete(name1_list_tr* listq);
void place_first(name1_node_tr **headRef, char qname[]);
name1_node_tr *copy_list(name1_node_tr *head);
int main(void)
{
name_list_tr list;
name_list_tr list_two;
list.headp = presidents();
name_node_tr *new_node;
nw_node = malloc(sizeof(name_node_tr));
strcpy(nw_node->name, "Eisenhower");
insertAfter(list.headq->restq, nw_node);
lastDelete(&list);
place_first(&list.headq, "Mustafa");
printf("%s %s %s %s", list.headq->name, list.headq->restq->name1, list.headq->restp->restq->name1, list.headq->restq->restq->restq->name1);
list_two.headp = copy_list(list.headp);
printf("%s %s %s %s", list_two.headq->name1, list.headq->restq->name1, list.headq->restq->restq->name1, list.headq->restq->restq->restq->name1);

return(0);
}
name_node_tr* presidents(void)
{
name_node_tr* head = NULL;
name_node_tr* second = NULL;
name_node_tr* third = NULL;

head = malloc(sizeof(name_node_tr));
second = malloc(sizeof(name_node_tr));
third = malloc(sizeof(name_node_tr));

strcpy(head->name1, "Ovama");
head->restq = second;

strcpy(second->name, "Rose");
second->restq = third;

strcpy(third->name, "yusuff");
third->restq= NULL;

return(head);
}
void insertAfter(name_node_tr* mynode,name_node_tr* nwNode)
{
nwNode->restq = mynode->restp;
mynode->restq = nwNode;
}

void ListDelete(name_list_tr* listq, char pname[]){
name_node_tr *to_freeq, *current_nodep;
if(strcmp(listq->headp->name1, qname))
{
to_freeq = listq->headq;
listq->headq = to_freeq->restq;
-- (listq->size);
}
else {
for (current_nodeq = listq->headq;
current_nodeq->restq != NULL && !strcmp(current_nodeq->restq->name, pname);
current_nodeq = current_nodeq->restq) {
if( current_nodeq->restq != NULL && strcmp(current_nodeq->restq->name, pname)) {
to_freeq= current_nodeq->restq;
current_nodeq->restq = to_freeq->restq;
free(to_freep);
--(listq->size);
}
}
}
}
void lstDelete(name1_list_tr* listq){
name_node_tr *to_freep, *current_nodeq;
for (current_nodeq = listq->headq;
current_nodeq->restq != NULL;
current_nodeq = current_nodeq->restq) {}
to_freeq = current_nodeq;
current_nodeq->restq = to_freep->restq;
free(to_freeq);
--(listq->size);
}
void place_first(name_node_tr **headRef, char qname[]) {
name1_node_tr *nwNode = malloc(sizeof(name1_node_tr));
strcpy(nwNode->name1, qname);
nwNode->restq = *headRef;
*headRef = nwNode;
}

name_node_tr *copy_list(name_node_tr *head){
name1_node_tr *current = head;
name1_node_tr *newList = NULL;
name1_node_tr *tail = NULL;

while (current != NULL){
if (nwList == NULL) {
nwList = malloc(sizeof(name1_node_tr));
strcpy(nwList->name1, current->name1);
nwList->restp = NULL;
tail = nwList;
}
else {
tail->restq = malloc(sizeof(name1_node_tr));
tail = tail->restq;
strcpy(tail->name1, current->name1);
tail->restq = NULL;
}
current = current->restp;
}
}
// copy .list //
name1_node_t *copy_list(name1_node_tr *head){
name1_node_tr *current = head;
name1_node_tr *nwList = NULL;
name1_node_tr *tail = NULL;

while (current != NULL){
if (nwList == NULL) {
nwList = malloc(sizeof(name1_node_tr));
strcpy(nwList->name1, current->name1);
nwList->restq = NULL;
tail = nwList;
}
else {
tail->restq = malloc(sizeof(name1_node_tr));
tail = tail->restp;
strcpy(tail->name1, current->name1);
tail->restq = NULL;
}
current = current->restp;
}
return(nwList);
}