当前位置: 技术问答>linux和unix
请看看我的有关链表的程序
来源: 互联网 发布时间:2014-11-24
本文导语: 我建立和输出链表,然后遍历,在linux下用gcc编译通过,可是运行,提示:segmentation fault(core dumped)错误(是遍历函数有错),请帮我检查一下好吗? #define NULL 0 #define ERROR error #define LEN sizeof(struct student) struct st...
我建立和输出链表,然后遍历,在linux下用gcc编译通过,可是运行,提示:segmentation fault(core dumped)错误(是遍历函数有错),请帮我检查一下好吗?
#define NULL 0
#define ERROR error
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
main()
{int i_get;
struct student *p;
struct student *element;
struct student *creat(void);
void print(struct student *head);
struct student *getitem(struct student *head,int i,struct student *e);
p=creat();
print(p);
printf("nPlease input the sequence of element to get:");
scanf("%d",&i_get);
getitem(p,1,element);
printf("n%ld %f n",element->num,element->score);
}
/*creat linklist*/
struct student *creat(void)
{struct student *head,*p1,*p2;
int n=0;
head=NULL;
p1=p2=(struct student *)malloc(LEN);
printf("nPlese input the num and the score of student:n");
scanf("%ld%f",&p1->num,&p1->score);
while(p1->num!=0)
{n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
/*input a linklist*/
void print(struct student *head)
{struct student *p_print;
p_print=head;
while(p_print!=NULL)
{ printf("%ld %fn",p_print->num,p_print->score);
p_print=p_print->next;
}
}
/*Getitem*/
struct student *getitem(struct student *head,int i,struct student *e)
{struct student *p_get;
int j;
if(head==NULL)
{
printf("nERROR!n");
return ;
}
p_get=head;
j=1;
while(p_get&&jnext;
j++;
}
if(!p_get||j>i) return ;
e->num=p_get->num;
e->score=p_get->score;
e->next=NULL;
return e;
}
#define NULL 0
#define ERROR error
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
main()
{int i_get;
struct student *p;
struct student *element;
struct student *creat(void);
void print(struct student *head);
struct student *getitem(struct student *head,int i,struct student *e);
p=creat();
print(p);
printf("nPlease input the sequence of element to get:");
scanf("%d",&i_get);
getitem(p,1,element);
printf("n%ld %f n",element->num,element->score);
}
/*creat linklist*/
struct student *creat(void)
{struct student *head,*p1,*p2;
int n=0;
head=NULL;
p1=p2=(struct student *)malloc(LEN);
printf("nPlese input the num and the score of student:n");
scanf("%ld%f",&p1->num,&p1->score);
while(p1->num!=0)
{n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
/*input a linklist*/
void print(struct student *head)
{struct student *p_print;
p_print=head;
while(p_print!=NULL)
{ printf("%ld %fn",p_print->num,p_print->score);
p_print=p_print->next;
}
}
/*Getitem*/
struct student *getitem(struct student *head,int i,struct student *e)
{struct student *p_get;
int j;
if(head==NULL)
{
printf("nERROR!n");
return ;
}
p_get=head;
j=1;
while(p_get&&jnext;
j++;
}
if(!p_get||j>i) return ;
e->num=p_get->num;
e->score=p_get->score;
e->next=NULL;
return e;
}
|
return 后面跟返回值,return ;表示return值是void.
linux下main函数的返回值应该是int型
linux下main函数的返回值应该是int型
|
element没malloc