首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一执行到p->next=NULL时,就出现Unhandle exception in 21302.exe:0x0000005:Access Violati

2012-03-21 
一执行到p-nextNULL时,就出现Unhandle exception in 21302.exe:0x0000005:Access Violation#includestd

一执行到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;} 

热点排行