3 ms·
> As a side note: I don't like the "elegant" version's use of double indirection. It's not necessary if you return a pointer instead of void, which is also nice
by xpe 6y ago
> As a side note: I don't like the "elegant" version's use of double indirection. It's not necessary if you return a pointer instead of void, which is also nice because you can treat calls to your list functions as lists themselves and chain them together.
Agreed.
> It also allows you to avoid warts like `p = &(p)->next;`
Does it? Isn't having `p = &(p)->next;` in the while loop necessary?
> at the modest cost of needing `p=remove(p,t)` instead of `remove(p,t)`.
I may disagree since an API is used many more times than the API function is written.
- ajarmst 6y agoThe & isn't needed if you're returning a pointer and no longer need a pointer to pointer to return the list as an argument. Also, if you get rid of the struct containing a single pointer and just use pointers, you can have the following instead… ` p=p->next As for the API, returning the pointer allows a function to be an argument for another function, which I find more flexible. ` p= remove(p,find(p,value))// I also like the option to be able to do stuff like: ` p=remove(remove(p,t),t); //remove two or: ` if (!remove(p,t)) {//that was my last node } But that may be an artifact of my preference for functional programming.
- xpe 6y agoNod, nod, nod. Thanks for clarifying :)