一执行到p->next=NULL时,就出现Unhandle exception in 21302.exe:0x0000005:Access Violation,该如何处理

一执行到p->next=NULL时,就出现Unhandle exception in 21302.exe:0x0000005:Access Violation
#include<stdio.h>
typedef struct inlist
{
int data;
struct inlist *next;
}inlist;

inlist *creatlist(inlist *l);
int len_list(inlist *I);


main()
{
inlist *l=malloc(sizeof(inlist));
int x=0,k=0,len=0;
l=creatlist(l);
len=len_list(l);

}
inlist *creatlist(inlist *l)
{
inlist *p=malloc(sizeof(inlist)),
*q=malloc(sizeof(inlist));
int e=0;
p=l;
while(e!=-1)
{
p->next=q;
p=p->next;
scanf("%d",&e);
p->data=e;
q=malloc(sizeof(inlist));
   
}
p->next=NULL;
free(p);
  free(q);
return l;
}
int len_list(inlist *l)
{
inlist *p=malloc(sizeof(inlist));
int i=0;
p=l;
  p=p->next;
while(p->next!=NULL) //*一执行到p->next=NULL时,就出现Unhandle exception in 21302.exe:0x0000005:Access Violation 为什么?????
{
i++;
p=p->next;
}


return i;
}


------解决方案--------------------
创建链表的部分不对。
C/C++ code
inlist *creatlist(inlist *l)
{
inlist *p=malloc(sizeof(inlist)),
*q=malloc(sizeof(inlist));
q->next=NULL; //先初始化一下
int e=0;
l->next=p;//p=l;
while(e!=-1)
{
p->next=q;
p=p->next;
scanf("%d",&e);
p->data=e;
q=malloc(sizeof(inlist));
q->next=NULL; //加上这个
}
p->next=NULL;
//free(p);
free(q);
return l;
}