为什么我在析构函数中得到Debug Assertion Failed Expression:_CrtlsValidHeapPointer(block)

时间:2015-11-23 00:42:17

标签: visual-studio-2010 visual-c++

template<class T>
LinkedList<T>::~LinkedList()
{
    ListNode *nodePtr;          // To traverse the list
    ListNode *nextNode;         // To point to the next node

将nodePtr放在列表的开头

nodePtr = head;

虽然nodePtr不在列表的末尾......

    while (nodePtr != nullptr)
    {
        // Save a pointer to the next node
        nextNode = nodePtr->next;

        // Delete the current node
        delete nodePtr;

        // Position nodePtr at the next node
        nodePtr = nextNode;
    }
}

template <class T>
void LinkedList<T>::appendNode(T val)
{
    ListNode *newNode;          // To point to a new node
    ListNode *nodePtr;          // To move through the list

    // Allocate a new node and store val there
    newNode = new ListNode;
    newNode->value = val;
    newNode->next = nullptr;

    cout << "Adding " << val << " to the end of the list" << endl;

    // If there are no nodes in the list
    // make newNode the first node
    if (!head)
        head = newNode;
    else    // Otherwise, insert newNode at end
    {
        // Initialize nodePtr to head of list
        nodePtr = head;

        // Find the last node in the list
        while (nodePtr->next)
            nodePtr = nodePtr->next;

        // Insert newNode as the last node
        nodePtr->next = newNode;
    }
}

template <class T>
void LinkedList<T>::insertNode(T val)
{
    ListNode *newNode;          // A new node
    ListNode *nodePtr;          // To traverse the list
    ListNode *previousNode;     // The previous node

    // Allocate a new node and store val there
    newNode = new ListNode;
    newNode->value = val;

    cout << "Inserting " << val << " into the list" << endl;

    // If there are no nodes in the list
    // make newNode the first node
    if (!head)
    {
        head = newNode;
        newNode->next = nullptr;
    }
    else    // Otherwise, insert newNode
    {
        // Position noePtr at the head of list
        nodePtr = head;

        // Initialize previousNode to nullptr
        previousNode = nullptr;

        // Skip all nodes whose value is less than val
        while (nodePtr != nullptr && nodePtr->value < val)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        // If the new node is to be the 1st in the list,
        // insert it before all other nodes
        if (previousNode == nullptr)
        {
            head = newNode;
            newNode->next = nodePtr;
        }
        else     // Otherwise insert after the previous node
        {
            previousNode->next = newNode;
            newNode->next = nodePtr;
        }
    }

}

template<class T>
void LinkedList<T>::deleteNode(T val)
{
    ListNode *nodePtr;          // To traverse the list
    ListNode *previousNode;     // To point to the previous node

    // If the list is empty, do nothing 
    if (!head)
        return;

    cout << "Removing " << val << " from the list" << endl;

    // Determine if the first node is the one 
    if (head->value == val)
    {
        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }
    else
    {
        // Initialize nodePtr to head of the list
        nodePtr = head;
        previousNode = nullptr;

        // Skip all nodes whose value member is
        // not equal to val
        while (nodePtr != nullptr && nodePtr->value != val)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        // If nodePtr is not at the end of the list,
        // link the previous node to the node after 
        // nodePtr, then delete nodePtr
        if (nodePtr)
        {
            previousNode->next = nodePtr->next;
            delete nodePtr;
        }
    }
}

template <class T>
void LinkedList<T>::displayList() const
{
    ListNode *nodePtr;          // To move through the list

    // Position at the head of the list
    nodePtr = head;

    cout << endl << "Here is the list:" << endl;

    // While nodePtr points to a node, traverse
    // the list
    while (nodePtr)
    {
        // Display the value in this node
        cout << nodePtr->value << endl;

        // Move to the next node
        nodePtr = nodePtr->next;
    }

    cout << endl;

}

0 个答案:

没有答案