为什么链表老是出问题,一运行就退不出来了
#include <stdio.h>#define NULL 0
struct student
{
char name[20];
char sex;
int num, year;
struct student *next;
};
struct student *input(struct student *head)
{
int n = 0;
struct student *p1, *p2;
p1 = p2 = (struct student*) malloc (sizeof(struct student));
printf ("please enter information:");
scanf ("%s%d%c%d", p1->name, &p1->num, &p1->sex, &p1->year);
while (p1->num > 0)
{
n++;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
p2 = p1;
}
p1 = (struct student*) malloc (sizeof(struct student));
scanf ("%s%d%c%d", p1->name, &p1->num, &p1->sex, &p1->year);
}
p2->next = NULL;
return (head);
}
void output (struct student *head)
{
struct student *p;
p = head;
while (p != NULL)
{
printf ("%s %d %c %d", p->name, p->num, p->sex, p->year);
p = p->next;
}
}
void main ()
{
struct student *head;
head = NULL;
head = input (head);
output (head);
}