谭浩强的动态链表有问题?
我为了动态链表已经纠结1个多月了 书上的代码按抄还是错。。强哥总是让我失望。。。#include<stdio.h>#include<malloc.h>
#define LEN sizeof(struct student)
int n;
struct student
{
char name[15];
int score;
struct student *next;
};
struct student *creat()
{
struct student*p2,*p1,*head;
n=0;
p1 = p2 = (struct student *)malloc(LEN);
scanf ("%s,%d",&(p1->name),&(p1->score));
head = NULL ;
if(p1->name != NULL)
{
if((++n)==1) head=p1;
else p2->next=p1;
p2=p1;
p1 = (struct student *) malloc (LEN);
scanf ("%s,%d",&(p1->name),&(p1->score));
}
p2->next = NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do
{
printf("%s,%d",p->name,p->score);
p=p->next;
}while(p!=NULL);
}
void main()
{
struct student *p;
p=creat();
print(p);
}