从DoublyLinkedList中删除重复项

时间:2013-04-20 23:08:45

标签: c++

我正在尝试删除C ++中的重复项。我已经设法通过使用对象默认构造函数将对象设置为= null。但我无法将其从列表中完全删除。此代码还删除了两个对象而不是一个。这是另一个问题的重新发布。我的代码和部分帖子已更改。How to remove duplicates from a doubly linked list by full name。谁能帮我这个?这是我的removeDuplicates函数:

***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);
    }
};

3 个答案:

答案 0 :(得分:2)

使用DoublyLinkedList的内置Remove函数并将其传递给迭代器。 删除调用是基本的C ++变体,它不处理节点删除和正确重新链接列表。对删除的调用只会删除节点中的内容,而不会删除节点本身!

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);
    }

答案 1 :(得分:2)

内部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();
}

将项目分配给统计数据也可能不是您想要的:您想要删除该项目,然后从列表中删除该项目。我需要看到迭代器和双向链表的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.

希望这有帮助。

答案 2 :(得分:1)

我对您正在使用的库一无所知,但在标准库中,删除时请执行以下操作:

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();) { … }