为什么《创建的链表》 当执行程序时输入相应的数 敲回车时会提示错误

hzc543806053 2011-05-25 02:53:33
#include<stdio.h>
#include<stdlib.h>
#define type struct stu
#define LEN sizeof(struct stu)

struct stu
{
int num;
char sex;
struct stu *next;
};
type *list(int n)
{
int i;
struct stu *head,*q,*p;
for(i=0;i<n;i++)
{
q=(type*)malloc(LEN);
scanf("%d%c",&q->num,&q->sex);
if(i==0)
p=head=q;
else
p->next=q;

q->next=NULL;
p=q;
}
head->next=NULL;
return (head);
}
main()
{
int i,conut=6;

struct stu *student;
student=list(conut);
printf("num\tsex\n");
for(i=0;i<conut;i++)
{
student=student->next;
printf("%d\t%c",student->num,student->sex);
}
}


1.看下以上的程序哪错了。。为什么会提示内存的错误?

2.还有就是我要在这个程序的基础上 添加一些程序实现有插入结点 有删除结点的功能 谢谢
...全文
62 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
如此美丽的你 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hzc543806053 的回复:]
C/C++ code
for(i=0;i<n;i++)
{
q=(type*)malloc(LEN);
scanf("%d %c",&q->num,&q->sex);//留个空格
if(i==0)
p=head=q;
else
{
p->next=q;

//q->next=NULL;
p=q;……
[/Quote]
++
hzc543806053 2011-05-25
  • 打赏
  • 举报
回复
for(i=0;i<n;i++)
{
q=(type*)malloc(LEN);
scanf("%d %c",&q->num,&q->sex);//留个空格
if(i==0)
p=head=q;
else
{
p->next=q;

//q->next=NULL;
p=q;
}
}
p->next=NULL; //head->next=NULL改为p->next=NULL
return (head);
}
newfarmerchi 2011-05-25
  • 打赏
  • 举报
回复

//这是一个有添加和删除的小程序,供参考
#include<stdio.h>
#include<malloc.h>
#define N 3

struct student *cj();
void disp(struct student *head);
void tj(struct student *head,int i);
void del(struct student *head,int i);

struct student{
int xuehao;
char name[10];
struct student *next;
};
void main()
{
struct student *head;
int i;
head=cj();
disp(head);
printf("请输入要添加的位置");
scanf("%d",&i);
tj(head,i);
disp(head);
printf("请输入要删除的位置");
scanf("%d",&i);
del(head,i);
disp(head);

}



struct student *cj()
{
struct student *head,*pnew,*p;
int i=0,k;

head=(struct student *)malloc(sizeof(struct student));
head->next=NULL;
p=head;

if(head==NULL)
return NULL;
while(i<3)
{
printf("input xuehao");
scanf("%d",&k);
getchar();

i++;
pnew=(struct student *)malloc(sizeof(struct student));
if(pnew==NULL)
return NULL;
pnew->xuehao=k;
printf("input name");
gets(pnew->name);
p->next=pnew;
p=pnew;
}
p->next=NULL;
return head;
}
void disp(struct student *head)
{
struct student *p=head->next;
while(p!=NULL)
{
printf("%d %s\n",p->xuehao,p->name);
p=p->next;
}
}
void tj(struct student *head,int i)
{
int j=0,k;

struct student *p=head, *s;
struct student *pnew;
while(j<i&&p)
{s=p;
p=p->next;
j++;
}
pnew=(struct student *)malloc(sizeof(struct student));
if(pnew==NULL)
return;
printf("input xuehao");
scanf("%d",&k);
getchar();
pnew->xuehao=k;
printf("input name");
gets(pnew->name);
if (p)
{
pnew->next=p->next;
p->next=pnew;
}
else
{
s->next=pnew;
pnew->next=NULL;
}
}
void del(struct student *head,int i)
{
struct student *p=head->next;
struct student *d, *r;

int j=1;
if (head==NULL||head->next==NULL)
{
return ;
}
if (i==0||i==1)
{
d=head->next;
head->next=head->next->next;
free(d);
}
else
{
r=head;
while(j<i&&p)
{
r=p;
p=p->next;
j++;
}
if (p)
{
d=p;
r->next=p->next;
free(d);
}
else
{
printf("node dose not exist\n");
}
}

}




newfarmerchi 2011-05-25
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
#define type struct stu
#define LEN sizeof(struct stu)

struct stu
{
int num;
char sex;
struct stu *next;
};
type *list(int n)
{
int i;
struct stu *head,*q,*p;
for(i=0;i<n;i++)
{
q=(type*)malloc(LEN);
scanf("%d %c",&q->num,&q->sex);//留个空格
if(i==0)
p=head=q;
else
{
p->next=q;

//q->next=NULL;
p=q;
}
}
p->next=NULL;
return (head);
}
main()
{
int i,conut=6;

struct stu *student;
student=list(conut);
printf("num\tsex\n");
for(i=0;i<conut;i++)
{
//student=student->next;
printf("%d\t%c\n",student->num,student->sex);
student=student->next;
}
}



hzc543806053 2011-05-25
  • 打赏
  • 举报
回复
那怎么改呢??
bdmh 2011-05-25
  • 打赏
  • 举报
回复
你的list中都没有对指针初始化,回车后访问就会出错,而且你返回的是局部指针,有问题的
bdmh 2011-05-25
  • 打赏
  • 举报
回复

69,382

社区成员

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

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