链表:
#include <stdio.h>
#define NULL 0
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
struct student a,b,c,*head,*p;
a.num=99101;
a.score=89.5;
b.num=99102;
b.score=90;
c.num=99103;
c.score=85;
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
while(p!=NULL)
{
printf("%ld %5.1f\\n",p->num,p->score);
p=p->next;
}
}
生成链表1:
思路:
head=p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
p1=(struct student *)malloc(LEN);
p2->next=p1;
p2=p1;
scanf("%ld,%f",&p1->num, &p1->score);
p1=(struct student *)malloc(LEN);
p2->next=p1;
p2=p1;
scanf("%ld,%f",&p1->num, &p1->score);
p1=(struct student *)malloc(LEN);
p2->next=p1;
p2=p1;
scanf("%ld,%f",&p1->num, &p1->score);
p2->next=NULL;
代码:
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
struct student *creat(void);
struct student *p=creat();
while(p!=NULL)
{
printf("%ld %5.1f\\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
head=p1=p2=(struct student *)malloc(LEN);
while(1)
{
scanf("%ld,%f",&p1->num, &p1->score);
if(p1->num==0)
{
if(head==p1)
head=NULL;
else
p2->next=NULL;
break;
}
else
p2=p1;
p1=(struct student *)malloc(LEN);
p2->next=p1;
}
return(head);
}
生成链表2:
思路:
p1=p2=(struct student *)malloc(LEN);
head=NULL
scanf("%ld,%f",&p1->num, &p1->score);
head=p1
p2=p1
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
p2->next=NULL;
代码:
#include <stdio.h>
#define NULL 0
#define LEN sizeof (struct student)
struct student
{
long num;
float score;
struct student *next;
};
void main()
{
struct student *creat(void);
struct student *p=creat();
while(p!=NULL)
{
printf("%ld %5.1f\\n",p->num,p->score);
p=p->next;
}
}
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student *) malloc(LEN);
scanf("%ld,%f",&p1->num, &p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
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);
}