我有一个双向链表,用于存储玩家对象。玩家对象包含名字,姓氏,等级和经验。我试图创建一个将删除重复的播放器对象的功能。例如,如果我输入Luis suarez然后再次输入,我希望该功能要求用户输入重复名称并删除其中一个luis suarez玩家(最好是列表中的最后一个)。我尝试了很多东西,但没有一个可以工作,也没有删除任何东西。任何人都可以帮我这个吗?
以下是我对RemoveDuplicate的尝试:
// -------------------------------------------------------------------------------------------------------
// Name: RemoveDuplicates.
// Description: Searchs through the list and finds duplicates and removes one.
// -------------------------------------------------------------------------------------------------------
void RemoveDuplicates(DoublyLinkedListIterator<Datatype> m_itr, string searchByFirstName)
{
Stats player;
string playerDuplicate = player.getFirstName();
for (m_itr.Start(); m_itr.Valid(); m_itr.Forth())
{
if (m_itr.Item().getFirstName() == searchByFirstName)
{
playerDuplicate = m_itr.Item().getFirstName();
}
}
delete(playerDuplicate);
}
我的stats类有4个带getter的成员变量。
private:
string firstName;
string secondName;
int level;
int experience;
在我的链表中,我有3个班级。
DoublyLinkedListIterator;
DoublyLinkedList;
DoublyLinkedListNode;
非常感谢任何帮助。
答案 0 :(得分:1)
看起来你必须删除链表中的实际节点(使用迭代器)。现在你只是在本地字符串变量playerDuplicate上使用delete。
完整解决方案in the follow-up.