如何通过全名从双向链表中删除重复项 [英] How to remove duplicates from a doubly linked list by full name

查看:186
本文介绍了如何通过全名从双向链表中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双向链接列表,其中存储播放器对象。玩家对象包含名字,姓氏,级别和经验。我试图创建一个将删除重复播放器对象的功能。例如,如果我输入Luis suarez,然后再次输入,我希望该功能要求用户输入重复名称,并删除一个luis suarez玩家(最好是列表中的最后一个)。我尝试过很多事情,没有任何工作,也不删除任何东西。有人可以帮助我吗?



这是我在RemoveDuplicate的尝试:

  // --------------------------------------------- -------------------------------------------------- -------- 
//名称:RemoveDuplicates。
//说明:搜索列表并查找重复项并删除一个。
// -------------------------------------------- -------------------------------------------------- ---------

void RemoveDuplicates(DoublyLinkedListIterator< Datatype> m_itr,string searchByFirstName)
{
统计播放器;
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);
}

我的统计类有4个成员变量与getter。

  private:
string firstName;
string secondName;
int level;
int体验;

在我的链接列表中,我有3个课程。

  DoublyLinkedListIterator; 
DoublyLinkedList;
DoublyLinkedListNode;

任何帮助都非常感谢。



编辑:后续行动:从DoublyLinkedList中删除重复项

解决方案

看起来你必须删除链表(使用迭代器)。现在你只是在本地字符串变量playerDuplicate上使用delete。



完整解决方案


I have a doubly linked list in which it stores player objects. The player object contains first name, last name, level and experience. Im trying to create a function that will remove a duplicate player object. For instance, if I enter Luis suarez and then he is entered again, I want the function to ask the user to enter the duplicates name and delete one of the luis suarez players (preferably the one last in the list). I've tried many things and none of them work nor delete anything. Can anyone help me with this?

Here is my try at 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);
}

My stats class has 4 member variables with getters.

private:
    string firstName;
    string secondName;
    int level;
    int experience;

In my linked list I have 3 classes.

DoublyLinkedListIterator;
DoublyLinkedList;
DoublyLinkedListNode;

Any help is greatly appreciated.

EDIT: Follow-up: Removing duplicates from a DoublyLinkedList

解决方案

Looks like you have to delete the actual node in the linked list (using the iterator). Now you are just using delete on the local string variable playerDuplicate.

Full solution in the follow-up.

这篇关于如何通过全名从双向链表中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆