为什么会出现死循环?
看代码程序代码:
#include <stdio.h> #include <stdlib.h> #define N 3 struct student { char number[12]; char name[10]; float score[N]; struct student *next; }; int main(void) { struct student *creat(); void print(struct student *head); struct student *head; head=creat(); print(head); return 0; } //创建链表 struct student *creat() { struct student *head; struct student *p1, *p2; int n, i; p1=p2=(struct student *)malloc(sizeof(struct student)); printf("Please input student's number:"); scanf("%s", p1->number); printf("Please input student's name:"); scanf("%s", p1->name); printf("Please input %d 门 score:", N); for(i=0; i<N; i++) { scanf("%f", &p1->score[i]); } head=NULL; while(p1->number != 0) { n += 1; if(1==n) { head=p1; } else { p2->next=p1; } p2=p1; p1=(struct student *)malloc(sizeof(struct student)); printf("Please input student's number:"); scanf("%s", p1->number); printf("please input student's name:"); scanf("%s", p1->name); printf("Please input student's %d 门score:", N); for(i=0; i<N; i++) { scanf("%f", &p1->score[i]); } p2->next=p1; p2=p1; } p2->next=NULL; return head; } //打印链表 void print(struct student *head) { int i; struct student *p1; p1=head; printf("number\tname\t"); for(i=0; i<N; i++) { printf("score%d\t", i+1); } printf("\n"); while(p1 != NULL) { printf("%s%s", p1->number, p1->name); for(i=0; i<N; i++) { printf("%3.1f\t", p1->score[i]); } printf("\n"); p1=p1->next; } }
如何修改!