-1

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;
}
Community
  • 1
  • 1
cosmoonot
  • 2,161
  • 3
  • 32
  • 38

1 Answers1

1

I believe the issue you're facing is due to the fact that at the end of the method, you set current->next to NULL.

Essentially, the line:

current->next = NULL;

removes all nodes from the LL except for the first one added.

If you remove this line, your code should work as expected.

Your code is using current to refer to the first node in the copy of the original list. current->next is supposed to point to the second node, and each node's next value should point to the node following it.

You'll also want to save list to a temporary variable, and iterate over that temporary variable in your method instead - that way you don't overwrite a global variable.

Finally, your method will be:

struct container* list_by_name()
{

        struct container *previous = NULL, *current = NULL, *tmp = list;


        while (tmp != 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, tmp->student->name);
                strcpy(tempStudentList->standard, tmp->student->standard);
                tempStudentList->absents = tmp->student->absents;

                // store student data into container
                tempMainContainer->student = tempStudentList;
                tempMainContainer->next = NULL;

                if (current == NULL) {
                        current = tempMainContainer;
                        previous = tempMainContainer;
                } else {
                        previous->next = tempMainContainer;
                        previous = tempMainContainer;
                }

                printf("%s\n", tempMainContainer->student->name);

                tmp = tmp->next;
        }

        return current;
}
Anish Goyal
  • 2,799
  • 12
  • 17