+91 9945200008     support@uttaracomputers.com

Question regarding group query continued



Hello Sir,


Thanks for your email regarding group_copy. I tried to code the same way
Kindly can u please check if this looks good





struct Person {
char* name;
struct Person * favorite;

struct Team {
char* name;
struct PersonNode* head;
struct PersonNode* tail;

struct PersonNode {
struct Person* value;
struct PersonNode* next;
struct PersonNode* prev;

char* _strdup1(char* s) {
return strcpy(malloc(sizeof(*s) * (strlen(s) + 1)), s);

struct Team copy_team(struct Team* team){
//makes a deep copy of a team and returns a returns a struct Team object.
//Every struct Person the copy refers to (including the .name field of each)
//will be a new copy on the heap.
struct Team team_copy;
struct PersonNode* personnode_copy;
struct Person *person_copy;

if (team == NULL) {
// team_copy = NULL;

team_copy.head = NULL;
team_copy.tail = NULL;
team_copy.name = _strdup1(team->name);

team_copy.head = team->head;

while (team->tail != NULL)
personnode_copy = malloc(sizeof(struct PersonNode));
personnode_copy -> value = malloc(sizeof(struct Person));
personnode_copy -> value -> name = _strdup1(team->head->value->name);
personnode_copy -> value -> favorite = _strdup1(team->head->value->favorite);
team_copy.tail -> next = personnode_copy;
team_copy.tail -> next -> prev = team->tail;
team_copy.tail = team->tail -> next;
return team_copy;


  • You must to post comments

Sorry for the delay in posting the answer.  Let me not be so lazy in the future.

The deep copy code that you have given looks alright except that I would like to suggest a small change:

Use a pointer instead of a structure in the stack as follows:

struct Team * team_copy;


team_copy = malloc ( sizeof (struct Team) );

team_copy->head = NULL;


I have not, of course, gone through the code line by line.  You will have to check that.


MLS Shastry

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.