马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
int n;
int main(void)
{
struct student *creat(void); //声明建立链表的函数
void print(struct student *); //声明输出链表的函数
struct student *head;
head = creat(); //调用create函数
print(head); //调用print函数
system("pause");
return 0;
}
struct student *creat(void) //定义建立链表的函数
{
struct student *head = NULL, *p1 = NULL, *p2 = NULL;
p1 = p2 = (struct student *)malloc(LEN);
printf("please input the num : ");
scanf("%d", &(p1 -> num) );
printf("please input score : ");
scanf("%f", &(p1 -> score));
n = 0;
while (0 != p1 -> num)
{
n++;
if (1 == n)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("please input the num : ");
scanf("%d", &(p1->num));
printf("please input score : ");
scanf("%f", &(p1->score));
}
p2->next = NULL;
return head;
}
void print(struct student *head) //定义输出链表的函数
{
struct student *p;
p = head;
while (0 != p -> num)
{
printf("成员%d的分数为%f\n", p -> num, p -> score);
p = p->next;
}
}
先放上代码
每次输出完数据后,都会跳出,是哪里出问题了吗?
|