C 创建链表
Posted 魄灆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 创建链表的主要内容,如果未能解决你的问题,请参考以下文章