Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code is a mess. There are a number of problems both semantic and logical, but fundamentaly the logic that decides where to insert new nodes is the most flawed. Change it to this (note my new code in the else block):</p> <pre><code>void recAdd(const char*t) { char temp[bufferSize]; strcpy(temp, t); p = new record; temp[strlen(temp)] = '\0'; for (int j=0;j&lt;bufferSize;j++) p-&gt;cont[j] = temp[j]; if (start==NULL) { start=p; start-&gt;next = 0; } else { record* x = start; record* prev = 0; while( x &amp;&amp; recComp(x-&gt;cont, p-&gt;cont) &lt;= 0 ) { prev = x; x = x-&gt;next; } // p is a new node. p, x and prev are arranged thusly: // prev -&gt; p -&gt; x // if prev is null, p is a new head // if x is null, p is a new tail // otherwise, p is inserted between prev and x if( !prev ) { p-&gt;next = start; start = p; } else if( !x ) // note this block and the next one could be combined. // done this way for clarity. { prev-&gt;next = p; p-&gt;next = 0; } else { p-&gt;next = x; prev-&gt;next = p; } } for (int j=0;j&lt;bufferSize;j++) temp[j] = NULL; }; </code></pre> <p>BUT the fact that you had enough difficulty writing this code that you would ask SO for help in fixing it illustrates an important point: the best code is code that you never have to write. You have written both a linked list type structure (bare bones tho it may be) and a sorting algorithm. Both are flawed, and both have working, tested and efficient versions available as part of the standard C++ libraries. You should be using them. Use strings instead of char*s. Use vectors instead of your linked list. Use <code>sort</code> instead of your hand rolled sorting algorithm. Taken together, all your code can be replaced by this:</p> <pre><code>vector&lt;string&gt; records; // this for block just populates the vector with random strings for( int i = 0; i &lt; 10; ++i ) { string s; for( int j = 0, jx = 3+(rand()/(RAND_MAX/10)); j &lt; jx; ++j ) s += 'A'-1+(rand()/(RAND_MAX/26)); cout &lt;&lt; s &lt;&lt; endl; records.push_back(s); } sort(records.begin(), records.end()); copy( records.begin(), records.end(), ostream_iterator&lt;string&gt;(cout, " ")); </code></pre> <p>Why hand-roll a bunch of stuff and expose yourself to countless defects when you can use tools that already work and do what you want?</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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