简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM