Нарушение прав доступа в двусвязном списке

Я отправил ранее и получил хорошую помощь в выяснении, как глубоко копировать мой двусвязный список. У меня возникла проблема с нарушением прав доступа «0xC000000005», которое, по моему мнению, вызвано попыткой удалить нулевой указатель. Это домашнее задание, и я новичок в C ++, поэтому мне просто нужна помощь, чтобы выяснить, где я ошибаюсь, в отличие от того, кто просто дает мне рабочий код.

Вот мой интерфейс, данный мне моим профессором. Я не могу изменить его в любом случае.

#ifndef TEXTANALYZER_H
#define TEXTANALYZER_H

#include <iostream>
#include <string>

using namespace std;

class TextAnalyzer {
private:

/*
* Class: Node
*
* This class represents a node in a sorted doubly linked list that stores a
* list of words and their frequency of occurrency.
*/
class Node {
public:
string word;
int wordFrequency;
Node* previous;
Node* next;

Node(const string& word,
const int wordFrequency,
Node* const previous,
Node* const next)
: word(word),
wordFrequency(wordFrequency),
previous(previous),
next(next)
{}
}; // end ListNode
/*********************************************************************/

Node* head;
Node* tail;/*
* Releases all the memory allocated to the list.
*/
void releaseNodes();

/*
* Makes a deep copy of the object.
*/
void copyNodes(Node* const copyHead);

/*
* Returns a populated Node.
* Throws a bad_alloc exception if memory is not allocated.
*/
Node* createNode(const string& word,
const int wordFrequency,
Node* const previous,
Node* const next);

public:
/*
* Initializes head and tail, each to a dymmy node.
*/
TextAnalyzer();

/*
* Makes a deep copy of the object passed in.
* Calls copyNodes() to do the actual work.
*/
TextAnalyzer(const TextAnalyzer& copyObject);

/*
* Releases all the memory allocated to the object.
* Calls the releaseNodes() method to do the actual work.
*/
~TextAnalyzer();

/*
* Makes a deep copy of the rhs object.
*/
TextAnalyzer operator =(const TextAnalyzer& assignObject);

/*
* Inserts the word in a sorted order into the list.
*
* If no Node exists with that initial character, one is added in
* sorted order. If one does exist (same word), then the word frequency
* of that word is incremented by one.
*/
void insertWord(const string& word);

/*
* Returns a count of all the words in the list.
*/
int wordCount() const;

/*
* Returns a count of all the words with the initial character.
*/
int wordCountWithInitialCharacter(const char startsWith);

/*
* Returns a description of the object. The string is formatted as:
* [A words:]
*     [<word>(<count>)]
*     [<word>(<count>)]
*     ...
*
* [B words:]
*     [<word>(<count>)]
*     [<word>(<count>)]
*     ...
*
*...
*/
string toString() const;

};

#endif

Вот мое определение класса:

#include "textAnalyzer.h"#include <string>
#include <iostream>
#include <sstream>

TextAnalyzer::Node* TextAnalyzer::createNode(const string& word, const int wordFrequency,
Node* const previous, Node* const next)
{
return new Node(word, wordFrequency, previous, next);
}
void TextAnalyzer::releaseNodes()
{
Node* del = tail;

while(tail != NULL)
{
tail = tail->previous;
tail->next = del;
delete del;
del = tail;
}

delete [] head;
delete [] tail;

head = tail = del = NULL;
}

void TextAnalyzer::copyNodes(Node* const copyHead)
{
head = new Node(*copyHead);
Node* iter = head->next;

for(Node* np = copyHead->next; np != NULL; np = np->next)
{
iter->next = new Node(*np);
iter = iter->next;
}

iter = NULL;
}

TextAnalyzer::TextAnalyzer():head(createNode("0",0,NULL,NULL)),tail(head)
{}

TextAnalyzer::TextAnalyzer(const TextAnalyzer& copyObject)
{
copyNodes(copyObject.head);
}

TextAnalyzer::~TextAnalyzer()
{
releaseNodes();
}

TextAnalyzer TextAnalyzer::operator=(const TextAnalyzer& assignObject)
{
return TextAnalyzer(assignObject);
}

void TextAnalyzer::insertWord(const string& word)
{
Node* iter = head->next;

while(iter != NULL)
{
if(iter->word == word)
iter->wordFrequency++;
else if(iter->word[0] == word[0] && iter->next != NULL)
{
Node* temp = iter->next;
iter->next = createNode(word, 1, iter, temp);
iter = iter->next;
temp->previous = iter;

temp = NULL;
}
else if(iter->word[0] == word[0] && iter->next == NULL)
{
iter = createNode(word, 1, tail, NULL);
tail = iter;
}
else
iter = iter->next;
}

iter = NULL;
}

int TextAnalyzer::wordCount() const
{
Node* iter = head->next;
int count = 0;

while(iter != NULL)
count++;

return count;
}

int TextAnalyzer::wordCountWithInitialCharacter(const char startsWith)
{
Node* iter = head->next;
int count = 0;

for(int i = 0; i < wordCount(); i++)
{
if(startsWith == iter->word[0])
count++;

iter->previous = iter;
iter = iter->next;
}

iter = NULL;

return count;
}

string TextAnalyzer::toString() const
{
Node* iter = head->next;
string desc = "List of words: \n";
ostringstream convert;

for(int i = 0; i < wordCount(); i++)
{
convert << iter->word[0] << " words:\n"<< iter->word    << "("<< iter->wordFrequency
<< ")\n";
iter->previous = iter;
iter = iter->next;
}

iter = NULL;

return desc + convert.str();
}

Проблема возникает в моем методе releaseNodes () в соответствии с отладчиком. Я добавил к нему комментарий, чтобы указать на конкретную строку, на которой он встречается:

void TextAnalyzer::releaseNodes()
{
Node* del = tail;

while(tail != NULL)
{
tail = tail->previous; //debugger flags this line when error occurs
tail->next = del;
delete del;
del = tail;
}

delete [] head;
delete [] tail;

head = tail = del = NULL;
}

Я не уверен, что вызывает нарушение прав доступа, но, как я уже сказал, я новичок в C ++. Любая помощь приветствуется.

0

Решение

В дополнение к ответу Билла, в вашем цикле удаления есть что-то подозрительное. Насколько я понимаю, вы хотите удалить свой список обратно на фронт. Что привело вас к той петле, которая у вас сейчас есть? Кажется, что более естественный подход будет работать так: curr указатель, начиная с tail, Затем цикл: декремент curr, удалять curr->next, Остановись когда curr является head, Затем удали head,

0

Другие решения

Вы не выделяете head and tail с new []так что вы не удалите их delete []

delete [] head;
delete [] tail;

Должно быть:

delete head;
delete tail;

микшировать new с delete [] является неопределенное поведение.

1

В ваших copyNodes вам нужно также установить предыдущий указатель

0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector