用C写的代码,链表的创建和输出,不知道为什么输出不出来。
#include<stdio.h>#include<stdlib.h>intn;structStudent{intnum;floatscore;structStudent...
#include<stdio.h>
#include<stdlib.h>
int n;
struct Student
{
int num;
float score;
struct Student *next;
};
# define LEN sizeof(struct Student)
struct Student *creat(void)
{
struct Student *head,*p1,*p2;
head=NULL;
p1=p2=(struct Student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
n=0;
while(p1->num!=0)
{
n++;
if(n==0)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return (head);
}
int main()
{
struct Student *head,*p;
head=creat();
p=head;
printf("%d %f\n",p->num,p->score);
if(p!=NULL)
do
{
printf("%d %f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
return 0;
} 展开
#include<stdlib.h>
int n;
struct Student
{
int num;
float score;
struct Student *next;
};
# define LEN sizeof(struct Student)
struct Student *creat(void)
{
struct Student *head,*p1,*p2;
head=NULL;
p1=p2=(struct Student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
n=0;
while(p1->num!=0)
{
n++;
if(n==0)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return (head);
}
int main()
{
struct Student *head,*p;
head=creat();
p=head;
printf("%d %f\n",p->num,p->score);
if(p!=NULL)
do
{
printf("%d %f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询