|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
书上的 单向动态链表程序 p1 p2定义成指向结构体struct student的指针 为什么scanf 里还需要加&(注释?的那行) p1 p2不是已经是地址了么
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num; //学号
float score; //成绩
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
]struct student *p1,*p2; //指向struct student 的指针p1 p2
p2=p1=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num, &p2->score); //???????????、
head=NULL;
n=0;
while(p1->num!=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);
}
void print(struct student *head) //输出函数
{
struct student *p;
printf("you %d ge shu jv:\n",n);
p=head;
if(head!=NULL)
{
while(p!=NULL)
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}
}
}
void main() //主
{
struct student *head;
head=creat();
print(head);
}
本帖最后由 claws0n 于 2018-8-18 10:53 编辑
更正:是的
|
|