Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中将链表复制到另一个列表_C_Linked List - Fatal编程技术网

在C语言中将链表复制到另一个列表

在C语言中将链表复制到另一个列表,c,linked-list,C,Linked List,对将链接列表复制到容器中的另一个列表有疑问。目前,我的代码正在从全局列表复制数据,并将其存储在临时列表中,该数据存储在容器的“student”节点中。但是,函数返回的结果会在显示第一个学生后停止程序 我假设指针失去了引用?有人能解释一下吗?自从我上一次使用链表已经有好几年了 当前输入: 汤姆 珍 肯 显示名字后,当前输出停止: 肯 我遵循这条线索作为参考: 我相信您面临的问题是由于在方法末尾,您将current->next设置为NULL 基本上,这条线: current->next =

对将链接列表复制到容器中的另一个列表有疑问。目前,我的代码正在从全局列表复制数据,并将其存储在临时列表中,该数据存储在容器的“student”节点中。但是,函数返回的结果会在显示第一个学生后停止程序

我假设指针失去了引用?有人能解释一下吗?自从我上一次使用链表已经有好几年了

当前输入:

汤姆

显示名字后,当前输出停止:


我遵循这条线索作为参考:



我相信您面临的问题是由于在方法末尾,您将
current->next
设置为NULL

基本上,这条线:

current->next = NULL;
从LL中删除除第一个添加的节点外的所有节点

如果删除这一行,代码应按预期工作

您的代码正在使用
current
引用原始列表副本中的第一个节点
current->next
应该指向第二个节点,每个节点的下一个值应该指向它后面的节点

您还需要将列表保存到一个临时变量,并在方法中迭代该临时变量,这样就不会覆盖全局变量

最后,您的方法是:

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;
}

你能把剩下的代码发出去吗?当然。很长,让我创建一个代码笔帖子。或者我可以给您发送一条带有代码的消息吗?我认为codepen最好是以防其他人稍后引用此问题?您的问题是函数的返回值,还是代码中printf语句的控制台输出?让我们来看看。我已经删除了current->next=NULL;问题仍然存在。函数中的printf语句正在打印正确的值,对吗?是,printf值正确。虽然循环正在工作,但我假设存在引用问题?
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;
}