如何解释这个双向链表代码
How to explain this doubly linked list code?
这段
关于双向链表的代码解释正确吗?
我无法理解代码的一部分。这段代码发生了什么?
我已经解释了部分代码,但不太确定它是否正确。
void DoublyLinkedList::SortedInsert(const int& new_element) {
if (new_element != 0) { //creates new node np with value equal to new element
Node* np = new Node(new_element); //if new element is not equal to zero and if its not head, it points to both
head and tail
if (!Head)
{
Head = np;
Tail = np;
}
else if (new_element < Head->Element) //if new element less than element in Head np's next pointer pointing to
Head and Head prev pointer pointing to np and Head is pointing to np
{
np->Next = Head;
Head->Prev = np;
Head = np;
}
else //if new element is greater than Head we take cur node which is pointing
to Head's next pointer pointing to node after Head
{
Node *cur = Head->Next;
while ((cur) && (new_element >= cur->Element)) //new element greater than Node after Head,cur will point to cur's next pointer which is node after cur
cur = cur->Next;
if (cur)
{ // ?? whats happening here
np->Prev = cur->Prev;
np->Next = cur;
cur->Prev->Next = np;
cur->Prev = np;
}
else // ?? whats happening here
{
Tail->Next = np;
np->Prev = Tail;
Tail = np;
}
}
}
我已经改变了似乎有问题的地方。
请记住,在 dll 中,将有 2 个指针,一个指向下一个元素,另一个指向上一个元素。dll 的每个元素都有数据、*next 和 *prev。
void DoublyLinkedList::SortedInsert(const int& new_element( {
if (new_element != 0) { //creates new node np with value equal to new element
Node* np = new Node(new_element);
//Here we create just a pointer of type 'node' if new element is not
//equal to zero and if no head exists then obviously this is the 1st
//element, hence head and tail points to both 1st element.
if (!Head)
{
Head = np;
Tail = np;
}
//if new element less than element in Head np's next pointer
//pointing to Head and Head prev pointer pointing to np and Head is
//pointing to np
//Here it seems you are thinking wrong. Remember head never has next
//or previous pointer. It always points to first element unless we
//make it move. You mentioned Head np's next pointer, that will be
//Head->element->next and not head->element. Head->element means the
//first position and if our list already has a element to which head
//is pointing we compare it with our new element which we want to
//insert.
else if (new_element < Head->Element)
{ //if new elm is less than 1st elm
np->Next = Head; //now new elm bcms 1st elm as it next pointer points to the one is currently being pointed by head
Head->Prev = np; //now elm pointed by head prev pointer will point to new element
Head = np; //finally our new element is 1st //element in list hence we assigb head pointer to point towards it.
}
//this is the condition when new elmt which we are inserting is
//greater than our element pointed by head. Thus obviously
//we need to traverse and find the element which is greater than
//our new element. This code does that and then adjusts the next and
//previous pointer
else
{
Node *cur = Head->Next;
while ((cur) && (new_element >= cur->Element))
cur = cur->Next;
if (cur)
{
np->Prev = cur->Prev;
np->Next = cur;
cur->Prev->Next = np;
cur->Prev = np;
}
else
{
Tail->Next = np;
np->Prev = Tail;
Tail = np;
}
}
}
希望对您有所帮助!
相关文章:
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 如何实现容纳整数和无效指针的双向链表?
- 为什么我的双向链表删除函数会删除多个节点?
- 我对数据结构、双向链表有一些问题
- C++ 双向链表:使用矩阵类成员创建和填充列表
- 在 c++ 中具有向量的双向链表构造函数
- 如何在双向链表上实现复制赋值?
- 在C++中删除双向链表的头节点后出现访问冲突异常
- 在双向链表中实现 Popback
- 在C++中反转双向链表
- 双向链表 - 无法删除第一个节点
- 我当前实现的双向链表类是否需要重构迭代器 end() 功能?
- 双向链表 std::unique_ptr 类在节点删除时无法按预期工作
- C++ 双向链表 - 插入同时保持递增顺序
- 如何找到双向链表的最大元素?
- 如何实现双向链表的迭代器?
- 双向链表问题 - 包含项目但不显示
- 双向链表未正确打印值
- 我在 c++ 中的链表代码只附加一个值
- 如何解释这个双向链表代码