了解如何在链接列表末尾添加新节点

时间:2014-03-13 05:56:04

标签: c++ linked-list nodes

struct Node
{
    int data;
    Node *next;
};

//function to add node
.
.
.
ListNode <NODETYPE> *newPtr = getNewNode(value);
if( isEmpty() ) //function to check if list is empty
    firstPtr = lastPtr = newPtr;
else
{
    lastPtr->next = newPtr; // 1 what does this do?
    lastPtr = newPtr;  // 2 and this?
}

这是我教科书中的一段代码。我很难理解标有上面数字的2条线。我理解如果列表为空,那么我们将第一个和最后一个指针指向我们新创建的节点。但是,如果列表中已有节点,它是如何工作的?

让我感到困惑的是指向newPtr创建的行。标记为1和2的行之间有什么区别?

这是我的看法。

For 1:我们告诉lastPtr的指针部分指向新节点

对于2:我们然后将lastPtr指向newPtr?为什么我们两次指向newPtr?

有人可以解释一下吗?

4 个答案:

答案 0 :(得分:2)

  

lastPtr-&gt; next = newPtr; // 1这是做什么的

在此阶段,lastPtr指向当前位于列表末尾的节点。它的next成员为null,因为它之后没有任何内容。更新next以指向新节点,因为新节点现在跟在上一个节点之后,因此必须更新上一个节点以指向列表中的next节点 - 新节点正在插入

  

lastPtr = newPtr; // 2和这个?

这告诉列表新节点现在是列表中的最后一个节点。

答案 1 :(得分:1)

节点保存数据和指向下一个节点的指针,以便列表可以链接:

struct Node
{
    int data;
    Node *next;
};

假设NodeTypeNode,则会创建一个新节点。创建新节点后,lastPtr是指向最后插入元素的指针,因此这会将newPtr添加到列表的末尾。

ListNode <NODETYPE> *newPtr = getNewNode(value);
if( isEmpty() ) //function to check if list is empty
    firstPtr = lastPtr = newPtr;
else
{
    lastPtr->next = newPtr; //This links your newPtr to the next field of the node that was last before and still is.
    lastPtr = newPtr;  // This updates lastPtr to point to the new last node, which is the one you just inserted.
}

答案 2 :(得分:1)

您教科书中的代码似乎试图扼杀单轨目的,而不是包含您应该考虑的内容,即管理列表。

  1. 此步骤将新节点连接到列表节点当前末尾的next指针。
  2. 此步骤建立了新的列表结尾。
  3. 也就是说,此代码中的链接列表模拟队列,其中firstPtr始终指向&#34;前面&#34;队列中,lastPtr始终指向&#34;返回&#34;新来者去的队列。如果一个是NULL,它们最好是。因此,你可以假设:

    if (lastPtr == NULL) // if this is true, the list is empty.
    

    因此,在新来的时候考虑这个:

    • 如果列表为空, firstPtrlastPtr应指向新节点。
    • 否则,firstPtr保持不变,lastPtr->next设置为新节点,然后lastPtr设置为新节点。

    请注意,在这些情况的两个中,lastPtr将在完成后引用新节点。因此整个事情变成了:

    if (lastPtr == nullptr)
        firstPtr = newPtr;       // list is empty, first must point to new arrival
    else
        lastPtr->next = newPtr;  // list is not empty, wire to end of list
    
    lastPtr = newPtr;            // regardless, establish new end-of-list
    

    删除节点有点类似,顺便说一句。

    if (firstPtr != nullptr)
    {
        victim = firstPtr;
        firstPtr = firstPtr->next;
        delete victim;
    
        if (firstPtr == nullptr)
            lastPtr = nullptr;
    }
    

    一张图片说千言万语。以下操作将生成相应的图表:

    1. 以空列表开头
    2. 添加NODE0
    3. 添加NODE1
    4. 添加NODE2
    5. 删除NODE0
    6. 添加NODE3
    7. 删除NODE1
    8. 删除NODE2
    9. 删除NODE3
    10. <强> 1。从空列表开始

      firstPtr ----> NULL
      lastPtr  ----> NULL
      

      <强> 2。添加NODE0

      firstPtr ------> NODE0 ---> NULL
                         |
      lastPtr ------------
      

      第3。添加NODE1

      firstPtr ----> NODE0 ---> NODE1 ----> NULL
                                  |
      lastPtr ---------------------
      

      <强> 4。添加NODE2

      firstPtr ----> NODE0 ---> NODE1 ----> NODE2 ----> NULL
                                              |
      lastPtr ---------------------------------
      

      <强> 5。删除NODE0

      firstPtr ----> NODE1 ----> NODE2 ----> NULL
                                   |
      lastPtr ----------------------
      

      <强> 6。添加NODE3

      firstPtr ----> NODE1 ----> NODE2 ----> NODE3 ----> NULL
                                               |
      lastPtr ----------------------------------
      

      <强> 7。删除NODE1

      firstPtr ----> NODE2 ----> NODE3 ----> NULL
                                   |
      lastPtr ----------------------
      

      <强> 8。删除NODE2

      firstPtr ----> NODE3 ----> NULL
                       |
      lastPtr ----------
      

      <强> 9。删除NODE3

      firstPtr ----> NULL
      lastPtr  ----> NULL
      

      老实说,我无法想出更好的表达方式。

答案 3 :(得分:0)

if( firstPtr == NULL) //function to check if list is empty
   firstPtr = lastPtr = newPtr; // if its empty then it assigns both firstPtr and  lastPtr to newPtr
else
{
   lastPtr->next = newPtr; // if its not the first node then it will b added at last
   lastPtr = newPtr;  // now lastPtr will be updated to point newPtr
}

例如假设您必须创建链接列表 1-2-3-NULL

这里newPtr将指向数据为 1 的节点,并且最初firstPtr和lastPtr都为NULL

firstPtr = lastPtr = newPtr // all将指向1

现在下一个节点newPtr = 2

列表不是空的 所以现在lastPtr-&gt; next = newPtr //这将使列表为1-2

lastPtr = newPtr // now lastPtr将指向2

同样,您可以在链接列表中添加其他节点。