// Transplant the linked list of Linux kernel (3.0.13) to Windows, and VS is compiled. // To be fully tested // to be added hlist
# Include <stdio. h> # include <string. h> # include <stdlib. h>/* transplant Linux implement */# define container_of (PTR, type, member) (type *) (\ (pchar) (Address)-\ (ulong_ptr) (& (type *) 0)-> Field)/* Linux kernel List Implementation */typedef int le; typedef struct list_head LH; typedef struct hlist_node HN; struct list_head {LH * Next, * Prev;}; struct hlist_head {HN * First;}; struct hlist_node {HN * Prev, ** PP Rev ;};# define list_head_init (name) {& (name), & (name) }# define list_head (name) LH name = list_head_init (name) static inline void init_list_head (LH * List) {list-> next = List; List-> Prev = List;} static inline void _ list_add (LH * n, LH * Prev, LH * Next) {next-> Prev = N; Prev-> next = N; n-> next = next; n-> Prev = Prev ;} static inline void list_add (LH * n, LH * head) {__ list_add (n, head, head-> next);} static inline Vo Id list_add_tail (LH * n, LH * head) {__ list_add (n, head-> Prev, head);} static inline void _ list_del (LH * Prev, LH * Next) {next-> Prev = Prev; Prev-> next = next;} static inline void _ list_del_entry (LH * entry) {__ list_del (Entry-> Prev, entry-> next);} static inline void list_del (LH * entry) {__ list_del_entry (entry);} static inline void list_replace (LH * n, LH * old) {n-> next = old-> next; n-> Prev = old-> Prev; n-> next-> Prev = N; n-> Prev-> next = N;} static inline void list_replace_init (LH * n, LH * Old) {list_replace (n, old ); init_list_head (old);} static inline void list_del_init (LH * entry) {__ list_del_entry (entry); init_list_head (entry);} static inline void list_move (LH * List, LH * head) {__ list_del_entry (list); list_add (list, head);} static inline void list_move_tail (LH * List, LH * head) {__ list_del_entry (list ); list_add_tail (L IST, head);} static inline int list_is_last (LH * List, LH * head) {return list-> next = head;} static inline int list_empty (const LH * head) {return head-> next = head;} static inline int list_empty_careful (LH * head) {LH * Next = head-> next; Return (next = head) & (next = head-> PREV);}/* head, head-> next interchange position */static inline void list_rotate_left (LH * head) // {LH * First; If (! List_empty (head) {First = head-> next; list_move_tail (first, head) ;}/ * judge that the list has only the header */static inline int list_is_singular (LH * head) {return! List_empty (head) & (Head-> next = head-> PREV);} static inline void _ list_cut_position (LH * List, LH * head, LH * entry) {LH * new_first = entry-> next; List-> next = head-> next; List-> next-> Prev = List; List-> Prev = entry; entry-> next = List; head-> next = new_first; new_first-> Prev = head ;} /* cut the nodes from head-> next to entry into list */static inline void list_cut_position (LH * List, LH * head, LH * entry) {If (list_empty (head)) Return; If (list_is_singular (head) & (Head-> next! = Entry & head! = Entry) return; If (Entry = head) init_list_head (list); else _ list_cut_position (list, Head, entry );} static inline void _ list_splice (const LH * List, LH * Prev, LH * Next) {LH * First = List-> next; LH * Last = List-> Prev; first-> Prev = Prev; Prev-> next = first; last-> next = next; next-> Prev = last ;} /* after the list is connected to the head (designed for stacks), merge the list */static inline void list_splice (const LH * List, LH * head) {If (! List_empty (list) _ list_splice (list, head, head-> next);} static inline void list_splice_tail (LH * List, LH * head) {If (! List_empty (list) _ list_splice (list, head-> Prev, head);} static inline void list_splice_init (LH * List, LH * head) {If (! List_empty (list) {__ list_splice (list, head, head-> next); init_list_head (list) ;}} static inline void list_splice_tail_init (LH * List, LH * head) {If (! List_empty (list) {__ list_splice (list, head-> Prev, head); init_list_head (list) ;}# define list_entry (PTR, type, member) \ container_of (PTR, type, member) # define list_first_entry (PTR, type, member) \ list_entry (PTR)-> next, type, member) # define list_for_each (Pos, head) \ for (Pos = (head)-> next; pos! = (Head); Pos = pos-> next) # DEFINE _ list_for_each (Pos, head) \ for (Pos = (head)-> next; pos! = (Head); Pos = pos-> next) # define list_for_each_prev (Pos, head) \ for (Pos = (head)-> Prev; pos! = (Head); Pos = pos-> PREV) # define list_for_each_safe (Pos, N, head) \ for (Pos = (head)-> next, N = pos-> next; pos! = (Head); \ pos = n, n = pos-> next) # define list_for_each_prev_safe (Pos, N, head) \ for (Pos = (head)-> Prev, N = pos-> Prev; \ pos! = (Head); \ pos = n, n = pos-> PREV) # define list_for_each_entry (Pos, Head, member) \ for (Pos = list_entry (head) -> next, typeof (* POS), member); \ & Pos-> member! = (Head); \ pos = list_entry (POS-> member. next, typeof (* POS), member) # define list_for_each_entry_reverse (Pos, Head, member) \ for (Pos = list_entry (head)-> Prev, typeof (* POS), member); \ & Pos-> member! = (Head); \ pos = list_entry (POS-> member. prev, typeof (* POS), member) # define list_prepare_entry (Pos, Head, member) \ (POS )?: List_entry (Head, typeof (* POS), member)/* traverse list from pos-> next rather than head-> next */# define list_for_each_entry_continue (Pos, Head, member) \ for (Pos = list_entry (POS-> member. next, typeof (* POS), member); \ & Pos-> member! = (Head); \ pos = list_entry (POS-> member. next, typeof (* POS), member) # define list_for_each_entry_continue_reverse (Pos, Head, member) \ for (Pos = list_entry (POS-> member. prev, typeof (* POS), member); \ & Pos-> member! = (Head); \ pos = list_entry (POS-> member. prev, typeof (* POS), member)/* traverse list from POS */# define list_for_each_entry_from (Pos, Head, member) \ for (; & Pos-> member! = (Head); \ pos = list_entry (POS-> member. next, typeof (* POS), member) # define list_for_each_entry_safe (Pos, N, Head, member) \ for (Pos = list_entry (head)-> next, typeof (* POS), member), \ n = list_entry (POS-> member. next, typeof (* POS), member); \ & Pos-> member! = (Head); \ pos = n, n = list_entry (n-> member. next, typeof (* n), member) # define list_for_each_entry_safe_continue (Pos, N, Head, member) \ for (Pos = list_entry (POS-> member. next, typeof (* POS), member), \ n = list_entry (POS-> member. next, typeof (* POS), member); \ & Pos-> member! = (Head); \ pos = n, n = list_entry (n-> member. next, typeof (* n), member) # define list_for_each_entry_safe_from (Pos, N, Head, member) \ for (n = list_entry (POS-> member. next, typeof (* POS), member); \ & Pos-> member! = (Head); \ pos = n, n = list_entry (n-> member. next, typeof (* n), member) # define list_for_each_entry_safe_reverse (Pos, N, Head, member) \ for (Pos = list_entry (head)-> Prev, typeof (* POS), member), \ n = list_entry (POS-> member. prev, typeof (* POS), member); \ & Pos-> member! = (Head); \ pos = n, n = list_entry (n-> member. prev, typeof (* n), member) # define list_safe_reset_next (Pos, N, member) \ n = list_entry (POS-> member. next, typeof (* POS), member)/* end Linux kernel List Implementation * // * User defination */typedef struct {LH list; Le data;} lnode; int main () {lnode h; init_list_head (& H. list); Return 0 ;}