0

Remove from list | error

Hi Please refer below code : I am aware that remove statement in code is causing the problem... But not sure how to solve this problem? Any suggestions plz ? I can't change function as it must be there as per main problem I am trying to solve... This is just the dummy code I have prepared to share here. Any suggestions on change is also welcome but only if that's the last option as restructure of code is not easy . https://code.sololearn.com/cG3R2pKPWyxh/?ref=app

16th Feb 2022, 8:20 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 odpowiedzi
+ 5
Based on the dummy code, you are potentially invalidating your outer iterator without knowing when you have done so, causing the segmentation fault. I can't really think of a way to fix that without restructuring at least something, but that is hard without knowing the bigger picture (e.g. are the elements guaranteed to be unique, does something need to be displayed during removal). The following would be a bandaid solution that fixes the immediate problem as long as the list contains unique elements (which seems to be the case for your pointers): https://code.sololearn.com/cC253OeTlcq9/?ref=app
17th Feb 2022, 12:31 AM
Shadow
Shadow - avatar
+ 2
Yes, your version should work for non-unique lists too.
17th Feb 2022, 4:30 PM
Shadow
Shadow - avatar
+ 1
Thanks Shadow ... I was trying to find a way and reached till the conclusion of iterator issue handling and did not got to use two iterators to reassign post removal... I just started it with initial i.e. beginning and that is fine for my requirement.. Below is that code without much error handling like when I am assigning the begin to itra and doing --, I should check for size and if it is >0 then only should do that -- If this is handled and the requirement is that once elelement is removed , it is ok to start from first elelement again.. with this, below code is ok to have removal of more than one element as well in case of non unique elements in list? https://code.sololearn.com/cVxkQ8v8pu2n/?ref=app
17th Feb 2022, 4:16 AM
Ketan Lalcheta
Ketan Lalcheta - avatar