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?
有人可以解释一下吗?
答案 0 :(得分:2)
lastPtr-&gt; next = newPtr; // 1这是做什么的
在此阶段,lastPtr
指向当前位于列表末尾的节点。它的next
成员为null,因为它之后没有任何内容。更新next
以指向新节点,因为新节点现在跟在上一个节点之后,因此必须更新上一个节点以指向列表中的next
节点 - 新节点正在插入
lastPtr = newPtr; // 2和这个?
这告诉列表新节点现在是列表中的最后一个节点。
答案 1 :(得分:1)
节点保存数据和指向下一个节点的指针,以便列表可以链接:
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.
}
答案 2 :(得分:1)
您教科书中的代码似乎试图扼杀单轨目的,而不是包含您应该考虑的内容,即管理列表。
next
指针。 也就是说,此代码中的链接列表模拟队列,其中firstPtr
始终指向&#34;前面&#34;队列中,lastPtr
始终指向&#34;返回&#34;新来者去的队列。如果一个是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;
}
一张图片说千言万语。以下操作将生成相应的图表:
<强> 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
同样,您可以在链接列表中添加其他节点。