C语言 空链表时为什么出现段错误,哪块代码有误?

img

img

#include <stdio.h>
#include <stdlib.h>
typedef struct Lnone
{
int date;
struct Lnone *next;
} Lnode, *linklist;
linklist createlist()
{
linklist L;
L = (linklist)malloc(sizeof(Lnode)); //建一个头结点
L->next = NULL;
linklist t = L; //建一个前指针指向头结点
do
{
linklist p;
p = (linklist)malloc(sizeof(Lnode)); //建一个新节点
p->next = NULL;
scanf("%d", &(p->date)); //给新节点数据域赋值
t->next = p; //新节点与前一结点连接
t = t->next; //前节点后移
} while (t->date != -1); //输入-1后结束
return L;
}
void mergelist(linklist La, linklist Lb, linklist Lc) //合并a,b链表为c
{
linklist a, b, c;
a = La->next;
b = Lb->next;
c = Lc;
while (a->date != -1 && b->date != -1) //一个一个比较,等-1参与比较时停止
{
if (a->date <= b->date) //谁小就先进入c
{
c->next = a;
c = a;
a = a->next;
}
if (a->date > b->date)
{
c->next = b;
c = b;
b = b->next;
}
}
c->next = (a->date == -1) ? b : a;
}
void printlist(linklist L)
{
linklist p = L->next;
if(p->date==-1)
printf("NULL");
while(p->next->date!=-1)
{
printf("%d ", p->date);
p = p->next;
}
printf("%d", p->date);
}

int main()
{
linklist La, Lb, Lc; // Lc是合并后的链表
Lc = (linklist)malloc(sizeof(Lnode)); //建立c链表的头结点
Lc->next = NULL;
La = createlist(); //构造a链表
Lb = createlist(); //构造b链表
mergelist(La, Lb, Lc);
printlist(Lc);
return 0;
}

你的想法可以,但是错就错在ab都有头节点,c没有,但是print的时候又按照有头节点来做。

修改处见注释,供参考:

#include <stdio.h>
#include <stdlib.h>
typedef struct Lnone
{
    int date;
    struct Lnone* next;
} Lnode, * linklist;
linklist createlist()
{
    linklist L;
    int indata;     //修改
    L = (linklist)malloc(sizeof(Lnode)); //建一个头结点
    L->next = NULL;
    linklist t = L; //建一个前指针指向头结点
    do
    {
        scanf("%d", &indata); //给新节点数据域赋值  修改
        if (indata == -1) break;  //输入-1后结束  修改
        linklist p;
        p = (linklist)malloc(sizeof(Lnode)); //建一个新节点
        p->next = NULL;
        p->date = indata;
        t->next = p; //新节点与前一结点连接
        t = t->next; //前节点后移
    } while (1);   //修改
    return L;
}
void mergelist(linklist La, linklist Lb, linklist Lc) //合并a,b链表为c
{
    linklist a, b, c;
    a = La->next;
    b = Lb->next;
    c = Lc;
    while (a && b)  //while (a->date != -1 && b->date != -1) //一个一个比较,等-1参与比较时停止 修改
    {
        if (a->date <= b->date) //谁小就先进入c
        {
            c->next = a;
            c = a;
            a = a->next;
        }
        else //if (a->date > b->date)   修改
        {
            c->next = b;
            c = b;
            b = b->next;
        }
    }
    c->next = a ? a : b;  //c->next = (a->date == -1) ? b : a; 修改
}
void printlist(linklist L)
{
    linklist p = L->next;
    if (!p) {      //if (p->date == -1)  修改
        printf("NULL");
        return;
    }
    while (p)     //while (p->next->date != -1)  修改
    {
        printf("%d ", p->date);
        p = p->next;
    }
    //printf("\n");  //printf("%d", p->date);
}

int main()
{
    linklist La, Lb, Lc; // Lc是合并后的链表
    Lc = (linklist)malloc(sizeof(Lnode)); //建立c链表的头结点
    Lc->next = NULL;
    La = createlist(); //构造a链表
    Lb = createlist(); //构造b链表
    mergelist(La, Lb, Lc);
    printlist(Lc);
    return 0;
}