I first created a linear linked list using initlist, and then used listlength to return to 0, but there was a segmentation fault when debugging. Why? the code is here
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
int InitList(LinkList L);
int ListLength(LinkList L);
int main(void)
{
LinkList L;
InitList(L);
printf("%d",ListLength(L));
return 0;
}
int InitList(LinkList L){
L=(LinkList)malloc(sizeof(LNode));
if(!L)return 0;
(L->next)=NULL;
return 1;
}
int ListLength(LinkList L)
{
int length=0;
if((L->next)==NULL) return 0;
struct LNode *x=(L->next );
while(x){
length++;
x=x->next ;
}
return length;
}
the segmentation fault was thrown
if((L->next)==NULL) return 0;