Note that there are some explanatory texts on larger screens.

plurals
  1. POLinkedList "node jump"
    text
    copied!<p>Trying to figure out why my <code>list()</code> class pointers are being overwritten with the third node. What happens in the insert function (below) is that the third time the insert function is called my <code>headByName-&gt;nextByName</code> node pointer is overwritten by the third node, when it should point to the second. So as you can guess the 4th node is overwritten by the 5th and the 6th node is overwritten by the 7th, hence the "jump" in nodes.</p> <p>The attributes of the winery object are passed through the winery ctor and then to the <code>list::insert</code> function via these calls in main:</p> <pre><code>//main.cpp //the attributes of the winery object are the paramaters, name, location, acres, rating: list *listPtr = new list(); wineries-&gt;insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95)); wineries-&gt;insert(winery("Gallo", "Napa Valley", 200, 25)); wineries-&gt;insert(winery("Cooper Mountain", "Willamette Valley", 100, 47)); </code></pre> <p>Then the winery ctor is called where I allocate the private pointer memembers:</p> <pre><code>//winery.cpp winery::winery(const char * const name, const char * const location, const int acres, const int rating) : m_acres( acres ), m_rating( rating ) { if (name) { size_t len = strlen( name ) +1; m_name = new char[len]; strcpy_s( m_name, len, name ); } else m_name = NULL; if (location) { size_t len = strlen( location ) +1; m_location = new char[len]; strcpy_s( m_location, len, location ); } else m_location = NULL; } </code></pre> <p>And then to the issue at hand: If you can imagine this function has already been called twice then current_node will have the thrid winery. <code>headByName</code> will have the first node and in it [+], <code>headByName-&gt;next</code> will have the third. I need <code>headByName-&gt;next</code> to have the second. I am just wondering why it has been overwritten.. </p> <pre><code>// list.cpp void list::insert(const winery&amp; winery) { node *current_node = new node( winery ); // the third wineries info! node *next_node = NULL; list *list_ptr = NULL; do { if ( headByName == NULL || headByRating == NULL ) // then we are here for the first item { headByName = current_node; // the list ptrs have a node address. headByRating = current_node; } else { next_node = current_node; // transfer by the next call in main. headByName-&gt;nextByName = next_node; headByRating-&gt;nextByRating = next_node; } } while ( headByName == NULL || headByRating == NULL ); next_node = NULL; current_node = NULL; } </code></pre> <p>Could someone please find my second node? oh yeah, and the pointers that are available to me:</p> <pre><code>struct node { winery item; node * nextByName; node * nextByRating; }; class list { ... private: node * headByName; node * headByRating; }; </code></pre> <p>this might be of some importance, its the body of the node ctor:</p> <pre><code>//list.cpp list::node::node(const winery &amp;winery) : nextByName( NULL ), nextByRating( NULL ), item( winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating() ) { // where the item paramters are all pub-mem functions of the winery class. } </code></pre>
 

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