Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code works fine on my machine (once the <code>insert(())</code> statement is "filled in" properly as explained in the code comment). The insertion works in all positions.</p> <hr> <p><strong>Something else, though:</strong> I initially had a look at your <code>insert</code> function. I thought I'd give you a hint on how to make it a little shorter and easier to understand what's going on:</p> <pre><code>void insert(List *last) { // create a new item and populate it: List* new_item = new List; populate(new_item); // insert it between 'last' and the item succeeding 'last': new_item-&gt;nextAddr = last-&gt;nextAddr; last-&gt;nextAddr = new_item; } </code></pre> <p>This would be preferable because it first creates a new, separate item, prepare it for insertion, and only then, when this has worked successfully, will the function "mess" with the linked list. That is, the linked list is not affected except in the very last statement, making your function "safer". Contrast this with your version of <code>insert</code>, where you mix code for constructing the new item with the actual insertion. If something goes wrong inside this function, chances are far higher that the linked list is messed up, too.</p> <p>(What's still missing btw. is a initial check whether the passed argument <code>last</code> is actually valid, ie. not a null pointer.)</p> <hr> <p><strong>P.S.:</strong> Of course you could just use a standard C++ <code>std::list</code> container instead of building your own linked list, but seeing that you tagged your question <code>beginner</code>, I assume you want to learn how it actually works.</p>
    singulars
    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