Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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:Can';t链接带有外部功能的双链接列表中的头和尾[解决问题]_C_List_Double - Fatal编程技术网

C:Can';t链接带有外部功能的双链接列表中的头和尾[解决问题]

C:Can';t链接带有外部功能的双链接列表中的头和尾[解决问题],c,list,double,C,List,Double,我简化了我的双链接列表。我的双链表是一个以头和尾为节点的结构 有一个函数可以创建列表并返回它。在相同的函数中,我在尾部和头部节点之间进行链接。问题是,当我返回列表时(因此转到函数外部),所有链接都消失了,或者它们只是指向在函数中临时创建的列表的节点。我的猜测正确吗?如果是这样,我将如何绕过这个问题 代码如下: #include <stdio.h> typedef struct node{ /*a node of a list*/ int number;

我简化了我的双链接列表。我的双链表是一个以头和尾为节点的结构

有一个函数可以创建列表并返回它。在相同的函数中,我在尾部和头部节点之间进行链接。问题是,当我返回列表时(因此转到函数外部),所有链接都消失了,或者它们只是指向在函数中临时创建的列表的节点。我的猜测正确吗?如果是这样,我将如何绕过这个问题

代码如下:

#include <stdio.h>

typedef struct node{        /*a node of a list*/
    int number;
    struct node *next;
    struct node *prev;
} node;

typedef struct list{    /*the list structure that holds only the head and tail*/
    node head;
    node tail;
} list;

list createList(){
    list newList;
    newList.head.prev=NULL;
    newList.head.next=&newList.tail; /*first node points to the second*/
    newList.tail.prev=&newList.head; /*second node points to the first*/
    newList.tail.next=NULL;
    puts("--CREATE LIST FUNC--");
    printf("Head element address: %p\n", &newList.head);
    printf("Tail element address: %p\n", &newList.tail);
    printf("Head element points here: %p\n\n\n", newList.head.next);
    return newList;
}

int main(){
    list numbers=createList();
    puts("--MAIN FUNC--");
    printf("Head element address: %p\n", &numbers.head);
    printf("Tail element address: %p\n", &numbers.tail);
    printf("Head element points here: %p\n", numbers.head.next);
    return 0;
}
#包括
typedef结构节点{/*列表的节点*/
整数;
结构节点*下一步;
结构节点*prev;
}节点;
typedef struct list{/*只包含头和尾的列表结构*/
节点头;
节尾;
}名单;
列表createList(){
列表新建列表;
newList.head.prev=NULL;
newList.head.next=&newList.tail;/*第一个节点指向第二个节点*/
newList.tail.prev=&newList.head;/*第二个节点指向第一个节点*/
newList.tail.next=NULL;
puts(“--CREATE LIST FUNC-->”);
printf(“头元素地址:%p\n”,&newList.Head);
printf(“尾部元素地址:%p\n”,&newList.Tail);
printf(“Head元素指向此处:%p\n\n\n”,newList.Head.next);
返回newList;
}
int main(){
列表编号=createList();
puts(“--MAIN FUNC-->”);
printf(“头元素地址:%p\n”、&numbers.Head);
printf(“尾部元素地址:%p\n”、&numbers.Tail);
printf(“此处的头部元素点:%p\n”,编号.Head.next);
返回0;
}

您需要使用动态内存分配(
malloc()
free()
),因为您无法按值返回新创建的列表项。

您的猜测是正确的<代码>新建列表在函数结束时超出范围。函数返回
列表
对象的副本,但指针成员仍将指向原始对象


您需要在堆上分配一个
列表
,并通过指针返回(记住在某个点释放内存),或者将指向-
列表
的指针作为参数,并修改调用者拥有的
列表。

您不能在函数内将列表创建为
列表新列表,因为它位于堆栈上。您需要返回指向列表的指针。一旦函数返回,堆栈上的变量就不能保证存在

list* createList(){
    list* newList = (list*)malloc(sizeof(list));
    newList->head.prev=NULL;
    newList->head.next=newList->tail; /*first node points to the second*/
    newList->tail.prev=newList->head; /*second node points to the first*/
    newList->tail.next=NULL;
    puts("--CREATE LIST FUNC--");
    printf("Head element address: %p\n", newList->head);
    printf("Tail element address: %p\n", newList->tail);
    printf("Head element points here: %p\n\n\n", newList->head.next);
    return newList;
}

你可以;问题是编译器只执行浅层复制。@不,在这种情况下不能,因为变量的地址将更改。是的,这是一个问题,因为浅层复制!哎呀。这是C而不是C++。固定的。