【问题标题】:I don't understand the implementation of inserting a new node in linked list我不明白在链表中插入新节点的实现
【发布时间】:2016-10-21 04:19:53
【问题描述】:

在链表实现中,向链表中插入一个新节点通常是这样写的:

void push(struct node** head_ref, int new_data)

/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));

/* 2. put in the data  */
new_node->data  = new_data;

/* 3. Make next of new node as head */
new_node->next = (*head_ref);

/* 4. move the head to point to the new node */
(*head_ref)    = new_node;

(我从http://quiz.geeksforgeeks.org/linked-list-set-2-inserting-a-node/获取此代码)

节点结构为:

结构节点 {

int 数据;

结构节点 *next;

};

我不明白的是插入部分的 3. 和 4.。所以你让new_node的next指针指向head,然后head指向new_node?所以这意味着下一个指针指向new_node?

这似乎是一个愚蠢的问题,但我无法理解它,所以我希望有人能给我解释一下。谢谢你。

【问题讨论】:

    标签: linked-list


    【解决方案1】:

    基本上在链表中,所有节点都相互连接。这取决于你在哪里插入一个新节点,无论是在结尾还是开始。每次我们插入一个新节点时,我们都会检查头指针。

       if(head == NULL)  //it means that the node is empty
       {
          head = newNode;  //so we will assign the new node to the head
       }
        else
       {
          node* temp = head;       //creating a temp pointer that will go 
                                   // to the end of the linked list
    
          while(temp -> next != NULL) { temp = temp->next; }
          temp = newNode;          //This will add the new node to the end
          newNode->next = NULL;enter code here 
        }
    

    【讨论】:

      【解决方案2】:

      如果我理解正确,这就是你的场景?

      http://www.kkhsou.in/main/EVidya2/computer_science/comscience/313.gif

      您的列表只是一个带有下一个指针的链表,直到列表的最后一项以 null 作为指针

      第 3 步使您的新节点指向在此操作之前位于列表开头的第二项

      第4步使列表头指向新节点

      希望对你有帮助

      【讨论】:

      • 非常感谢。你的图表真的帮助我理解。
      【解决方案3】:

      /* 1. 分配节点/ 结构节点 new_node = (struct node*) malloc(sizeof(struct node));

      /* 2. 输入数据 */ new_node->data = new_data;

      /* 3. 将新节点的next作为head */ new_node->next = (*head_ref);

      /* 4. 移动头部指向新节点 */ (*head_ref) = new_node;

      在第 1 步和第 2 步中,创建一个新节点并为其分配数据。

      当您的列表为空时,您的 *head_ref 将为空。 或者如果它有任何元素,它会指向那个 举个例子吧

      Now 
      *head_ref is null
      when input is 1
      newnode.data=1
      newnode.next=null
      *headref=newnode 
      now your *headref points to the latest node that is added ,this happens with step4
      
      When you insert 2
      newnode.data=2
      newnode.next=*headref(to the node which is 1)
      newnode=*headref
      now your list is
      1->2(*headref)
      now if you add 3 here
      it becomes
      1->2->3(*headref)
      

      希望你能理解

      【讨论】:

      • 感谢您的示例
      【解决方案4】:

      我不会向你解释,而是建议一种技术来帮助你自己找出答案。

      1. 准备一张纸、一支铅笔和一块橡皮。

      2. 在纸上画一个方框来表示算法中的每个变量

      3. 绘制初始链表:

        • 画一个方框来代表初始链表中每个现有的node
        • 将每个框分成代表字段的子框。
        • 在每个字段中写入一个值或一个点,表示指针的“从”端。
        • 对于每个指针,在所指向的事物(例如node)上画一条线,并在“到”端放置一个箭头。 NULL 指针只是一个点。
      4. 现在执行算法。

        • 每次分配一个新的node,就画一个新的框。
        • 每次为变量或字段赋值时,删除当前值并写入/绘制新值或新点/箭头。

      如果您仔细而系统地执行此操作,您将能够准确地看到列表插入算法在做什么。

      同样的技术可用于可视化任何列表/树/图形算法...取模您将所有内容放到一张纸上的能力,以及纸在反复擦掉的能力。


      (这种铅笔和纸的方法非常“老派”。就像,这是我在 1970 年代学习编程时被教导要做的。更现代的方法是使用白板...)

      【讨论】:

        【解决方案5】:

        首先头指针指向列表中的第一个节点。 在 (1) 中创建新节点。 在 (2) 中,数据被保存到新节点。 在(3)新节点指向头部指向的位置(意味着第一个节点) 在 (4) 中,现在头指向新节点,因此新节点现在是第一个节点。就是这样。

        【讨论】:

          猜你喜欢
          • 2016-03-30
          • 1970-01-01
          • 2018-07-28
          • 2022-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多