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

struct person { char *name; int age; struct person * next; // We\'ve added a \"n

ID: 3750663 • Letter: S

Question

struct person {

   char *name;

int age;

struct person * next; // We've added a "next" pointer.

};

struct linkedList { // We are now packaging the two pointers into a struct

struct person *front;

struct person *rear;

};

void print (stuct person *p)

{

printf ("Name=%s age=%d ", p->name, p->age);

}

struct person* makeNode (char* name, int age)

{

struct person *p;

p = (struct person*) malloc (sizeof(struct person));

p->name = name;

p->age = age;

p->next = NULL;

return p;

}

struct linkedList initList ()

{

struct linkedList* L;

L = (struct linkedList*) malloc (sizeof(struct linkedList));

L -> front = NULL;

L -> rear = NULL;

return L;

}

void addToList (struct inkedList* L, char* name,int age)

{

if (L->front == NULL) {

L->front = makeNode (name,age);

L->rear = L-> front;

}

else {

L-> rear-> next = makeNode (name, age);

L-> rear = L->rear->next;

}

}

void printLisr (struct linkedList* L)

{

struct person *p;

  

// WRITE YOUR CODE HERE

}

struct linkedList* copy (struct linkedList* L)

{

struct person *p;

struct linkedList *L2;

  

//WRITE YOUR CODE HERE

}

int main ()

{

struct linkedList *L1, *L2;

  

L1 = initList ();

addToList (L1, "R2-D2", 609);

addToList (L1, "Optimus Prime", 2700);

addToList (L1, "Wall-E", 210);

printList (L1);

  

// Wrong way to copy

L2=L1;

addToList (L2, "Hal-9000", 2);

printList (L1);

  

/*

better:

L2 = copy (L1);

addToList (L2, "T-1000", 30);

printList (L2);

*/

}

3.9 - complete the required methods. For the moment, the copy () method can remain not implemented.

3.10 - implement the copy() method, ensuring that a deep copy is made, so that every person struct is duplicated. Un-comment the block comments in main () add all this code to the code gicen above

Explanation / Answer



Given below is the completed code for the question.
Please do rate the answer if it was helpful. Thank you

#include <stdio.h>
#include <stdlib.h>
struct person {
char *name;
int age;
struct person * next; // We've added a "next" pointer.
};
struct linkedList { // We are now packaging the two pointers into a struct
struct person *front;
struct person *rear;
};
void print (struct person *p)
{
printf ("Name=%s age=%d ", p->name, p->age);
}
struct person* makeNode (char* name, int age)
{
struct person *p;
p = (struct person*) malloc (sizeof(struct person));
p->name = name;
p->age = age;
p->next = NULL;
return p;
}
struct linkedList* initList ()
{
struct linkedList* L;
L = (struct linkedList*) malloc (sizeof(struct linkedList));
L -> front = NULL;
L -> rear = NULL;
return L;
}
void addToList (struct linkedList* L, char* name,int age)
{
if (L->front == NULL) {
L->front = makeNode (name,age);
L->rear = L-> front;
}
else {
L-> rear-> next = makeNode (name, age);
L-> rear = L->rear->next;
}
}
void printList (struct linkedList* L)
{
struct person *p;
// WRITE YOUR CODE HER
p = L->front;
while(p != NULL){
print(p);
p = p->next;
}
}
struct linkedList* copy (struct linkedList* L)
{
struct person *p;
struct linkedList *L2;
//WRITE YOUR CODE HERE
L2 = initList();
p = L->front;
while(p != NULL){
addToList(L2, p->name, p->age);
p = p->next;
}
return L2;
}
int main ()
{
struct linkedList *L1, *L2;
L1 = initList ();
addToList (L1, "R2-D2", 609);
addToList (L1, "Optimus Prime", 2700);
addToList (L1, "Wall-E", 210);
printList (L1);
// Wrong way to copy
/*
L2=L1;
addToList (L2, "Hal-9000", 2);
printList (L1);
*/

// better:

L2 = copy (L1);

addToList (L2, "T-1000", 30);

printList (L2);

}
output
-------
Name=R2-D2 age=609
Name=Optimus Prime age=2700
Name=Wall-E age=210
Name=R2-D2 age=609
Name=Optimus Prime age=2700
Name=Wall-E age=210
Name=T-1000 age=30