Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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_Insert_Null_Linked List - Fatal编程技术网

C程序不能识别空指针

C程序不能识别空指针,c,insert,null,linked-list,C,Insert,Null,Linked List,我正试图在C中逆向实现一个插入函数,但我遇到了各种各样的问题。我在一个介绍C的课上,他们在我们使用C实验室之前就开始向我们扔C实验室。问题的一部分是列表指针没有被识别为NULL,我也非常确定我使用malloc的方式不正确 #include <stdio.h> #include <stdlib.h> #define True 1 #define False 0 typedef int BOOLEAN; struct Node{ int value; struct Nod

我正试图在C中逆向实现一个插入函数,但我遇到了各种各样的问题。我在一个介绍C的课上,他们在我们使用C实验室之前就开始向我们扔C实验室。问题的一部分是列表指针没有被识别为NULL,我也非常确定我使用malloc的方式不正确

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

#define True 1
#define False 0
typedef int BOOLEAN;

struct Node{
int value;
struct Node *next;
};

void insert(int x, struct Node **pL){
printf("insert\n");
if(*pL == NULL){
    printf("inside if\n");
    struct Node *pN;
    pN = (struct Node*) malloc(sizeof(struct Node));
    (*pN).value = x;
    (*pN).next = NULL;
    return;
}
if (*pL != NULL){
    printf("inside else\n");
    insert(x, &(((*pL)->next)));
}
printf("end insert\n");
};

void printList(struct Node *L){
while (L != NULL){
printf("%d", (*L).value);
printList((*L).next);
}
return;
};

main(){
printf("main\n");
struct Node* L;
//L).next = NULL;
int i;
printf("for loop\n");
for (i = 3; i < 20; i+=2){
    printf("%d\n", i);
    insert(i, &L);
}
printList(L);
};
首先,主要需要初始化L:

其次,在insert中,当您分配新节点pN时,您没有将其分配给pL,也就是说,它没有被插入。把这个放在返回之前;插入:

如果*pL!=空到其他

然后,在printList中,使用while循环和递归进行迭代。选择一个,而不是两个,例如:

while (L) {
    printf("%d\n", L->value);
    L = L->next;
}

此外,在整个代码中,您可以使用指向结构的指针->字段替换指向结构的指针,以获得更好的样式。

struct Node*L=NULL;可能会解决一些问题我已经试过了,它只是推迟了无法解决的问题。由于它会逆向地回忆,最终它到达了需要检查NULL的点,所有的东西都会下地狱。为了节省我们阅读所有东西的时间,问题到底出在哪里?哪个函数?插入函数,第一个if语句哦,我明白了-你创建了pn,但没有把它放在列表中
*pL = pN;
while (L) {
    printf("%d\n", L->value);
    L = L->next;
}