|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/* 实现链表的创建 */
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof( struct stu )
// 定义结构体
struct stu
{
int num;
float score;
struct stu *next;
};
//声明创建链表函数
struct stu *create();
// int n ,用来记录链表中节点的个数
int n=0;
void main()
{
struct stu *boy1;
boy1=create();
}
//创建链表函数
struct stu *create()
{
struct stu *head, *p1, *p2;
head=NULL; p2=NULL;
p1=( struct stu * ) malloc ( LEN );
printf( " \nplease input the num :");
scanf("%d", &p1->num);
printf("please input the score :");
scanf("%f",&p1->score); //执行到这句老是报错。runtime error!!不知道是什么回事。。
while ( 0 != p1->num )
{
n++;
if(1==n)
{
head=p2=p1;
p2->next=NULL;
}
else
{
p2->next=p1;
p2=p1;
}
p1=( struct stu * ) malloc ( LEN );
printf( " \nplease input the num :");
scanf("%d", &p1->num);
printf("please input the score :");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
|
|