Have a question about copying a linked list to another list in a container. At the moment, my code is copying data from the global list and storing them in a temporary list and that data is stored in "student" node of the container. However, the result returned from the function halts the program after showing the first student.
I'm assuming the pointer is losing reference? Would anyone be able to shed some light on this? It has been years since I last worked with linked list.
Current Input:
Tom
Jen
Ken
Current Output halts after showing the first name:
Ken
I followed this thread as a reference: C program to make a second copy of a linked list
struct container* list_by_name()
{
struct container *previous = NULL, *current = NULL;
while (list != NULL) {
struct container *tempMainContainer = (struct container *) malloc(sizeof(struct container));
struct student *tempStudentList = (struct student *) malloc(sizeof(struct student));
// copy all students over to the list
strcpy(tempStudentList->name, list->student->name);
strcpy(tempStudentList->standard, list->student->standard);
tempStudentList->absents = list->student->absents;
// store student data into container
tempMainContainer->student = tempStudentList;
if (current == NULL) {
current = tempMainContainer;
previous = tempMainContainer;
} else {
previous->next = tempMainContainer;
previous = tempMainContainer;
}
printf("%s\n", tempMainContainer->student->name);
list = list->next;
}
// set container next to NULL
current->next = NULL;
return current;
}