Question: struct person { char *name; int age; struct person * next; // We've added a next pointer. }; struct linkedList { // We are now
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
