首页 > 代码库 > C 创建链表

C 创建链表

#include<malloc.h>
#include<stdio.h>
#define LEN sizeof(struct student)
typedef struct student
{
int num;
int age;
float score;
struct student *next;
}stu;
int n;
stu *creat(void)
{
stu *struHead;
stu *struP1;
stu *struP2;
n = 0;
struP1 = struP2 = ( stu *)malloc( LEN );
scanf("%d,%d,%f", &struP1->num, &struP1->age, &struP1->score);
struHead = NULL;
while( struP1->num != 0 )
{
n = n + 1;
if( n == 1 )
{
struHead = struP1;
}
else
{
struP2->next = struP1;
}
struP2 = struP1;
struP1 = ( stu *)malloc( LEN );
scanf("%d,%d,%f", &struP1->num, &struP1->age, &struP1->score);
}
struP2->next = NULL;
return( struHead );
}
void main()
{
stu *p;
stu *head;
head = creat();
p = head;
if( head != NULL )
do
{
printf( "%d,%d,%f\n", p->num, p->age, p->score);
p = p->next;

}while( p != NULL );

}

C 创建链表