C 链表问题
这是什么意思C 链表问题,c,linked-list,C,Linked List,这是什么意思 void add(struct node **root, int x) { struct node *conductor; if(*root==NULL) { (*root)=malloc(sizeof(struct node)); (*root)->x=x; (*root)->next=NULL ; } else {
void add(struct node **root, int x)
{
struct node *conductor;
if(*root==NULL)
{
(*root)=malloc(sizeof(struct node));
(*root)->x=x;
(*root)->next=NULL ;
}
else
{
conductor = *root;
while(conductor->next!=NULL)
{
conductor = conductor -> next;
}
conductor->next=malloc(sizeof(struct node));
conductor->next->x=x;
conductor->next->next=NULL;
}
}
conductor=conductor->next代码>这是什么意思?我需要满足我的好奇心,我想知道我的想法是否正确
它是我的,我只是想确定我的想法是否正确,我一直在怀疑我的代码一个链表是由一系列对象构成的,每个对象都指向列表中的下一个元素。线路conductor=conductor->next
只需更新conductor
变量(指向列表元素,struct节点
)即可指向列表中的下一个元素
更新:维基百科关于这样一个数据结构的文章提供了良好的视觉表现。conductor->next
只是一种书写(*conductor.)的方式。next
由于conductor是指向struct的指针,您不能通过conductor访问它的成员。下一步,在这种情况下,它意味着conductor
现在将指向列表中的下一个元素(前面指向的conductor->next
)conductor=*root
将指针导体设置为指向列表根中的第一个元素
conductor=conductor->next
将指针导体设置为指向列表根中的下一个元素
与长度为1的数组相比,第一行将设置指向数组第0个元素的指针,而下一行将设置指向第一个元素的指针。如果这没有意义,您应该仔细阅读链接列表,以及为什么要将它们与数组相比较。根据您的提问:
conductor=conductor->next means that the means that the conductor will move
到下一个内存位置
例如:
void insert(struct node **ptr)
{
struct node *tmp;
tmp=*ptr;
tmp=tmp->next;
}
tmp现在指向链表的起始内存位置,因为如果它不是常量指针,我们可以将指针指向任何地方
tmp=tmp->next意味着它指向下一个内存位置,这取决于编译器。如果指针的大小为4字节,它将移动到4字节。你能提供任何图形或图片表示吗?我对它的想法仍然模糊。你之前的问题应该是关于“[你的]链表代码”(引自你上一个问题的正文…)如果你不理解这段代码,它怎么可能是你的?做你的家庭作业,停止复制/粘贴,开始思考一点来理解编程!代码看起来非常类似:而且,如果你不能理解它,就去问它。当你试图让自己站起来的时候,你会把自己往下移,或者像这样往上移动。我做对了吗?第二步uctor=conductor->next,将成为头?它不会成为列表的头。头仍然是根,但while循环将使conductor
指向最后一个节点(当然不包括NULL)。换句话说,while循环实际上表示:while conductor没有指向最后一个节点(导体->下一步!=NULL,表示存在另一个节点)使导体指向下一个节点。