双重链接列表的深度复制
Deep copy of doubly linked list
我在深度复制双链表时遇到问题。这是一项家庭作业,所以我想知道为什么我的代码不工作,而不是得到我不理解的工作代码。
这是我的课:
#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();
}
这是接口:
#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 occurrence.
*/
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
我需要使用上面给出的界面。我的问题是,我在复制构造函数中收到一个错误,说"对象的限定符不兼容"或类似的话。我假设这是因为copyObject
是常数。然而,我不知道该怎么做,有人能告诉我我在这里错过了什么吗?我是C++的新手,我对Java更有经验,所以这可能是我感到困惑的原因。
编辑:
感谢您的回复。我想我即将想出如何成功地进行深度复制。我已经更新了我的代码,以显示到目前为止我已经完成的内容。现在我已经编译了代码,我得到了一个新的错误。每次运行它时都会出现"未处理的异常0xc0000005"。我在谷歌上搜索了它,并认为这是由于试图取消引用空指针而导致的错误。调试器显示它是在我的releaseNodes()
方法中抛出的。
void TextAnalyzer::releaseNodes()
{
Node* del = tail;
while(tail != NULL)
{
tail = tail->previous; //error on this line
tail->next = del;
delete del;
del = tail;
}
delete [] head;
delete [] tail;
head = tail = del = NULL;
}
上面只是我的releaseNodes()
方法,其中有一条注释显示调试器指出错误的来源。我想看看我的其余代码是否有效,因为我是C++的新手,很可能我的逻辑在其他地方也有缺陷,不幸的是,在这个错误得到解决之前,我无法测试任何东西。我仍在追踪我的代码,试图找出可能的原因。如果有人能给我指明正确的方向,我将不胜感激。
我假设这是写的行
void TextAnalyzer::copyNodes(Node* const copyHead)
{
head->next = new Node(*copyHead);
}
您只是为头中的下一个指针指定了一个新的Node对象。基本上,您只复制一个节点,节点在头后面,而不是整个列表。您已经掌握了在带有while循环的releaseNode中应该做什么的正确想法。
此外,您在复制构造函数中错误地调用了它。您在定义中给了它一个参数,但您调用的是构造函数中不接受任何参数的参数。
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- C++尝试深度复制唯一指针时出现内存访问冲突
- 在 c++ 中使用深度复制的运算符重载
- 如何深度复制链表对象指针
- 我有两棵二叉树.我想在不更改输入树的情况下深度复制两个二叉树的结果
- 如何将结构深度复制到向量的每个元素
- 我需要做一个深度复制,我是否正确使用了我的复制构造函数?
- 是否可以在不显式迭代每个元素的情况下深度复制指针容器?
- 如何深度复制unique_ptr向量
- 二叉搜索树 深度复制和取消引用
- 链表的这个别名运算符是否会产生深度复制
- 有没有一种 stl 方法来执行指针向量的深度复制
- cv::P oint3f 赋值运算符是否执行"深度"复制?
- Qt:通过深度复制访问列表中的数据结构是否应该比通过指针访问它慢得多
- 深度复制包含引用成员(C++)的结构
- 如何将“char *”深度复制到std::stringstream
- 如何在双向链表上执行深度复制
- 在复制构造函数中执行深度复制
- 深度复制cv::垫子替代品,哪个更好?
- 在 C/C++ 中使用 ProtoBuf 进行深度复制