这是我的源代码,请求高手指点
//建立动态单链表 用cin,cout程序没法运行
#include<stdlib.h>//利用malloc函数
#include<stdio.h>
#define len sizeof(struct student)
struct student
{
float score;
int num;
struct student *next;
};
void main ()
{
void print(struct student *head);
struct student *creat(void);//声明返回指针值的函数, 说明creat()函数是一指针型函数,即函数返回值是一指针
struct student *pt,*head;
pt=creat();
print(head);
// printf("\nnum:%ld\nscore:%5.lf\n",pt->num,pt->score);//输出第一个节点的成员值,有返回值决定
}
int n;//节点个数
struct student *creat(void)
{
struct student *p1, *p2,*head;
n=0;
p1=p2=(struct student*)malloc(len);//(struct student *)使malloc返回的指针是struct student类型的,
//*不能少,否则返回的就不是指针了。系统能够实现隐式的转换所以,
//可写成p1=p2=malloc(len)
scanf("%ld,%f",&p1->num,&p1->score );//输入第一个学生的信息
head=NULL;
while(p2->num!=0)//最后节点信息输入完毕后输入0,0以表示信息输入完毕
{
n=n+1;
if (n==1) head=p1;
else p2->next =p1;
p2=p1;
p1=(struct student *)malloc(len);
scanf("%ld,%f",&p1->num,&p1->score );//输入其他学生的信息
}
p2->next =NULL;
return head ;//返回头结点,也可返回其他节点head->next即返回第二节点
}
void print(struct student *head)
{
struct student *p=NULL;
printf("%nNow,these%d records are:\n",n);
p=head;
if (head!=NULL)
do
{
printf("%ld %5.lf\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}