了解如何在链接列表末端添加新节点
understanding how to add new node at end of link list
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;
};
假设NodeType
是Node
,它会创建一个新的节点。创建新节点后,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.
}
您的教科书中的代码似乎是在试图启动单轨目的,而不是涵盖您应该考虑的内容,即管理列表。
- 此步骤将新节点连接到列表节点当前端的
next
指针。 - 此步骤建立了列表的新端。
也就是说,此代码中的链接列表模拟了 queue ,其中 firstPtr
始终指向队列的"前面", lastPtr
始终指向队列的"背面"。新来的人去了。如果一个人为null,他们都最好是。因此,您可以假设:
if (lastPtr == NULL) // if this is true, the list is empty.
因此,在新到达时考虑这一点:
- 如果列表为空,
firstPtr
和lastPtr
应该指向新节点。 - 否则,
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;
}
一张图片说一千个字。以下操作将生成相应的图:
- 从一个空列表开始
- 添加node0
- 添加node1
- 添加node2
- 删除Node0
- 添加node3
- 删除Node1
- 删除Node2
- 删除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
同样,您可以在链接列表中添加其他节点。
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- 为什么我的节点在我设置后被设置为 nullptr = 新节点?
- 指向二叉树中新节点的指针
- 将树节点添加到向量向量中的 n 元树遍历的平均和最坏情况时间复杂度是多少?
- 将新元素添加到列表中,并返回对该元素的引用?
- 将节点添加到链接列表中,而无需明确分配内存空间(无需使用新)
- 添加新节点后,链接列表的负责人总是无效
- 推送功能(将新节点添加到列表顶部)C
- C++链表将新节点添加到列表的开头
- 了解如何在链接列表末端添加新节点
- 将新节点添加到列表中并动态命名
- C++:尝试将新节点添加到链表会产生"线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)"错误
- 在头部添加新节点
- 创建一个函数,该函数返回一个新的相同二叉树,并向所有预先存在的叶添加新节点
- 链表只打印添加新节点后的最后一个元素
- 当我尝试将新节点添加到列表中时,我收到错误消息"no matching function call"
- 将新节点添加到具有两个类的链表中
- 迭代地向二叉树中添加一个新节点,根被覆盖
- 我的单链表中的一个函数(在找到特定节点后添加新节点)使我的程序崩溃
- 向链表添加新节点的新方法