为了账号安全,请及时绑定邮箱和手机立即绑定

关于动态链表,为什么头指针要为空,不为空也可以赋值p1,不是吗?

关于动态链表,为什么头指针要为空,不为空也可以赋值p1,不是吗?

C
溯源1 2017-01-04 18:51:24
#include "stdio.h"#include<malloc.h>struct weapon{ int price; int atk; struct weapon *next;};struct weapon * create(){    struct weapon *head;    struct weapon *p1,*p2;    int n=0;    p1=p2=(struct weapon*)malloc(sizeof(struct weapon));    scanf("%d%d",&p1->price,&p1->atk);    head=NULL;//为什么头指针要为空,不为空也可以赋值p1,不是吗?    while(p1->price!=0){    n++;    if(n==1) head=p1;    else p2->next=p1;    p2=p1;    p1=(struct weapon*)malloc(sizeof(struct weapon));    scanf("%d%d",&p1->price,&p1->atk);    }    p2->next=NULL;    return (head);}int main(){ struct weapon *p; p=create(); printf("%d%d",p->price,p->atk); return 0;}
查看完整描述

1 回答

已采纳
?
DOUBLE_l

TA贡献45条经验 获得超12个赞

可以的,
查看完整回答
反对 回复 2017-01-04
  • 1 回答
  • 0 关注
  • 1668 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信