Note that there are some explanatory texts on larger screens.

plurals
  1. PODeep copy of doubly linked list
    primarykey
    data
    text
    <p>I am having trouble doing a deep copy of my doubly linked list. This is a homework assignment, so I'd like to know why my code is not working, rather than get working code that I don't understand.</p> <p>Here is my class:</p> <pre><code>#include "textAnalyzer.h" #include &lt;string&gt; #include &lt;iostream&gt; #include &lt;sstream&gt; TextAnalyzer::Node* TextAnalyzer::createNode(const string&amp; 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-&gt;previous; tail-&gt;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-&gt;next; for(Node* np = copyHead-&gt;next; np != NULL; np = np-&gt;next) { iter-&gt;next = new Node(*np); iter = iter-&gt;next; } iter = NULL; } TextAnalyzer::TextAnalyzer():head(createNode("0",0,NULL,NULL)),tail(head) {} TextAnalyzer::TextAnalyzer(const TextAnalyzer&amp; copyObject) { copyNodes(copyObject.head); } TextAnalyzer::~TextAnalyzer() { releaseNodes(); } TextAnalyzer TextAnalyzer::operator=(const TextAnalyzer&amp; assignObject) { return TextAnalyzer(assignObject); } void TextAnalyzer::insertWord(const string&amp; word) { Node* iter = head-&gt;next; while(iter != NULL) { if(iter-&gt;word == word) iter-&gt;wordFrequency++; else if(iter-&gt;word[0] == word[0] &amp;&amp; iter-&gt;next != NULL) { Node* temp = iter-&gt;next; iter-&gt;next = createNode(word, 1, iter, temp); iter = iter-&gt;next; temp-&gt;previous = iter; temp = NULL; } else if(iter-&gt;word[0] == word[0] &amp;&amp; iter-&gt;next == NULL) { iter = createNode(word, 1, tail, NULL); tail = iter; } else iter = iter-&gt;next; } iter = NULL; } int TextAnalyzer::wordCount() const { Node* iter = head-&gt;next; int count = 0; while(iter != NULL) count++; return count; } int TextAnalyzer::wordCountWithInitialCharacter(const char startsWith) { Node* iter = head-&gt;next; int count = 0; for(int i = 0; i &lt; wordCount(); i++) { if(startsWith == iter-&gt;word[0]) count++; iter-&gt;previous = iter; iter = iter-&gt;next; } iter = NULL; return count; } string TextAnalyzer::toString() const { Node* iter = head-&gt;next; string desc = "List of words: \n"; ostringstream convert; for(int i = 0; i &lt; wordCount(); i++) { convert &lt;&lt; iter-&gt;word[0] &lt;&lt; " words:\n" &lt;&lt; iter-&gt;word &lt;&lt; "(" &lt;&lt; iter-&gt;wordFrequency &lt;&lt; ")\n"; iter-&gt;previous = iter; iter = iter-&gt;next; } iter = NULL; return desc + convert.str(); } </code></pre> <p>Here is the interface:</p> <pre><code>#ifndef TEXTANALYZER_H #define TEXTANALYZER_H #include &lt;iostream&gt; #include &lt;string&gt; 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&amp; 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&amp; 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&amp; 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&amp; 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&amp; 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:] * [&lt;word&gt;(&lt;count&gt;)] * [&lt;word&gt;(&lt;count&gt;)] * ... * * [B words:] * [&lt;word&gt;(&lt;count&gt;)] * [&lt;word&gt;(&lt;count&gt;)] * ... * *... */ string toString() const; }; #endif </code></pre> <p>I am required to use the interface given above. My problem is that I get an error in my copy constructor saying "The object has qualifiers that are not compatible" or something similar. I am assuming this is because <code>copyObject</code> is constant. However, I am at a loss as to how to do this otherwise, can someone tell me what I am missing here? I am fairly new to C++, I am more experienced with Java so that could be why I'm being confused.</p> <p><strong>EDIT:</strong></p> <p>Thanks for the responses. I think I was about to figure out how to successfully do a deep copy. I've updated my code to show what I have completed so far. Now that I have compiled the code, I've gotten a new error. "unhandled exception 0xc0000005" every time I run it. I googled it and believe it to be an error caused by attempting to dereference a null pointer. The debugger shows it is thrown in my <code>releaseNodes()</code> method.</p> <pre><code>void TextAnalyzer::releaseNodes() { Node* del = tail; while(tail != NULL) { tail = tail-&gt;previous; //error on this line tail-&gt;next = del; delete del; del = tail; } delete [] head; delete [] tail; head = tail = del = NULL; } </code></pre> <p>Above is just my <code>releaseNodes()</code> method, with a comment showing where the debugger says the error originates. Id like to see if the rest of my code works since I am new to C++ and it is very likely my logic is flawed elsewhere as well, unfortunately until this error is resolved I can't test anything. I'm still tracing through my code trying to find what could be causing it. If anyone can point me in the right direction it would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload