这段动态链表代码不知错哪了,纠结好久了,求破。
vs2012下编译没有错,运行时输入第一行完回车后
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct student {
int num;
float score;
struct student *next;
};
struct student *head, *p, *p1;
int n = 0, i;
struct student *creat(){
p1 = p = (struct student *)malloc(sizeof(struct student));
scanf_s("%d%10.2f", &p->num,&p->score);
while(p->num!= NULL){
n++;
if(n == 1)head = p;
else p1->next = p;
p1 = p;
p = (struct student *)malloc(sizeof(struct student));
scanf_s("%d%10.2f", &p->num,&p->score);
}
p1->next = NULL;
return head;
}
void main (){
head=creat();
p = head;
for(i = 0; i < n; i++){
printf("%d%10.2f\n", p->num,p->score);
p = p->next;
}
system("pause");
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct student {
int num;
float score;
struct student *next;
};
struct student *head, *p, *p1;
int n = 0, i;
struct student *creat(){
p1 = p = (struct student *)malloc(sizeof(struct student));
scanf_s("%d%f", &p->num,&p->score);//输入这里改为%f。。
while(p->num!= NULL){
n++;
if(n == 1)head = p;
else p1->next = p;
p1 = p;
p = (struct student *)malloc(sizeof(struct student));
scanf_s("%d%f", &p->num,&p->score);
}
p1->next = NULL;
return head;
}
void main (){
head=creat();
p = head;
for(i = 0; i < n; i++){
printf("%d%10.2f\n", p->num,p->score);
p = p->next;
}
system("pause");
}