Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting a Doubly Linked List C++
    primarykey
    data
    text
    <p>Trying to do it through a loop that traverses through the list.</p> <p>In the loop I'm feeding the head node into a sorting function that I have defined and then I'm using strcmp to find out if which name in the node should come first. </p> <p>It is not working because writing the names too early. </p> <p>Im comparing them all linearly by going down the list one node at a time and not going back to see if the first should come before the last. That part explained would be helpful.</p> <p>The two functions that are most important to me now are defined as follows: I have tried my best to do what I think is right for the sorting function.</p> <pre><code>void list::displayByName(ostream&amp; out) const { list *ListPtr = NULL; node *current_node = headByName; winery *wine_t = new winery(); // winery is another class object type // im allocating it to prevent a crash when I call it. while ( current_node != NULL ) { *(wine_t) = current_node-&gt;item; wine_t = ListPtr-&gt;sort( current_node ); out &lt;&lt; wine_t &lt;&lt; endl; current_node = current_node-&gt;nextByName; } delete wine_t; } winery * const list::sort( node * current_node ) const { // current_node is the first node. const char *SecondName = NULL, *FirstName = NULL; winery *wine_t = new winery(); if ( current_node != NULL ) { SecondName = current_node-&gt;item.getName(); current_node = current_node-&gt;nextByName; FirstName = current_node-&gt;item.getName(); } if ( strcmp( FirstName, SecondName ) == -1 ) { *(wine_t) = current_node-&gt;item; FirstName = NULL; SecondName = NULL; return wine_t; } else if ( strcmp( FirstName, SecondName ) == 1 ) { *(wine_t) = current_node-&gt;item; FirstName = NULL; SecondName = NULL; return wine_t; } else return wine_t;// then the strings are equal FirstName = NULL; SecondName = NULL; return wine_t; } </code></pre> <p>And I started to develop my nodes here:</p> <pre><code>void list::insert(const winery&amp; winery) { node *current_node = new node( winery ); if ( headByName == NULL ) { headByName = current_node; headByRating = current_node; tail = headByName; current_node-&gt;prev = current_node; } else { current_node-&gt;prev = tail; tail-&gt;nextByName = current_node; } tail = current_node; current_node = NULL; } </code></pre> <p>I think its correct in that function above. Could I possibly get away with sorting it there?</p> <p>Below are my varaibles that I am working with:</p> <pre><code>public list { ... void insert(const winery&amp; winery); void displayByName(ostream&amp; out) const; } private: struct node { node(const winery&amp; winery); // constructor winery item; node * prev; node * nextByName; node * nextByRating; }; winery * const sort(node*) const; node * headByName; node * headByRating; node * tail; }; </code></pre> <p>Any help is appreciated. Thanks very much =)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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