Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The mistake is (I think) in these three lines:</p> <pre><code>current = current.link; q = current; </code></pre> <p>(where you set <code>q</code> to be the same as <code>current</code>) and</p> <pre><code>q.link= current.link.link; </code></pre> <p>(and maybe also in using <code>length()</code> depending on it's implementation)</p> <p>To see why let's look at how to delete in more detail: Let's consider there are three nodes in your list x->y->z and you want to delete y. In order to do so you need to set <code>x.link = z</code>. </p> <p>To return to you example it means that the variable <code>q</code> should store the element before <code>current</code>, then deleting <code>current</code> can be done by </p> <pre><code>q.link = current.link; </code></pre> <p>In order order to have <code>q</code> be the predecessor of <code>current</code> you have to reverse the two lines above, i.e., use </p> <pre><code>q = current; current = current.link; </code></pre> <p>Why am I saying depending on the implementation of <code>length</code> ? If you implementation of length just returns some number maintained by increasing whenever you add a value to the list, you should also decrement it when deleting one. If length traverses the list in order to find the number of elements then its ok, although not very efficient.</p> <p>One last comment: your code will <em>not work</em> (even with the fixes I explain above) when there is no element with the given <code>regN</code>. Why? Because you always delete one element. Moreover, you might want to rethink the logic inside the loop. Currently if the element to delete is the second one and there is 1000000 elements you will run the loop nearly 1000000 times. </p>
 

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