下面程序无法运行,请找出程序的错误,多谢。
/*链表的建立和输入输出*/
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[20];
int number;
struct Student *pnext;
};
struct Student * create (int n);
void print (struct Student * phead, int n);
int main ()
{
int n;
scanf ("%d", &n);
print (create (n), n);
return 0;
}
struct Student * create (int n)
{
struct Student * phead = NULL;
struct Student * pnew, *pold;
pnew = (struct Student *) malloc (sizeof(struct Student));
pold = pnew;
int count = 0;
while (n > 0);
{
scanf ("%s %d", &pnew -> name, &pnew -> number);
count++;
if (count == 1)
{
pnew -> pnext = NULL;
phead = pnew;
pold = pnew;
}
else
{
pnew -> pnext = NULL;
pold -> pnext = pnew;
pold = pnew;
}
pnew = (struct Student *) malloc (sizeof(struct Student));
n--;
}
free (pnew);
return phead;
}
void print (struct Student *phead, int n)
{
struct Student * ptemp = phead;
while (n > 0)
{
printf ("%s %d\n", ptemp -> name, ptemp -> number);
ptemp = ptemp -> pnext;
n--;
}
}
/*链表的建立和输入输出*/
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[20];
int number;
struct Student *pnext;
};
struct Student * create (int n);
void print (struct Student * phead, int n);
int main ()
{
int n;
scanf ("%d", &n);
print (create (n), n);
return 0;
}
struct Student * create (int n)
{
struct Student * phead = NULL;
struct Student * pnew, *pold;
pnew = (struct Student *) malloc (sizeof(struct Student));
pold = pnew;
int count = 0;
while (n > 0);
{
scanf ("%s %d", &pnew -> name, &pnew -> number);
count++;
if (count == 1)
{
pnew -> pnext = NULL;
phead = pnew;
pold = pnew;
}
else
{
pnew -> pnext = NULL;
pold -> pnext = pnew;
pold = pnew;
}
pnew = (struct Student *) malloc (sizeof(struct Student));
n--;
}
free (pnew);
return phead;
}
void print (struct Student *phead, int n)
{
struct Student * ptemp = phead;
while (n > 0)
{
printf ("%s %d\n", ptemp -> name, ptemp -> number);
ptemp = ptemp -> pnext;
n--;
}
}