注册 登录
编程论坛 数据结构与算法

关于链表创建无错误但运行程序终止

JJ是也 发布于 2012-05-19 21:56, 344 次点击
关于链表创建无错误但运行程序终止
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define GoodType int

typedef struct Good
{
    GoodType num;
    struct Good *next;
}LGood, *LinkedGood;

LinkedGood GoodInit (LinkedGood L)
{
    L = (LGood*) malloc (sizeof(LGood));
    if (L==NULL)
    {
        printf ("没有足够内存空间!");
        exit (0);
    }
    L->next = NULL;
    return L;
}

LinkedGood GoodInsert (LinkedGood L, GoodType e)
{
    LGood* p;
    LGood* s;
    p = L;
    while (p->next)
    {
        p = p->next;
    }
    s = (LGood*) malloc (sizeof(LGood));
    s->num = e;
    s->next = p->next;
    p->next = s;
    return L;
}

LinkedGood GoodPrintNum (LinkedGood  L)
{
    LGood* p;
    p = L->next;
    while (p!=NULL)
    {
        printf ("%d ", p->num);
        p = p->next;
    }
    return L;
}

void GoodRelease (LinkedGood L)
{
    LGood* p;
    LGood* s;
    p = L;
    if (p==NULL)
    {
        printf ("空表!");
        exit (0);
    }
    while (p!=NULL)
    {
        s = p;
        p = p->next;
        free (s);
    }
}

void main ()
{

    LinkedGood L;

    GoodInit (L);
    GoodInsert (L, 10);
    GoodPrintNum (L);
    GoodRelease (L);

}
3 回复
#2
JJ是也2012-05-19 21:57
补充一下。。。我试过断点调试。程序运行到LinkedGood GoodInsert 函数就终止了。。
#3
寒风中的细雨2012-05-20 07:48
程序代码:
void main ()

 {

    LinkedGood L;

     L = GoodInit (L);
     GoodInsert (L, 10);
     GoodPrintNum (L);
     GoodRelease (L);

}
#4
JJ是也2012-05-20 16:59
回复 3楼 寒风中的细雨
谢谢
1