#include"stdafx.h"
#include
#define LEN sizeof(student)
struct student{
int num;
float score;
student *next;
};
student * creat(){
int num;
float score;
student *head, *p,*q;
q=p=head = (student *)malloc(LEN);
head->num = NULL;
scanf_s("%d%f",&num,&score);
while (num != 0)//如果輸入0的話就當成結束
{
p = (student*)malloc(LEN);
p->num = num;
p->score = score;
p->next = NULL;
q->next = p;
q = p;
scanf_s("%d%f",&num,&score);
}
p->next = NULL;
return head;
}
void print(student * head){
student *p;
p = head;
for (; p->next != NULL;)
{
printf("%d %f", p->next->num, p->next->score);
p = p->next;
}
}
void main(){
student *head;
creat();
head = creat();
print(head);
}
//我輸入
1 20
2 30
3 40
0 10
的話沒有輸出什麼東西啊
主函數裡為什麼會有兩個creat函數調用,我把你的代碼測試了一下,去掉第一個creat 是可以運行成功的,也有輸出信息。另外你的代碼缺少標准庫能編譯通過嗎?希望有所幫助。
#include
#include
#define LEN sizeof(student)
struct student{
int num;
float score;
student *next;
};
student * creat(){
int num;
float score;
student head, *p,*q;
q=p=head = (student *)malloc(LEN);
head->num = NULL;
scanf("%d%f",&num,&score);
while (num != 0)//如果輸入0的話就當成結束
{
p = (student)malloc(LEN);
p->num = num;
p->score = score;
p->next = NULL;
q->next = p;
q = q->next;
scanf("%d%f",&num,&score);
}
p->next = NULL;
return head;
}
void print(student * head){
student *p;
p = head;
for (; p->next != NULL;)
{
printf("%d %f", p->next->num, p->next->score);
p = p->next;
}
}
int main(){
student *head;
head = creat();
print(head);
return 0;
}