异常抛出错误

时间:2016-10-09 21:15:42

标签: c++ debugging exception

我正在试图找出异常的位置,我无法在我的程序中找到它。程序列表的作用是查找节点并删除它们。这是我的头文件:

#ifndef DOUBLYLIST_H
#define DOUBLYLIST_H

#include <string>
#include <iostream>
using namespace std;

class Node
{
public:
    Node() : data(0), previousLink(NULL), nextLink(NULL) {}
    Node (int theData, Node *previous, Node *next)
            : data(theData), previousLink(previous), nextLink(next){}
    Node *getNextLink( ) const { return nextLink; }
    Node *getPreviousLink( ) const { return previousLink; }
    int getData( ) const { return data; }
    void setData(int theData) { data = theData; }
    void setNextLink(Node *newNext) { nextLink = newNext; }
    void setPreviousLink(Node *newPrev) { previousLink = newPrev; }
    ~Node(){}
private:
    int data;
    Node *nextLink;
    Node *previousLink;
};


class DoublyList
{
public:
DoublyList();   

void insertBack(int newData);
bool search(int searchData) const;
void deleteNode(int deleteData);    
void print() const;
void reversePrint() const;

void destroyList();
~DoublyList();

private:
    Node *first;    // pointer to the first node on the list
    Node *last;     // pointer to the last node on the list
    int count;      // number of nodes in the list
};
#endif

这是cpp文件:

/*
    Huynh, Dex
    CS A250
    October 9, 2016

    Lab - 7 Dll Error
*/
#include "DoublyList.h"

DoublyList::DoublyList()
{
    first = NULL;
    last = NULL;
    count = 0;
}

DoublyList::~DoublyList()
{
    destroyList();
}

void DoublyList::insertBack(int newData)
{
    Node *newNode = new Node(newData, last, NULL);

    if (first == nullptr)
        first = newNode;
    else
    {
        last->setNextLink(newNode);
        newNode->setPreviousLink(last);
    }
}

bool DoublyList::search(int searchData) const
{
    Node *current = first;  

    while (first != nullptr)    
    {       
        if (current->getData() == searchData)
            return true;
        else
            current = current->getNextLink();   
    }
}

void DoublyList::deleteNode(int deleteData) 
{
    if (first == nullptr)       
    {
        cerr << "Cannot delete from an empty list." << endl;
    }
    else  
    {
        Node *current;      

        if (first->getData() == deleteData)   
        {
            current = first;                
            first = first->getNextLink();   

            if (first == NULL)  
                last = NULL;                

            delete current;
            current = NULL;     
        }
        else 
        {
            bool found = false;                             
            current = first;    

            while (current != NULL || !found) 
            {                 
                if (current->getData() == deleteData) 
                    found = true;                     
                else
                    current = current->getNextLink(); 
            }

            if (current == NULL)    
                cerr << "The item to be deleted is not in the list." << endl;
            else         
            {
                if (current != last)        
                    current->getPreviousLink()->setNextLink(current->getNextLink());
                else
                    last = current->getPreviousLink();          

                --count;
                delete current;
                current = NULL;
            }
        }
    }   
}

void DoublyList::print() const
{
    if (first == nullptr)
        cerr << "List is empty. Cannot print." << endl;
    else
    {
        Node *temp = first;

        while (temp != NULL)
        {
            cout << temp->getData() << " ";
            temp = temp->getNextLink();
        }
        cout << endl;
    }   
}

void DoublyList::reversePrint() const
{
    if (first == nullptr)
        cerr << "List is empty. Cannot print." << endl;
    else
    {
        Node *temp = last;

        while (temp != NULL)
        {
            cout << temp->getData() << " ";
            temp = temp->getPreviousLink();
        }
        cout << endl;
    }
}

void DoublyList::destroyList()
{ 
    Node  *temp;

    while (first != NULL)
    {
        first = temp;
        temp = temp->getNextLink();
        delete first;
        first = NULL;
    }

    last = NULL;    
    count = 0;
}

1 个答案:

答案 0 :(得分:1)

Node  *temp;
...
temp = temp->getNextLink();

destroyList中,此代码使用未初始化的值。程序的行为未定义。