Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.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_Dangling Pointer - Fatal编程技术网

在C中实现链表失败

在C中实现链表失败,c,linked-list,dangling-pointer,C,Linked List,Dangling Pointer,我不明白为什么这个程序不起作用,元素没有按预期插入列表 每次调试时,我都会看到,当我在“insert”方法之后转到main方法时,链表仍然是空的,我不理解为什么,因为我认为它应该是好的,因为我使用了指针(这看起来像是“悬空指针”的情况,但如果是,我不理解为什么) 也许我应该用双星(**)? 如果是,为什么在阵列中它不重要 以下是源代码: #include <stdio.h> #include <stdlib.h> struct A{ int val; s

我不明白为什么这个程序不起作用,元素没有按预期插入列表

每次调试时,我都会看到,当我在“insert”方法之后转到
main
方法时,链表仍然是空的,我不理解为什么,因为我认为它应该是好的,因为我使用了指针(这看起来像是“悬空指针”的情况,但如果是,我不理解为什么)

也许我应该用双星(**)? 如果是,为什么在阵列中它不重要

以下是源代码:

#include <stdio.h>
#include <stdlib.h>

struct A{
    int val;
    struct A* next;
} A;

void insert(struct A* L, int newVal) {

    if (L == NULL) {

        L = (struct A*) malloc(sizeof(struct A));
        L->val = newVal;
        L->next = NULL;
    }

    else {

        struct A* p = L;

        while (p->next != NULL) {

            p = p->next;
        }

        p->next = (struct A*) malloc(sizeof(struct A));
        p->next->val = newVal;
        p->next->next = NULL;
    }
}


void printA(struct A* printed) {

    struct A* p = printed;

    while (p != NULL) {
        printf("%d\n", p->val);
        p = p->next;
    }
}


int main() {

    struct A* L = NULL;

    insert(L, 1);
    printf("1 success\n");

    insert(L, 2);
    printf("2 success\n");

    insert(L, 3);
    printf("3 success\n");

    insert(L, 4);
    printf("4 success\n");

    insert(L, 5);
    printf("5 success\n");

    printf("\n\n\n");

    printA(L);

    return 0;
}
#包括
#包括
结构A{
int-val;
结构A*下一步;
}A;
空插入(结构A*L,int newVal){
if(L==NULL){
L=(结构A*)malloc(结构A的大小);
L->val=newVal;
L->next=NULL;
}
否则{
结构A*p=L;
while(p->next!=NULL){
p=p->next;
}
p->next=(结构A*)malloc(sizeof(结构A));
p->next->val=newVal;
p->next->next=NULL;
}
}
无效打印A(结构A*打印){
结构A*p=打印;
while(p!=NULL){
printf(“%d\n”,p->val);
p=p->next;
}
}
int main(){
结构A*L=NULL;
插入(L,1);
printf(“1成功\n”);
插入(1,2);
printf(“2成功\n”);
插入(L,3);
printf(“3成功\n”);
插入(L,4);
printf(“4成功\n”);
插入(L,5);
printf(“5成功\n”);
printf(“\n\n\n”);
printA(L);
返回0;
}

谢谢。

插入函数的第一个参数是指向结构的指针。传递结构时,
insert
接收地址,并创建指向同一位置的本地指针。为了更改实际结构(来自main)指向的对象,必须传递一个双指针

以下是需要更改的部分:

void insert(struct A** L, int newVal) {

    if (*L == NULL) {

        *L = (struct A*) malloc(sizeof(struct A));
        (*L)->val = newVal;
        (*L)->next = NULL;
    }

    else {

        struct A* p = *L;

        ...
        ...
        ...
    }
}    

int main() {

    struct A* L = NULL;

    insert(&L, 1);
    printf("1 success\n");

    ...
    ...
    ...

    printA(L);

    return 0;
}
另一种方法是使用单个指针,但将
insert
的返回值更改为
struct A*
。您只需将返回值分配给主结构,如下所示:

struct A *insert(struct A* L, int newVal) {

    if (L == NULL) {

        L = (struct A*) malloc(sizeof(struct A));
        L->val = newVal;
        L->next = NULL;

        return L;
    }

    else {
        ...
    }

    return L;
}

int main() {

    struct A* L = NULL;

    L = insert(L, 1);
    ...
    return 0;
}
此外,打印功能不会移动到任何地方。添加行
p=p->next

void printA(struct A* printed) {

    struct A* p = printed;

    while (p != NULL) {
        printf("%d\n", p->val);
        p = p->next;
    }
}

您总是将
NULL
传递给
insert()
。请参见,当传递指针时,要在子函数中修改的内容,则必须传递指针的地址,而不是指针的内容,就像传递希望子函数修改的任何其他变量的地址一样。非常感谢!