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

c语言链表里面有一些概念不清楚,求大神给我讲讲

c语言链表里面有一些概念不清楚,求大神给我讲讲

C
依木兰 2019-05-07 15:05:05
有点不明白链表里面的 struct node *head=(struct node*)malloc(sizeof(struct node)); struct node *ptail; head=ptail; 这个是什么意思,让ptail指向head节点吗? struct node *head=(struct node*)malloc(sizeof(struct node)); struct node *ptail=(struct node*)malloc(sizeof(struct node)); head=ptail; 那么这个又是什么意思呢? 还有 struct node *head=(struct node*)malloc(sizeof(struct node)); struct node *ptail;*pnew=(struct node*)malloc(sizeof(struct node)); head=ptail; ptail->next=pnew相当于head->next=pnew吗?
查看完整描述

3 回答

?
qq_慕圣9438953

TA贡献4条经验 获得超0个赞

第一部分

 struct node *head=(struct node*)malloc(sizeof(struct node)); 

这句话的意思是说:定义一个大小为struct node的内存空间,这个内存空间的首地址将储存在struct node类型的指针变量head中

struct node *ptail; 

定义一个struct node类型的指针变量ptail

head=ptail;

将ptail赋值给head,也可以说是让head指向ptail


第二部分

 struct node *head=(struct node*)malloc(sizeof(struct node));

 struct node *ptail=(struct node*)malloc(sizeof(struct node)); 

这两句话的意思是:分别分配两块struct node大小的内存空间给指针变量head和ptail

head=ptail;

同理,让head指向ptail


第三部分

 struct node *head=(struct node*)malloc(sizeof(struct node));

分配struct node大小的内存空间给指针变量head

 struct node *ptail;

声明指针变量ptail

*pnew=(struct node*)malloc(sizeof(struct node));

分配struct node大小的内存空间给指针变量pnew 

head=ptail; 

让head指向ptail

ptail->next=pnew

将指针pnew所指向的内存空间的首地址,储存在ptail的next元素中

这个操作相当于head->next=pnew,但并不是随时都如此,只有在head和ptail指向同一个内存空间时才是这样

查看完整回答
反对 回复 2019-05-22
  • 3 回答
  • 0 关注
  • 909 浏览

添加回答

举报

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