Whose address shall the node of a linked list store: other node, or data structure having a node as a field?

独自空忆成欢 提交于 2019-12-06 16:41:17

list_head stores pointer to next/prev list nodes. One data structure can be placed in more then one list using more then one list_head elements in it. Processing code "knows" which of these elements placed into given list and can restore pointer to data structure from pointer to node using list_entry.

Why do the pointers in a list_head field store the addresses of other list_head fields rather than the addresses of the whole data structures in which the list_head structure is included?

Because it is logically right and easier to implement. You don't need to know the type to iterate the list. Also list head is not embedded into any structure. What should it point to when list is empty?

Given a pointer to a list_head object, how can I get the object of a data structure (such as "data structure 1") which contains the list_head object?

Using container_of macro.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!