这个链表程序出现段错误,是什么原因,如何解决?
//代码段1:#include<stdio.h>#include<stdlib.h>typedefstructstudent{intno;structstudent*ne...
//代码段1:
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int no;
struct student *next;
}student;
int main()
{
student *head=NULL,*p1=NULL,*p2=NULL,*p=NULL;
p1=(student *)malloc(sizeof(student));
printf("Input No:");
scanf("%d",&p1->no);
while(p1->no)
{
//if(head==NULL)
// head=p1;
//else
p2->next=p1;
p2=p1;
p1=(student *)malloc(sizeof(student));
//p2=p1;
printf("Input No:");
scanf("%d",&p1->no);
}
p=head;
while(p)
{
printf("%d\n",p->no);
p=p->next;
}
return 0;
}
//代码段2(即把注释去掉):
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int no;
struct student *next;
}student;
int main()
{
student *head=NULL,*p1=NULL,*p2=NULL,*p=NULL;
int n=0;
p1=(student *)malloc(sizeof(student));
printf("Input Student's No.:");
scanf("%d",&p1->no);
while(p1->no)
{
n++;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(student *)malloc(sizeof(student));
printf("Input Student's No.:");
scanf("%d",&p1->no);
}
p=head;
while(n)
{
printf("\n%d",p->no);
p=p->next;
n--;
}
return 0;
}
RT,运行代码段1时,输入第一个数据就提示段错误,而代码段2不会 展开
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int no;
struct student *next;
}student;
int main()
{
student *head=NULL,*p1=NULL,*p2=NULL,*p=NULL;
p1=(student *)malloc(sizeof(student));
printf("Input No:");
scanf("%d",&p1->no);
while(p1->no)
{
//if(head==NULL)
// head=p1;
//else
p2->next=p1;
p2=p1;
p1=(student *)malloc(sizeof(student));
//p2=p1;
printf("Input No:");
scanf("%d",&p1->no);
}
p=head;
while(p)
{
printf("%d\n",p->no);
p=p->next;
}
return 0;
}
//代码段2(即把注释去掉):
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int no;
struct student *next;
}student;
int main()
{
student *head=NULL,*p1=NULL,*p2=NULL,*p=NULL;
int n=0;
p1=(student *)malloc(sizeof(student));
printf("Input Student's No.:");
scanf("%d",&p1->no);
while(p1->no)
{
n++;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(student *)malloc(sizeof(student));
printf("Input Student's No.:");
scanf("%d",&p1->no);
}
p=head;
while(n)
{
printf("\n%d",p->no);
p=p->next;
n--;
}
return 0;
}
RT,运行代码段1时,输入第一个数据就提示段错误,而代码段2不会 展开
1个回答
展开全部
//代码段1:
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int no;
struct student *next;
}student;
int main()
{
student *head=NULL,*p1=NULL,*p2=NULL,*p=NULL;
int oneItem;
printf("Input No:");
scanf("%d",&oneItem);
if(oneItem==0) //先检查输入的号码是否为0
{
printf("\n没有学生数据.\n");
return 0;
}
//确认了输入号码不为0,再分配动态内存,保存数据
p1=(student *)malloc(sizeof(student));
if(p1==NULL)
{
printf("\n分配动态内存出错.\n");
return 0;
}
p1->no=oneItem;
p1->next=NULL; //必须让p1->next等于NULL
head=p1; //head指向第1个节点
p2=p1; //p2指向当前节点,p1是新节点
while(1)
{
printf("Input No:");
scanf("%d",&oneItem);
if(oneItem==0) //先检查输入的号码是否为0
{
break;
}
//确认了输入号码不为0,再分配动态内存,保存数据
p1=(student *)malloc(sizeof(student));
if(p1==NULL)
{
printf("\n分配动态内存出错.\n");
return 0;
}
p1->no=oneItem;
p1->next=NULL;
p2->next=p1; //将新节点p1加在p2的末尾
p2=p1; //p2指向当前节点
}
p=head;
while(p) //语句while(p)就是相当于while(p!=NULL)
{
printf("%d\n",p->no);
p=p->next;
}
return 0;
}
//代码段2的问题:
......
while(p1->no)
{
n++;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(student *)malloc(sizeof(student));
//新节点p1->next没有设定为NULL
printf("Input Student's No.:");
scanf("%d",&p1->no);
//假设链表已经有2个学生数据,此时n=2,
//p1提前分配了内存,然后,屏幕输入数字0,此时p1->no等于0,
//n仍然是2个数据,但是,实际有3个数据,最后的数据是0,
//退出了while(p1->no)循环之后,while(n)循环只显示2个数据,
//最后那个节点p1成为了"游离"的数据,没有加入链表里.
}
......
while(n)
{
printf("\n%d",p->no);
p=p->next;
n--;
}
......
追问
忘了说一下,我是在linux环境下的c。
代码段2是没有问题的,我只想知道代码段1跟代码段2的区别是什么?以及代码段1为什么会出现错误,该如何解决,谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询