Pendo826
Pendo826

Reputation: 1002

DoublyLinkedList remove error

Im making a doublyLinkedList. The error is to do with my Remove method. I cant figure this out. does anyone know?

Here is where the error is?

Error 1 error C2027: use of undefined type 'DoublyListNode' c:\users\conor\documents\college\c++\projects\repeat - doublylinkedlist\repeat - doublylinkedlist\doublylinkedlist.h 230 1 Repeat - DoublyLinkedList

// -------------------------------------------------------------------------------------------------------
//  Name:           Remove
//  Description:    Removes the node that the iterator points to, moves iterator forward to the next node.
//  Arguments:      p_iterator: The iterator to remove
//                  isForward: Tells which direction the iterator was going through the list
//  Return Value:   None.
// -------------------------------------------------------------------------------------------------------
void Remove(DoublyListIterator<Datatype>& m_itr)
{
    DoublyListNode<Datatype>* node = m_head;
    // if the iteratordoesn’t belong to this list, do nothing.
    if (m_itr.m_list != this)
        return;
    // if node is invalid, do nothing.
    if (m_itr.m_node == 0)
        return;
    if (m_itr.m_node == m_head)
    {
        // move the iteratorforward and delete the head.
        m_itr.Forth();
        RemoveHead();
        m_size--;
    }
    else
    {
        // scan 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 you are deleting is the tail,
        // update the tail node.
        if (node->m_next == m_tail)
        {
            m_tail = node;
        }
        // delete the node.
        delete node->m_next;
        // re-link the list.
        node->m_next = m_itr.m_node;
        m_size--;
    }
}

If anymore code is needed just ask. I do not want to put lots of code on Stack overflow users.

Upvotes: 3

Views: 116

Answers (2)

Pendo826
Pendo826

Reputation: 1002

The problem was a typo of the class DoublyListNode. The class was names DLNode. So this gave the error being discussed above.

Upvotes: 4

perilbrain
perilbrain

Reputation: 8197

You are checking for tail node but not for a node between head and a tail.You are breaking the chain by deleting the node even before linking it to the next member.

Let us analyze:-

while (node->m_next != m_itr.m_node)
            node = node->m_next;

after the loop node->m_next points m_itr.m_node

    delete node->m_next;
    // re-link the list.
    node->m_next = m_itr.m_node;

You are assigning a deleted node!!!!

Change the code:-

node->m_next = m_itr.m_node;
delete m_itr;

Upvotes: 3

Related Questions