Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your <code>size()</code> function you are modifying the list in the loop. You don't want to modify <code>tempPtr-&gt;link</code> but rather just change <code>tempPtr</code> as you iterate. Changing <code>tempPtr</code> won't modify anything permanently. You should also avoid passing <code>flist</code> by reference here as there's no need to modify it. So:</p> <pre><code>int size(Node *flist) { int count = 0; Node *tempPtr = flist; while (tempPtr != 0) { count += 1; tempPtr = tempPtr-&gt;link; } return count; } </code></pre> <p>As for <code>push()</code>, the biggest problem is that you're allocating the new node as a local variable which means it'll be on the stack and will get destroyed when the function returns. To create a node that is more permanent you need to allocate it on the heap using the <code>new</code> operator. And again the '&amp;' for <code>flist</code> is unnecessary:</p> <pre><code>Node* push(Node *flist, string name) { Node *tempPtr = new Node; tempPtr-&gt;fileName = name; tempPtr-&gt;link = flist; cout &lt;&lt; tempPtr-&gt;fileName &lt;&lt; endl; cout &lt;&lt; tempPtr-&gt;link-&gt;fileName &lt;&lt; endl; return tempPtr; } </code></pre> <p>Note that the counterpart to <code>new</code> is <code>delete</code>. Since the new nodes are allocated on the heap they will not be destroyed automatically so you will need to manually <code>delete</code> them when you are done with the list. Your goal is to have one <code>delete</code> for every <code>new</code>, so if you <code>new</code> 5 nodes your code should <code>delete</code> 5 nodes when it cleans up. If you don't do this your program will run fine but it will have a small memory leak.</p> <p>(Actually, when it exits all allocated memory is automatically freed. But it's a bad habit to allocate memory and never free it, in general, so you should pretend this automatic cleanup doesn't happen.)</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