为啥我这个链表输不进去数据就结束了

img

img


照着C primer plus上写的,稍微修改了一些,如果不把主函数里三个指针初始化一下,VS一直会报错

初始化函数() 和 建立链表函数() 里,形参都是值传递的,函数里做的工作,都没有反馈给它的上一级,工作都白做了。修改如下,供参考:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int data;
    struct Node* next;
};
void init_linklist(struct Node** p) //修改 *p
{
    (*p) = (struct Node*)malloc(sizeof(struct Node));//修改
    (*p)->next = NULL;                               //修改
}
void create_linklist(struct Node** p1) //,struct Node* p2,struct Node* p3) //修改
//p1为头指针,p2为申请的新指针
{
    struct Node* p2 = NULL, * p3 = NULL; //修改
    int flag = 1, i = 1;
    while (flag != 0)
    {
        init_linklist(&p2);
        if ((*p1) == NULL)
        {
            (*p1) = p2;
        }
        else
        {
            p3->next = p2;
        }
        p3 = p2;
        printf("请输入数据:");
        scanf("%d", &p2->data );  //scanf("%d", p2->data );//修改
        printf("\n是否继续输入:[1.继续] [0.退出]");
        scanf("%d", &flag);
        if (flag == 0)
        {
            p2->next = NULL;
            break;
        }
    }
}

void print(struct Node* L)
{
    struct Node* p = L;
    while (p){
        printf("%d ",p->data);
        p = p->next;
    }
    printf("\n");
}
int main()
{
    struct Node* phead=NULL;
    struct Node* current=NULL;
    struct Node* pmove=NULL;
    create_linklist(&phead);    //, current, pmove); //修改
    print(phead);
    return 0;
}

phead你没有初始化啊
而且初始化函数也是错的,根本无法实现对外部指针p的空间分配的

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int data;
    struct Node* next;
};
void init_linklist(struct Node* p)
{
    p = (struct Node*)malloc(sizeof(struct Node));
    p->next = NULL;
}
void create_linklist(struct Node* p1,struct Node* p2,struct Node* p3)//p1为头指针,p2为申请的新指针
{
    int flag = 1, i = 1;
    init_linklist(p2);
    while (flag != 0)
    {
        if (p1 == NULL)
        {
            p1 = p2;
        }
        else
        {
            p3->next = p2;
        }
        printf("请输入数据:");
        scanf("%d", p2->data );
        printf("\n是否继续输入?");
        scanf("%d", &flag);
        if (flag == 0)
        {
            p2->next = NULL;
            break;
        }
        p3 = p2;
    }
}
int main()
{
    struct Node* phead=NULL;
    struct Node* current=NULL;
    struct Node* pmove=NULL;
    create_linklist(phead, current, pmove);
    return 0;
}

你整个过程中,好像不明白指针的用法哦
就拿init_linklist 参数不能就单纯的指针,你那样是创建了一个野指针,而不是p2
我简单给你改下,有问题我们再看

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct Node
{
    int data;
    struct Node* next;
};
void init_linklist(struct Node** p)
{
    *p = (struct Node*)malloc(sizeof(struct Node));
    *p->next = NULL;
}
void create_linklist(struct Node** p1,struct Node** p2,struct Node** p3)//p1为头指针,p2为申请的新指针
{
    int flag = 1, i = 1;
    init_linklist(p2);
    while (flag != 0)
    {
        if (*p1 == NULL)
        {
            *p1 = *p2;
        }
        else
        {
            *p3->next = *p2;
        }
        printf("请输入数据:");
        scanf("%d", *p2->data );
        printf("\n是否继续输入?");
        scanf("%d", &flag);
        if (flag == 0)
        {
            *p2->next = NULL;
            break;
        }
        *p3 = *p2;
    }
}
int main()
{
    struct Node* phead=NULL;
    struct Node* current=NULL;
    struct Node* pmove=NULL;
    create_linklist(&phead, &current, &pmove);
    return 0;
}