3 ms·
The root of the problem is that "the thing that points to an entity" is not a consistent concept in this kind list. Sometimes it's "head", sometimes it's an ent
by solipsism 10y ago
The root of the problem is that "the thing that points to an entity" is not a consistent concept in this kind list. Sometimes it's "head", sometimes it's an entity's ->next. I wonder what Linus would think of implementing the linked list with a dummy head node, having no value and pointing to the first entity. Personally, I think that would be tasteful. It allows for simple loops like the "good taste" one, but you don't have to think so hard about how to implement a simple operation like remove. You don't need the added indirection.
- dpark 10y agoThis is exactly what Linus's code does. "indirect" is just "dummy->next" but doesn't pay the extra space cost for the node value. If writing this code in a higher level language, I think the dummy node would be more appropriate and idiomatic despite the slight increase in space.
- majewsky 10y agoIt's one more pointer that you have to resolve, everytime you do something with the list. Sounds smelly to me.
- dpark 10y agoDepends on where you store it. If you're just passing around node pointers, then you could create the dummy node on the stack only when you need it and it's the same indirection cost as the "indirect" pointer. If you've got a "list" struct that you're passing around (pretty unlikely in C), you could embed the dummy node directly.