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

HQWEIEI 2010-04-29 09:48:31
#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;
}
...全文
140 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
huanmie_09 2010-04-29
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>

typedef struct inlist
{
int data;
struct inlist *next;
} inlist;

inlist *creatlist(inlist *l);
int len_list(inlist *I);
void print_list(inlist *l);
void free_list(inlist *l);

int main()
{
inlist *l=(inlist *)malloc(sizeof(inlist));
int x=0,k=0,len=0;
if(l == NULL) {
exit(1);
}
l->next = NULL;
l=creatlist(l);
print_list(l);
len=len_list(l);
printf("len=%d\n", len);
free_list(l);
return 0;
}

inlist *creatlist(inlist *l)
{
inlist *p = NULL; //=(inlist *)malloc(sizeof(inlist));
inlist *q = l;
//*q=malloc(sizeof(inlist));
int e=0;
//p=l;
while(1)
{
//p->next=q;
//p=p->next;
scanf("%d",&e);
if(e == -1) {
break;
}
p=(inlist *)malloc(sizeof(inlist));
if(p == NULL) {
exit(1);
}
p->data=e;
p->next = NULL;
q->next = p;
q = p;
}
q->next = NULL;
//free(p);
//free(q);
return l;
}

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

void print_list(inlist *l)
{
inlist *p;
p=l->next;
while(p != NULL)
{
printf("%d ", p->data);
p=p->next;
}
printf("\n");
}

void free_list(inlist *l)
{
inlist *p, *q;
p = l;
while(p != NULL)
{
q = p->next;
free(p);
p = q;
}
}
田暗星 2010-04-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 peng6sl 的回复:]
我试了下楼主的代码,主要是所有的malloc(sizeof(inlist))都没有指定类型,应该是(inlist*)malloc(sizeof(inlist)),然后就没什么问题了,我是在unbuntu下编译的
[/Quote]

对了,问题找到了
wade_2003 2010-04-29
  • 打赏
  • 举报
回复
给你个参考例子

#include <iostream>
#include "malloc.h"
#define LEN sizeof(struct node)
struct node
{
int m;
struct node *prev,*next;
};

struct node* create(int n)
{
struct node *first,*last,*temp;
first =last = new node;
first->m = 1;
first->next = NULL;
last = first;
for (int i =2;i<=n;i++)
{
temp =(struct node*)malloc(LEN);
temp ->m =i;
temp->next = NULL;
last->next =temp;
temp->prev =last;
last = temp;
}
last->next =first;
first->prev =last;
return first;
}
void main()
{
int n;
scanf("%d",&n);
struct node *move,*link,*temp;
move =link = create(n);

while(move !=link);

for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("%d",move->m);
move=move->next;
}
move = move->next;
printf("\n");
}
free(move);
}*/
cattycat 2010-04-29
  • 打赏
  • 举报
回复
创建链表的部分不对。
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;
}
peng6sl 2010-04-29
  • 打赏
  • 举报
回复
我试了下楼主的代码,主要是所有的malloc(sizeof(inlist))都没有指定类型,应该是(inlist*)malloc(sizeof(inlist)),然后就没什么问题了,我是在unbuntu下编译的
wade_2003 2010-04-29
  • 打赏
  • 举报
回复

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;

pengzhixi 2010-04-29
  • 打赏
  • 举报
回复
你应该在你创建链表这个函数里面找原因。

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧