In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the use of tbox linked list list and list_entry". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Various list operations are available in TBOX:
List: a two-way linked list of elements maintained internally
List_entry: a bi-directional linked list of elements maintained externally
Single_list: an one-way linked list of elements maintained internally
Single_list_entry: an one-way linked list of elements maintained externally
Because the interface of double-chain and single-chain is similar, this paper mainly explains the specific use of double-chain.
So what is internal maintenance and external maintenance? To put it simply:
External maintenance: the linked list container itself does not store elements, does not open up memory space, but is only a node header, which is more memory-saving and more flexible. (especially when elements are migrated between multiple linked lists, or when multiple linked lists require unified memory pool maintenance).
Internal maintenance: the linked list container itself goes back to open up a space to store the element content separately. The operation of the interface in this way is relatively simple, but the flexibility and performance are not as good as the previous one. If you do not need multiple linked lists to maintain the same element, then it is more appropriate to use this mode for simple operation. (and the storage of internal elements is also optimized with memory pools).
The use of list
List is easy to use and the interface is easy to use. Here's a simple example:
/ / create a double chain of long type. Parameter 0 means the default automatic element growth size is adopted, or you can manually set the more appropriate size tb_list_ref_t list = tb_list_init (0, tb_element_long ()). If (list) {/ / insert element: 1 in the header of the linked list and return the iterator index tb_size_t itor = tb_list_insert_head (list, (tb_pointer_t) 1) of this new element; / / insert a new element after the previous new element: 2 tb_list_insert_next (list, itor, (tb_pointer_t) 2) / / insert elements at the end of the linked list: 3 tb_list_insert_tail (list, (tb_pointer_t) 3); / / remove the specified element tb_list_remove (list, itor); / / traverse all linked list elements, tb_for_all (tb_long_t, item, list) {/ / print element value tb_trace_i ("% ld", item) } / / destroy list tb_list_exit (list);} use of list_entry
Because list_entry is an external container, it needs to operate on the external self-defined structure, such as defining:
/ / linked list element structure typedef struct _ _ tb_demo_entry_t {/ / external double-linked node, which is used to maintain the actual data of tb_list_entry_t entry; / / element tb_size_t data;} tb_demo_entry_t
The specific operations on the linked list are as follows:
/ / define some static elements For inserting linked lists (you may need to create them dynamically for actual use) tb_demo_entry_t entries [12] = {{0}, 0}, {{0}, 1}, {{0}, 2}, {{0}, 3}, {{0}, 4}, {{0}, 5}, {{0}, 6}, {{0}, 7} {{0}, 8}, {{0}, 9}, {{0}, 10}, {{0}, 11}} / / initialize the linked list, you need to specify the structure type of the external element, and the node name of the linked list is tb_list_entry_head_t list;tb_list_entry_init (& list, tb_demo_entry_t, entry, tb_null); / / insert some elements. Note: all operations operate tb_list_entry_insert_tail (& list, & entries [5] .entry) on the list_entry node in the external structure. Tb_list_entry_insert_tail (& list, & entries [6] .entry); tb_list_entry_insert_tail (& list, & entries [7] .entry); tb_list_entry_insert_tail (& list, & entries [8] .entry); tb_list_entry_insert_tail (& list, & entries [9] .entry); tb_list_entry_insert_head (& list, & entries [4] .entry) Tb_list_entry_insert_head (& list, & entries [3] .entry); tb_list_entry_insert_head (& list, & entries [2] .entry); tb_list_entry_insert_head (& list, & entries [1] .entry); tb_list_entry_insert_head (& list, & entries [0] .entry) / / access the element data of a specific node tb_demo_entry_t* entry = (tb_demo_entry_t*) tb_list_entry (& list, & entries [5] .entry); tb_trace_i ("entry:% lu", entry- > data); / / traverse all elements tb_trace_i ("insert:% lu", tb_list_entry_size (& list)) Tb_for_all_if (tb_demo_entry_t*, item0, tb_list_entry_itor (& list), item0) {tb_trace_i ("% lu", item0- > data);} / / replace the head and tail element tb_list_entry_replace_head (& list, & entries [10] .entry); tb_list_entry_replace_last (& list, & entries [11] .entry) / / remove the head and tail elements tb_list_entry_remove_head (& list); tb_list_entry_remove_last (& list); / / move the element position. Here, the head and tail elements are switched to tb_list_entry_ref_t head = tb_list_entry_head (& list); tb_list_entry_moveto_head (& list, tb_list_entry_last (& list)); tb_list_entry_moveto_tail (& list, head) / / exit list tb_list_entry_exit (& list)
Anyway, it's not very complicated, because the memory of the elements is maintained on the outside, so the flexibility is improved a lot, and multiple linked lists can be maintained at the same time, and then optimized by sharing a memory pool, the efficiency and memory can be greatly improved. This mode is very common in the linux kernel.
If you want to make a metaphor, list is a stupid operation, and list_entry is a customized operation.
This is the end of the introduction to "how to use tbox linked list list and list_entry". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.