Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting and Re-Adding Nodes in C++
    primarykey
    data
    text
    <p>I'm using a linked list to implement a concordance program. If the same word is read multiple times, I need to delete the current node, increment the count, and add a new node. I can't add any more functions to this program. I'm thinking I'll have to use get_count somehow, but I'm not sure.</p> <p>For example, instead of it looking like this:</p> <p>THE 1<br> THE 1</p> <p>It should be:</p> <p>THE 2</p> <p>How can I accomplish this? Thanks in advance!</p> <p>Header file:</p> <pre><code>#ifndef CONCORDANCE_H #define CONCORDANCE_H #include &lt;iostream&gt; #include &lt;cstdlib&gt; const int MAX = 8; class Concordance { public: typedef char Word[MAX+1]; // CONSTRUCTOR Concordance() { first = NULL; } // DESTRUCTOR ~Concordance(); // MODIFICATION MEMBER FUNCTIONS void insert(Word&amp; word, int&amp; n); void remove(Word&amp; word); int get_count(Word&amp; word); // OTHER FUNCTIONS int length() const; friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; out_s, Concordance&amp; c); private: // NODE STRUCT struct Node { Word wd; int count; Node *next; }; Node *first; // GET_NODE FUNCTION Node* get_node(Word&amp; word, int&amp; count, Node* link); }; #endif </code></pre> <p>Class:</p> <pre><code>//class definition #include "concordance.h" #include &lt;iostream&gt; #include &lt;cstring&gt; #include &lt;iomanip&gt; using namespace std; Concordance::~Concordance() { Node *temp; while(first != NULL) { temp = first; first = first -&gt; next; delete temp; } } void Concordance::insert(Word&amp; word, int&amp; n) { Node *prev; if(first == NULL || strcmp(first -&gt; wd, word) &gt; 0) first = get_node(word, n, first); else { prev = first; while(prev -&gt; next != NULL &amp;&amp; strcmp(prev -&gt; next -&gt; wd, word) &lt; 0) prev = prev -&gt; next; prev -&gt; next = get_node(word, n, prev -&gt; next); } } void Concordance::remove(Word&amp; word) { Node *prev, *temp; prev = temp; if(prev -&gt; wd == word) { first = first -&gt; next; delete prev; } else { while(strcmp(prev -&gt; next -&gt; wd, word) &gt; 0) prev = prev -&gt; next; temp = prev -&gt; next; prev -&gt; next = temp -&gt; next; delete temp; } } int Concordance::get_count(Word&amp; word) { while(strcmp(first -&gt; wd, word) != 0) first = first -&gt; next; return first -&gt; count; } int Concordance::length() const { Node *cursor; int length; length = 0; for(cursor = first; cursor != NULL; cursor = cursor -&gt; next ) length++; return length; } Concordance::Node* Concordance::get_node (Word&amp; word, int&amp; count, Node* link) { Node *temp; temp = new Node; strcpy(temp-&gt; wd, word); temp-&gt; next = link; temp -&gt; count = count+1; return temp; } ostream&amp; operator &lt;&lt; (ostream&amp; out_s, Concordance&amp; c) { Concordance::Node *output; out_s &lt;&lt; "Word" &lt;&lt; setw(10) &lt;&lt; " " &lt;&lt; "Count" &lt;&lt; setw(8) &lt;&lt; endl; out_s &lt;&lt; "--------------------" &lt;&lt; endl; for(output = c.first; output != NULL &amp;&amp; output-&gt;next != NULL; output = output-&gt; next ) out_s &lt;&lt; left &lt;&lt; setw(10) &lt;&lt; output-&gt; wd &lt;&lt; right &lt;&lt; setw(9) &lt;&lt; output -&gt; count &lt;&lt; endl; if(output != NULL) out_s &lt;&lt; output-&gt; wd &lt;&lt; setw(13) &lt;&lt; " " &lt;&lt; output -&gt; count &lt;&lt; endl; out_s &lt;&lt; "--------------------" &lt;&lt; endl; return out_s; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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