|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- struct student *creat() //创建链表函数
- {
- struct student *head;
- struct student *p1,*p2;
-
- p1 = p2 = (struct student *)malloc(LEN); //调用系统函数malloc申请空间,并通过强制转换赋值给p1和p2
-
- printf("Please Input number:"); //打印提示
- scanf("%d",&p1->num); //获取输入到p1->num
- printf("Please Input score:"); //打印提示
- scanf("%f",&p1->score); //获取输入到p1->score
- printf("\n");
-
- head = NULL; //初始化链表,代表链表为空
-
- while(p1->num) //如果输入的序号不为0
- {
- n++; //节点计数器自增1
- if(n == 1) //判断节点计数器的值
- {
- head = p1; //head指向p1
- }
- else
- {
- p2->next = p1; //p2->next指向p1
- }
- p2 = p1; //把p1的值赋值给p2
- p1 = (struct student *)malloc(LEN); //调用系统函数malloc申请空间,并通过强制转换赋值给p1
-
- printf("Please Input number:"); //打印提示
- scanf("%d",&p1->num);
- printf("Please Input score:");
- scanf("%f",&p1->score);
- printf("\n");
- }
- p2->next = NULL;
- free(p1);
- return head;
- }
复制代码 上面是一个建立链表的函数,其中 p2->next = p1;和p2 = p1; p1不是指向头结点么,然后把p1的值赋值给p2,那不是会把p2->next的值也覆盖了?我记得2个结构体之间赋值,是会把所有成员都赋值了。
|
|