我的代码为何发生错误


#include<stdio.h>
#include<stdlib.h>
typedef struct link
{
    int data;
    struct link *pNext;
}Node,*PNode;

PNode creat()
{
    PNode L=NULL;
    L=(PNode)malloc(sizeof(Node));
    if(!L)
        return 0;
    L->pNext=NULL;
    return L;
}

void Insert(PNode L,int e)
{
    PNode pcur=NULL,pnew=NULL,prear=NULL;
    pnew=(PNode)malloc(sizeof(Node));
    if(!pnew)
        pnew=NULL;
    pnew->pNext=NULL;
    pnew->data=e;
    if(L->pNext=NULL)
        L->pNext=pnew;
    else
    {
        pcur=L;
        prear=pcur->pNext;
        while(prear->data<=pnew->data)
        {
            pcur=pcur->pNext;
            prear=pcur->pNext;
            if(prear==NULL)
                break;
        }
        pnew->pNext=prear;
        pcur->pNext=pnew;
    }
}
void print(PNode L)
{
    PNode p=NULL;
    p=L->pNext;
    while(p)
    {
        printf("%d->",p->data);
        p=p->pNext;
    }
    printf("\n");
}
void destroy(PNode L)
{
    PNode p=NULL;
    while(L)
    {
        p=L->pNext;
        L=L->pNext;
        free(p);
        L=p;
    }
}
int main()
{
    PNode Lz=NULL,Lf=NULL;
    int a;
    Lz=creat();
    Lf=creat();
    do
    {
        scanf("%d",&a);
        if(a>0)
            Insert(Lz,a);
        else
            Insert(Lf,a);
    }while(getchar()==' ');

    print(Lz);
    print(Lf);

    destroy(Lz);
    destroy(Lf);
    return 0;
}

该程序实现了一个链表的插入和输出操作,将数据根据正负号分别插入到两个链表中,并最终输出这两个链表的内容。

在插入过程中,使用带头结点的链表进行实现,新建节点后按数据大小顺序插入到链表中合适的位置。输出时遍历链表,依次输出每个节点的数据。

程序中存在一个错误:在判断是否为NULL时,Insert函数的if语句用的是"="赋值符号,应该使用"=="等于号进行判断。

正确的写法是修改条件判断语句:

if(L->pNext==NULL)
    L->pNext=pnew;

同时程序最后没有释放链表节点占用的内存空间,需要加上销毁链表的函数destroy来释放这些内存。

if(L->pNext=NULL)
->
if(L->pNext==NULL)

错误发出来看看啊

代码修改如下,改动处见注释,供参考:

#include<stdio.h>
#include<stdlib.h>
typedef struct link
{
    int    data;
    struct link* pNext;
}Node, * PNode;

PNode creat()
{
    PNode L = NULL;
    L = (PNode)malloc(sizeof(Node));
    if (!L)
        return 0;
    L->pNext = NULL;
    return L;
}

void Insert(PNode L, int e)
{
    PNode pcur = NULL, pnew = NULL, prear = NULL;
    pnew = (PNode)malloc(sizeof(Node));
    if (!pnew) {
        pnew = NULL;
        return;        // 修改
    }
    pnew->pNext = NULL;
    pnew->data = e;
    if (L->pNext == NULL)  //if(L->pNext = NULL)   // 修改
        L->pNext = pnew;
    else
    {
        pcur = L;
        prear = pcur->pNext;
        while (prear && prear->data <= pnew->data)
        {
            pcur = prear;        //pcur = pcur->pNext;  // 修改
            prear = prear->pNext;//prear = pcur->pNext; // 修改
                                //if (prear == NULL)     // 修改 
                                //    break;               // 修改
        }
        pnew->pNext = pcur->pNext; //pnew->pNext = prear; // 修改
        pcur->pNext = pnew;
    }
}
void print(PNode L)
{
    PNode p = NULL;
    p = L->pNext;
    while (p)
    {
        printf("%d->", p->data);
        p = p->pNext;
    }
    printf("\n");
}
void destroy(PNode L)
{
    PNode p = NULL;
    while (L)
    {
        p = L; //p = L->pNext;  // 修改
        L = L->pNext;
        free(p);
        //L = p;   // 修改
    }
}
int main()
{
    PNode Lz = NULL, Lf = NULL;
    int a;
    Lz = creat();
    Lf = creat();
    do
    {
        scanf("%d", &a);
        if (a > 0)
            Insert(Lz, a);
        else
            Insert(Lf, a);
    } while (getchar() == ' ');

    print(Lz);
    print(Lf);

    destroy(Lz);
    destroy(Lf);
    return 0;
}