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

understanding how to add new node at end of link list

本文关键字:新节点 添加 节点 链接 列表 了解      更新时间:2023-10-16
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的指针部分指向新节点

for 2:然后,我们将LastPTR指向NEWPTR?我们为什么两次指向newptr?

有人可以解释吗?

lastptr-> next = newptr;//1这是什么做

在此阶段,lastPtr指向当前列表末尾的节点。它的next成员是无效的,因为它之后还没有任何内容。next已更新以指向新节点,因为现在新节点在上一个节点之后遵循,因此必须更新上一个节点以指向列表中的next节点 - 插入的新节点。

lastptr = newptr;//2和这个?

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

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

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.
}

您的教科书中的代码似乎是在试图启动单轨目的,而不是涵盖您应该考虑的内容,即管理列表。

  1. 此步骤将新节点连接到列表节点当前端的next指针。
  2. 此步骤建立了列表的新端。

也就是说,此代码中的链接列表模拟了 queue ,其中 firstPtr始终指向队列的"前面", lastPtr始终指向队列的"背面"。新来的人去了。如果一个人为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

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

老实说,我想不出一种更好的表达方式。

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将指向Node的数据为 1 ,最初是firstptr和LastPtr均为null

firstptr = lastptr = newptr//所有人都指向1

现在下一个节点newptr = 2

列表不是空的所以现在lastptr-> next = newptr//这将使列表为1-2

lastptr = newptr//现在lastptr将指向2

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