求老鸟,单项链表最后总要输出空值,怎么限制
程序代码:
#include <stdio.h> #include <malloc.h> typedef struct node{ int score; char name[30]; struct node *link; }NODE; void create(NODE *head,int n){ NODE *p; for( ;n>0;n--){ p=(NODE *)malloc(sizeof(NODE)); if(p==NULL) exit(0); p->link=head->link; head->link=p; printf("Pleas ENter name: "); scanf("%s",&p->name); printf("Pleas ENter score: "); scanf("%d",&p->score); } } void print_str(NODE *head){ NODE *p; p=head; while(p->link!=NULL){ p=p->link; printf("Name: %s, Score: %d\n",p->name,p->score); } } int main(void){ NODE *head; int n=0; printf("Pleas Enter Stu Nu:"); scanf("%d",&n); create(head,n); print_str(head); getch(); return 0; }