Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This recursive approach isn't going to work very well, because as you've spotted when you remove items from the array you'll have to move everything down. This will mess up the offsets in the list for the parent recursive call.</p> <p>You could modify the call to <code>merge</code> so that the arguments are pointers and they get updated to the new values if the list is resized. Then you'd also have to have <code>remove_duplicates</code> also return these values in the same way. This is likely to be quite fiddly, but if you're changing the size of the list then I don't see any other approach - parent calls in the recursion chain need to know that their bounds have changed by the inner calls deleting items from the list. Just remember to take a copy of the values passed in by pointer before passing them into the next call down, or you'll just have pointers to the same variables all the way down and that will break things horribly.</p> <p>However, do you really need to use a recursive approach? I don't think it's either the easiest or most efficient approach - in this case, an iterative method is likely to be far easier to implement.</p> <p>You seem to be using linked lists, so an iterative approach might look like this:</p> <pre><code>void remove_duplicates(ListItem *items) { while (items) { ListItem *current = items; while (current-&gt;next) { if (current-&gt;next-&gt;item == items-&gt;item) { current-&gt;next = current-&gt;next-&gt;next; } else { current = current-&gt;next; } } items = items-&gt;next; } } </code></pre> <p>I've assumed your lists are terminated by a NULL <code>next</code> pointer in this example, but it should be easy to adapt for length-bounded lists as well. I've also assumed it's only singly-linked - the basic approach for doubly-linked lists would be similar but of course you need to update both next and previous pointers.</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. This table or related slice is empty.
    1. This table or related slice is empty.
    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