首页>
知识库>
详情

游戏程序设计:C语言中实现通用双链表

2020-07-21 来源:CloudBest 阅读量: 0
关键词:

    include
    #include
    struct list_head {
    struct list_head *next, *prev;
    };
    #define LIST_HEAD_INIT(name) { &(name), &(name) }
    #define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)
    #define INIT_LIST_HEAD(ptr) do { \
    (ptr)->next = (ptr); (ptr)->prev = (ptr); \
    } while (0)
    static void __list_add(struct list_head * new, struct list_head * prev, struct list_head * next);
    static void list_add(struct list_head *new, struct list_head *head);
    static void list_add_tail(struct list_head *new, struct list_head *head);
    static void __list_del(struct list_head * prev, struct list_head * next);
    static void list_del(struct list_head *entry);
    static void list_del_init(struct list_head *entry);
    static int list_empty(struct list_head *head);
    static void list_splice(struct list_head *list, struct list_head *head);
    #define list_entry(ptr, type, member) \
    ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))
    #define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)
    #define NEW_LIST_NODE(type, node) \
    {\
    node = (struct type *)malloc( sizeof(struct type)); \
    if (node == NULL) exit(-1);\
    }
    #define FREE_LIST(type, p, list_name)\
    {\
    struct type *posnode;\
    while(!list_empty(&(p)->list_name)) {\
    posnode = list_entry((&(p)->list_name)->next, type, list_name);\
    list_del((&(p)->list_name)->next);\
    free(posnode);\
    }\
    }
    static void __list_add(struct list_head * new, struct list_head * prev, struct list_head * next)
    {
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
    }
    static void list_add(struct list_head *new, struct list_head *head)
    {
    __list_add(new, head, head->next);
    }
    static void list_add_tail(struct list_head *new, struct list_head *head)
    {
    __list_add(new, head->prev, head);
    }
    static void __list_del(struct list_head * prev, struct list_head * next)
    {
    next->prev = prev;
    prev->next = next;
    }
    static void list_del(struct list_head *entry)
    {
    __list_del(entry->prev, entry->next);
    }
    static void list_del_init(struct list_head *entry)
    {
    __list_del(entry->prev, entry->next);
    INIT_LIST_HEAD(entry);
    }
    static int list_empty(struct list_head *head)
    {
    return head->next == head;
    }
    static void list_splice(struct list_head *list, struct list_head *head)
    {
    struct list_head *first = list->next;
    if (first != list) {
    struct list_head *last = list->prev;
    struct list_head *at = head->next;
    first->prev = head;
    head->next = first;
    last->next = at;
    at->prev = last;
    }
    }
    typedef struct int_list
    {
    struct list_head list;
    int data;
    } int_list, *pint_list;
    void test_int_list()
    {
    struct int_list *dlink, *newnode, *posnode;
    struct list_head *pos;
    int i;
    NEW_LIST_NODE(int_list, dlink);