gpt4 book ai didi

c++ - 双链表。尝试在 2 个节点之间插入时不执行任何操作

转载 作者:行者123 更新时间:2023-11-28 03:18:37 25 4
gpt4 key购买 nike

我的 DLL 插入函数有问题。附加到列表上没有问题,但是当我有一个包含 5 个对象的列表并且我想插入节点之间时,它什么也不做。我一直在四处寻找数小时来解决这个问题,但没有任何改变。

这是我的代码:在 main.cpp 中实现

cout << "\n---------------------------Insert---------------------------\n";
//Ask the user what position they want to enter the player in.
cout << "Enter the position you want to enter the player: ";
int insertChoice = validators.getNum();
//Move the iterator back to the start.
itr.Start();
//If the position is NOT equal to zero, Continue.
if (insertChoice != 0)
{
//Loop until the position entered is reached.
for (int i = 0; i < insertChoice; i++)
{
//Move the iterator forware to this position.
itr.Forth();
}
//Insert the Player to the position entered by the user.
//list.Insert(itr,stats.input());
}
//If the position entered is equal to zero, Continue.
else
{
//Append onto the list.(Add it onto the start).
list.Append(stats.input());
}

在头文件中插入函数。

// ----------------------------------------------------------------------------------------------------------------
// Name: Insert
// Description: Inserts data before the iterator, this works whether the iterator is backwards of forwards
// through the list.Inserts at the end of the list if iterator is invalid.
// Arguments: p_iterator: The iterator to insert before
// p_data: the data to insert
// Return Value: None.
// ----------------------------------------------------------------------------------------------------------------
void Insert(DoublyLinkedListIterator<Datatype>& p_itr, Datatype p_data)
{
if(p_itr.m_node != 0)
{
// insert the data before the iterator
p_itr.m_node->InsertBefore(p_data);

//if the iterator was at the head of the list,
//reset the head pointer
if(p_itr.m_node == m_head)
{
m_head = m_head->m_prev;
}
// increment the count
m_count++;
}
else
{
Append(p_data);
}
}

追加

void Append(Datatype p_data)
{
if(m_head == 0)
{
// create a new head node.
m_head= m_tail= new DoublyListNode<Datatype>;
m_head->m_data= p_data;
m_head->m_next= 0;
m_head->m_prev= 0;
}
else
{
// insert a new node after the tail and reset the tail.
m_tail->InsertAfter(p_data);
m_tail= m_tail->m_next;
}
m_count++;
}

InsertBefore

// ----------------------------------------------------------------
// Name: InsertBefore
// Description: This adds a node before the current node.
// Arguments: p_data - The data to store in the new node.
// Return Value: None.
// ----------------------------------------------------------------

void InsertBefore(Datatype p_data)
{
//Create new Node
DoublyLinkedListNode<Datatype>* newNode = new DoublyLinkedListNode<Datatype>;
newNode->m_data = p_data;
//Set up new Node Pointers
newNode->m_next = this;
newNode->m_prev = m_prev;
//if theres a node before it, make it point to new node
if(m_prev != 0)
m_prev = newNode;
}

我认为问题不在于 main 中的实现,而在于实际的插入函数。提前致谢,贝卡。

最佳答案

InsertBefore

newNode->m_next = this;
newNode->m_prev = this->m_prev;

您需要告诉 this 也指向(如前所述)新节点。

newNode->m_next = this;
newNode->m_prev = this->m_prev;
this->m_prev->m_next = newNode;
this->m_prev = newNode;

这也应该是

在头文件中插入函数。

 if(p_itr.m_node == m_head)
{
m_head = p_itr->m_prev;
^^^^^^
}

关于c++ - 双链表。尝试在 2 个节点之间插入时不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16084143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com