經典數據結構:list

linux內核中有不少經典的數據結構, 好比list, hlist, rbtree等等,這些數據結構都是由c語言編寫的,與平臺無關,徹底能夠從內核中拿出來放到本身的代碼中。linux

list
=======git

移植自linux內核, windows平臺可用github

因爲windows沒有typeof, 故WIN32下:windows

#define list_for_each_entry(pos, head, member, type)          \
 for (pos = list_entry((head)->next, type, member); \
      &pos->member != (head);  \
      pos = list_entry(pos->member.next, type, member))

其他涉及到使用typeof關鍵字的函數或者宏並未移植安全


函數
=======數據結構

1. list_add 在 head 以後追加一個節點
2. list_add_tail 在 head 以前追加一個節點, 也就是在末尾追加一個節點
3. list_del 刪除一個節點, 並將這個節點的next, prev 置爲 NULL
4. list_del_init 刪除一個節點並初始化刪除的節點
5. list_replace 替換一個節點
6. list_replace_init 替換一個節點, 並初始化被替換的節點
7. list_move 移動節點到 head 以後
8. list_move_tail 移動節點到 head 以前
9. list_is_last 判斷節點是不是鏈表中最後一個
10. list_empty 判斷鏈表是否爲空 (即, 是否只有 head 節點)
11. list_is_singular 判斷鏈表中是否只有一個節點 (除了 head 以外)
12. list_cut_position 將1個鏈表截斷爲2個鏈表
13. list_splice 將2個鏈表合併爲1個鏈表, @list中的全部節點(不包括list)加入到 head 以後
14. list_splice_tail 將2個鏈表合併爲1個鏈表, @list中的全部節點(不包括list)加入到 head 以前
15. list_splice_init 同 list_splice, 最後會初始化 @list
16. list_splice_tail_init 同 list_splice_tail, 最後會初始化 @list
 函數


=======spa

1. list_entry 獲取包含此節點的 struct
2. list_first_entry 獲取包含此節點的 首個 struct
3. list_for_each 從 head節點以後一個節點開始向後循環
4. list_for_each_prev 從 head節點以前一個節點開始向前循環
5. list_for_each_safe list_for_each 的安全版本, 即, 循環時即便有其它線程刪除節點也可正常運行
6. list_for_each_prev_safe list_for_each_prev 的安全版本
7. list_for_each_entry 同 list_for_each, 只是參數不一樣
8. list_for_each_entry_reverse 同 list_for_each_prev, 只是參數不一樣
9. list_for_each_entry_continue 同 list_for_each_entry, 但不是從頭(head)開始循環的
10. list_for_each_entry_continue_reverse 同 list_for_each_entry_reverse, 但不是從頭(head)開始循環的
11. list_for_each_entry_from 從指定位置開始向後循環
12. list_for_each_entry_safe list_for_each_entry 的安全版本
13. list_for_each_entry_safe_continue list_for_each_entry_continue 的安全版本
14. list_for_each_entry_safe_from list_for_each_entry_from 的安全版本
15. list_for_each_entry_safe_reverse list_for_each線程

 

代碼下載: https://github.com/xieweihua/Data-structure-and-algorithm/tree/master/listcode

相關文章
相關標籤/搜索