0
私は例外がどこにあるのか把握しようとしており、私のプログラムではそれを実際に把握できません。プログラムリストが行うことは、ノードを見つけて削除することです。ここに私のヘッダファイルです:例外がスローされたエラー
#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ファイルです:destroyList
で
/*
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;
}
'search'のループ条件はwhile(first!= nullptr)の代わりにwhile(current!= nullptr)でなければなりません。あなたの例外/エラーは何ですか? – Franck
[MCVE]を提示してください。 –
いくつかのものを修正した後の私の例外は次のとおりです。 "例外がスローされました:Project.exeの" System.NullReferenceException " 追加情報:オブジェクト参照がオブジェクトのインスタンスに設定されていません この例外のハンドラがある場合、プログラムを安全に継続することができます。そしてそれは私のヘッダーファイルの18行目を指しています、そして、私は問題が現在何かの周りに私の頭を包んでいるように見えることができません。その値を指しているように見えますが、私は理由を知らないのです。 –