找了好久还是不懂为啥,为什么一个插入(CreateList1())输出就是图片这种情况,而(CreateList2())就可以输出成功,不是明明都一样吗?


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

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int ElemType;
typedef int Status;

typedef struct LNode {
    ElemType data;
    struct LNode *next;
} LNode, *LinkList;

//遍历
Status ListTraverse(LinkList L) {

    L = L->next;
    while (L != NULL) {
        printf("%3d", L->data);
        L = L->next;
    }
    printf("\n");
    return OK;
}



//尾插法(这个有问题)
LinkList CreateList1(LinkList &L) {
    int x;
    L = (LinkList)malloc(sizeof(LNode));
    LinkList j, m = L;
    scanf("%d", &x);
    while (x != 9999) {
        m = (LinkList)malloc(sizeof(LNode));
        m->data = x;
        j->next = m;
        j = m;
        scanf("%d", &x);
    }

    j->next = NULL;
    return L;
}


//这个没问题
LinkList CreateList2(LinkList &L) {
    int x;
    L = (LinkList)malloc(sizeof(LNode));
    LinkList s, r = L;
    scanf("%d", &x);
    while (x != 9999) {
        s = (LinkList)malloc(sizeof(LNode));
        s->data = x;
        r->next = s;
        r = s;
        scanf("%d", &x);
    }
    r->next = NULL;
    return L;
}

int main () {
    LinkList L;
    CreateList1(L);
    ListTraverse(L);

    return 0;

}

//显示结果

img

你把j换成s,m换成r,看看47行和65行一样?
另外建议换个智能点的IDE,可以更快找出错误,比如vs

明显的问题,取地址符号&是不能作为函数原型的参数的,只能用在实参传递

有问题的LinkList CreateList1(LinkList &L) 函数里,定义的局部变量 j 没有初始化,所以造成错误,其它没任何问题,见注释,供参考:

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

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int ElemType;
typedef int Status;

typedef struct LNode {
    ElemType data;
    struct LNode *next;
} LNode, *LinkList;

//遍历
Status ListTraverse(LinkList L) {

    L = L->next;
    while (L != NULL) {
        printf("%3d", L->data);
        L = L->next;
    }
    printf("\n");
    return OK;
}



//尾插法(这个有问题)
LinkList CreateList1(LinkList &L) {
    int x;
    L = (LinkList)malloc(sizeof(LNode));
    LinkList j = L, m = L;   //LinkList j, m = L; 修改
    scanf("%d", &x);
    while (x != 9999) {
        m = (LinkList)malloc(sizeof(LNode));
        m->data = x;
        j->next = m;
        j = m;
        scanf("%d", &x);
    }
    j->next = NULL;
    return L;
}
 

//这个没问题
LinkList CreateList2(LinkList &L) {
    int x;
    L = (LinkList)malloc(sizeof(LNode));
    LinkList s, r = L;
    scanf("%d", &x);
    while (x != 9999) {
        s = (LinkList)malloc(sizeof(LNode));
        s->data = x;
        r->next = s;
        r = s;
        scanf("%d", &x);
    }
    r->next = NULL;
    return L;
}
 
int main () {
    LinkList L1 ,L2;
    CreateList1(L1);
    ListTraverse(L1);

    CreateList1(L2);
    ListTraverse(L2);

    return 0;
}