0

我正在尝试删除 C++ 中的重复项。我已经设法通过使用对象默认构造函数将对象设置为 = null。但我无法将其从列表中完全删除。此代码还删除了两个对象,而不仅仅是一个。这是另一个问题的重新发布。我的代码和部分我的帖子已经改变。如何按全名从双向链表中删除重复项。谁能帮我这个?这是我的 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);
    }
};
4

3 回答 3

2

使用 DoublyLinkedList 的内置 Remove 函数并将其传递给迭代器。delete调用是简单的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);
    }
于 2013-04-20T23:17:45.550 回答
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.

希望这可以帮助。

于 2013-04-20T23:33:49.147 回答
1

我对您正在使用的库一无所知,但在标准库中,随手删除是这样的:

for (auto i = begin(list); i != end(list);) {
    if (dont_want(*i)) {
        i = list.erase(i);
    } else {
        ++i;
    }
}

基本思想是,在循环内部,要么您正在擦除当前节点,并且list.erase(…)在它擦除当前节点后通知您从哪个节点继续迭代,或者您保留该节点,在这种情况下您只需增加迭代器即可越过它。

在您的代码中,等效于++iism_itr.Forth()list.erase(…)is 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();) { … }
于 2013-04-20T23:22:07.187 回答