链接列表问题

时间:2011-03-26 15:13:13

标签: 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
      {
          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;这是什么意思?我需要满足我的好奇心,我想知道我的想法是否真实

这是我的,我只是想确定我的想法是否正确,我对我的代码一直有疑问

4 个答案:

答案 0 :(得分:1)

链表由一系列对象构成,每个对象指向列表中的下一个元素。第conductor = conductor->next;行只是更新conductor变量(指向列表元素,struct node)以指向列表中的下一个元素。

更新:关于linked-list的维基百科文章提供了这种数据结构的良好直观表示。

答案 1 :(得分:0)

conductor -> next只是撰写(*conductor).next的一种方式。

由于conductor是指向struct的指针,你不能直接通过conductor.next访问它的成员,在这种情况下,它意味着conductor现在将指向列表中的下一个元素( conductor->next之前指出过)

答案 2 :(得分:0)

conductor=*root将指针指针设置为指向列表根

中的第一个元素

conductor=conductor->next将指针指针设置为指向列表根目录中的下一个元素。

与长度为1的数组相比,第一行将设置指向数组第0个元素的指针,而下一行则将指针设置为第1个元素。如果这没有意义,你应该阅读链表,以及为什么你使用它们与数组相比。

答案 3 :(得分: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现在指向链表的起始内存位置,因为我们可以将指针指向任何位置,如果它不是const指针。

tmp = tmp-> next表示它指向下一个内存位置,如果sizeof指针是4bytes,它将依赖于编译器,它将移动到4个字节。

相关问题