从DoublyLinkedList中删除重复项
Removing duplicates from a DoublyLinkedList
我试图在c++中删除重复的项目。我已经设法通过使用对象的默认构造函数使对象为= null。但我无法完全将其从列表中删除。这段代码还删除了两个对象,而不是一个对象。这是另一个问题的转发。我的代码和部分帖子已经更改。如何按全名从双重链表中删除重复项。有人能帮我一下吗?下面是我的removeduplicate函数:
***Remove Duplicates***
void RemoveDuplicates(DoublyLinkedListIterator<Datatype> m_itr, string searchByFirstName, string searchBySecondName)
{
for (m_itr.Start(); m_itr.Valid(); m_itr.Forth())
{
if ((m_itr.Item().getFirstName() == searchByFirstName )
&& (m_itr.Item().getSecondName() == searchBySecondName))
{
for (m_itr.Item(); m_itr.Valid(); m_itr.Forth())
{
if ((m_itr.Item().getFirstName() == searchByFirstName )&&
(m_itr.Item().getSecondName() == searchBySecondName))
{
m_itr.Item() = Stats();
}
}
}
}
delete m_itr.Item();
}
***Remove***
void Remove(DoublyLinkedListIterator<Datatype> m_itr)
{
query.clock1();
DoublyLinkedListNode<Datatype>* node = m_head;
//Check to see if the iterator belongs to this list, if not return nothing.
if (m_itr.m_list != this)
return;
//Check to see if the node is valid, if not return nothing.
if (m_itr.m_node == 0)
return;
//If the iterator is pointing to the head...
if (m_itr.m_node == m_head)
{
//Move the iterator forward.
m_itr.Forth();
//Delete the head.
RemoveHead();
//Decrement the size.
m_count--;
}
//If the iterator is not pointing to the head...
else
{
//Search forward through the list until you find
//the node prior to the node you want to remove.
while (node->m_next != m_itr.m_node)
node = node->m_next;
// move the iterator forward.
m_itr.Forth();
//If the node being are deleted is the tail...
//Then update the tail node.
if (node->m_next == m_tail)
{
//Tail is now equal to node. Which means we can now delete the node.
m_tail = node;
}
//Delete the node.
delete node -> m_next;
//Relink the list.
node -> m_next = m_itr.m_node;
//Decrement the count because a node was removed.
m_count--;
query.clock2();
cout << "nTime Taken : " << query.time2 - query.time1 << "n";
}
}
***Class Declarations***
template <class Datatype>
class DoublyLinkedList
{
public:
//-------------------------------------------------------------------------------------------
// Member Vairables.
//-------------------------------------------------------------------------------------------
DoublyLinkedListNode<Datatype>* m_head;
DoublyLinkedListNode<Datatype>* m_tail;
int m_count;
template<class Datatype>
class DoublyLinkedListNode
{
public:
//-------------------------------------------------------------------------------------------
// Member Vairables.
//-------------------------------------------------------------------------------------------
DoublyLinkedListNode<Datatype>* m_next; //The next node.
DoublyLinkedListNode<Datatype>* m_prev; //The previous node.
Datatype m_data; //The data in the node.
template <class Datatype>
class DoublyLinkedListIterator
{
public:
//-------------------------------------------------------------------------------------------
// Member Vairables.
//-------------------------------------------------------------------------------------------
DoublyLinkedListNode<Datatype>* m_node; //A node for the Iterator to pointn to.
DoublyLinkedList<Datatype>* m_list; //A list for the Iteraotor to go through.
//-------------------------------------------------------------------------------------------
// Name: Constructor.
// Description: Constructs the DoublyLinkedListIterator.
//-------------------------------------------------------------------------------------------
DoublyLinkedListIterator(DoublyLinkedList<Datatype>* p_list= 0, DoublyLinkedListNode<Datatype>* p_node= 0)
{
m_list= p_list;
m_node= p_node;
}
// ------------------------------------------------------------------
// Name: Start
// Description: Resets the iterator to the beginning of the list.
// Arguments: None.
// Return Value: None.
// ------------------------------------------------------------------
void Start()
{
if(m_list!= 0)
m_node= m_list -> m_head;
}
// ----------------------------------------------------------------
// Name: End
// Description: Resets the iterator to the end of the list.
// Arguments: None.
// Return Value: None.
// ----------------------------------------------------------------
void End()
{
if(m_list!= 0)
m_node = m_list->m_tail;
}
// ----------------------------------------------------------------
// Name: Forth
// Description: Moves the iterator forward by one position.
// Arguments: None.
// Return Value: None.
// ----------------------------------------------------------------
void Forth()
{
if(m_node != 0)
{
m_node = m_node ->m_next;
}
}
// ----------------------------------------------------------------
// Name: Back
// Description: Moves the iterator back by one position.
// Arguments: None.
// Return Value: None.
// ----------------------------------------------------------------
void Back()
{
if(m_node!= 0)
m_node = m_node->m_prev;
}
// ----------------------------------------------------------------
// Name: Item
// Description: Gets the item that the iterator is pointing to.
// Arguments: None.
// Return Value: Reference to the data in the node.
// ----------------------------------------------------------------
Datatype& Item()
{
return m_node->m_data;
}
//-----------------------------------------------------------------
// Name: Valid
// Description: Determines if the node is valid.
// Arguments: None.
// Return Value: true if valid.
// ----------------------------------------------------------------
bool Valid()
{
return (m_node!= 0);
}
};
使用DoublyLinkedList的内置Remove函数并将迭代器传递给它。delete调用是c++的基本变体,它不处理节点的删除和正确地重新链接列表。调用delete只删除节点中的内容,而不删除节点本身!
Remove(m_itr);
代替
delete m_itr.Item();
而且,我认为你删除的地方可能是关闭的(你总是删除最后一个项目,似乎)。也许你想做下面这样的事情。不确定Stats()是做什么的,但希望您能理解。基本上,你需要把要删除的项放好,向前移动常规迭代器,然后删除要删除的项。或者,一旦删除完成,就完全停止迭代。这是必需的,因为当迭代器被删除时,它不能用于进一步的迭代。
if ((m_itr.Item().getFirstName() == searchByFirstName ) &&
(m_itr.Item().getSecondName() == searchBySecondName))
{
DoublyLinkedListIterator<Datatype> toDelete = m_itr;
m_itr.Forth();
Remove(toDelete);
}
内部for循环不为您做任何事情:它只是继续由外部循环开始的迭代。
这里有很多问题,我猜你正在学习,我为你的努力鼓掌:你伸出手来真好。
void RemoveDuplicates(DoublyLinkedListIterator<Datatype> m_itr, string searchByFirstName, string searchBySecondName)
{
for (m_itr.Start(); m_itr.Valid(); m_itr.Forth())
{
if ((m_itr.Item().getFirstName() == searchByFirstName )&& (m_itr.Item().getSecondName() == searchBySecondName))
{
if ((m_itr.Item().getFirstName() == searchByFirstName )&&
(m_itr.Item().getSecondName() == searchBySecondName))
{
m_itr.Item() = Stats();
}
}
}
delete m_itr.Item();
}
将项赋值给stats也可能不是您想要的:您想要删除项,然后从列表中删除该条目。我需要看到迭代器和双链表的api,但你需要删除项目,并从列表中删除它,可能涉及这样的东西:
m_itr.Item().m_prev.m_next = m_itr.Item().m_next;
if (m_itr.Item().m_next != null)
m_itr.Item().m_next.m_prev = m_itr.Item().m_prev;
// now that the item is spliced out of the list, delete it.
我对你使用的库一无所知,但是在标准库中,删除是这样的:
for (auto i = begin(list); i != end(list);) {
if (dont_want(*i)) {
i = list.erase(i);
} else {
++i;
}
}
基本思想是,在循环内部,要么擦除当前节点,然后由list.erase(…)
通知您在擦除当前节点后从哪个节点继续迭代,要么保留该节点,在这种情况下,通过简单地增加迭代器来移动它。
在您的代码中,++i
的等价物是m_itr.Forth()
, list.erase(…)
是Remove(…)
。然而,由于Remove()
没有返回关于如何通过列表继续进展的任何信息,您会陷入困境;你不能调用m_itr.Forth()
,因为它指向的节点已经不存在了。
我不知道我能进一步帮助你,因为我正在努力理解你的代码是如何工作的。表达式m_itr.Item().getFirstName()
表示m_itr.Item()
是对对象的引用,而delete m_itr.Item()
表示它必须是指针,而不是引用。
注意上面的代码符合c++ 11。c++ 03的等效函数是:
for (std::list<Datatype>::iterator i = list.begin(); i != list.end();) { … }
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- C++中的线程安全删除
- 如何从存储在std::映射中的std::集中删除元素
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数