How to remove duplicates from a doubly linked list by full name

孤街醉人 提交于 2019-12-11 20:33:25

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/16124663/how-to-remove-duplicates-from-a-doubly-linked-list-by-full-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!