DoublyLinkedList.当尝试在2个节点之间插入时不做任何事情
DoublyLinkedList. Does nothing when trying to insert between 2 nodes
我有一个问题与我的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++;
}
方法// ----------------------------------------------------------------
// 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的实现,而在于实际的插入函数。提前谢谢你,贝卡。
方法
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++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- 有效地使用std::unordered_map来插入或增加键的值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 正在插入动态数组
- 插入或删除时获取usb的dos_name
- 叮叮当当在修复时插入多个"覆盖"说明符
- 链表c++插入,所有情况都已检查,但没有任何工作
- 将重物插入std::map
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 在数字之间插入 + 或 - 符号以使其等于整数
- 在字符串中插入空格
- 频率字长,程序只打开cmd,但不做任何事,c++