list连接件

list简单应用.  从linux内核源码中获取并修改得到. 留下备用

main.c

#include <stdio.h>
#include <stdlib.h>

#include "list.h"

enum {
    NAMEMAX = 16,
    STUNUM = 10
};

typedef struct {
    char name[NAMEMAX];
    int math;
    int chinese;
    struct list_head head;
} student_t;

int main(void)
{
    struct list_head stu_list;
    student_t    *stup;    
    int    i;

    INIT_LIST_HEAD(&stu_list);
    
    for(i = 0; i < STUNUM; ++i) {
        stup = (student_t *)malloc(sizeof(student_t));

        snprintf(stup->name, NAMEMAX, "student_%02d", i + 1);
        stup->math = random() % 101;
        //stup->math = i;
        stup->chinese = random() % 101;
        
        INIT_LIST_HEAD(&stup->head);
        list_add(&stup->head, &stu_list);
    }
    
    printf("-----------head->>>-tail------\n");
    list_for_each_entry(stup, &stu_list, head) {
        printf("name: %s\tmath: %3d\tchinese: %3d\n",stup->name, stup->math, stup->chinese);
    }
    printf("-----------tail->>>-head------\n");
    list_for_each_entry_reverse(stup, &stu_list, head) {
        printf("name: %s\tmath: %3d\tchinese: %3d\n",stup->name, stup->math, stup->chinese);
    }
    printf("-----------head->>>-tail-after-del-next----\n");
    //list_del(&stu_list);
    list_del(stu_list.next);
    list_for_each_entry(stup, &stu_list, head) {
        printf("name: %s\tmath: %3d\tchinese: %3d\n",stup->name, stup->math, stup->chinese);
    }
    printf("-----------head->>>-tail-after-del-prev----\n");
    //list_del(&stu_list);
    list_del(stu_list.prev);
    list_for_each_entry(stup, &stu_list, head) {
        printf("name: %s\tmath: %3d\tchinese: %3d\n",stup->name, stup->math, stup->chinese);
    }
    return 0;
}

list.h文件内容如下:

#ifndef _LIST_H_
#define _LIST_H_

/*
 * These are non-NULL pointers that will result in page faults
 * under normal circumstances, used to verify that nobody uses
 * non-initialized list entries.
 */
#define LIST_POISON1  ((void *) 0x00100100)
#define LIST_POISON2  ((void *) 0x00200200)


struct list_head {
    struct list_head *next, *prev;
};

#if 0
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
        struct list_head name = LIST_HEAD_INIT(name)
#endif

static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

static inline 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;
}

/**
 * list_add - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = LIST_POISON1;
    entry->prev = LIST_POISON2;
}

/**
 * list_replace - replace old entry by new one
 * @old : the element to be replaced
 * @new : the new element to insert
 *
 * If @old was empty, it will be overwritten.
 */
static inline void list_replace(struct list_head *old,
                struct list_head *new)
{
    new->next = old->next;
    new->next->prev = new;
    new->prev = old->prev;
    new->prev->next = new;
}

static inline void list_replace_init(struct list_head *old,
                    struct list_head *new)
{
    list_replace(old, new);
    INIT_LIST_HEAD(old);
}

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

#ifndef ARCH_HAS_PREFETCH
#define prefetch(x) __builtin_prefetch(x)
#endif 

#if 0
#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))
#endif
/**
 * list_for_each_entry    -    iterate over list of given type
 * @pos:    the type * to use as a loop cursor.
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 */
#define list_for_each_entry(pos, head, member)                \
    for (pos = list_entry((head)->next, typeof(*pos), member);    \
         prefetch(pos->member.next), &pos->member != (head);     \
         pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_each_entry_reverse - iterate backwards over list of given type.
 * @pos:    the type * to use as a loop cursor.
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 */
#define list_for_each_entry_reverse(pos, head, member)            \
    for (pos = list_entry((head)->prev, typeof(*pos), member);    \
         prefetch(pos->member.prev), &pos->member != (head);     \
         pos = list_entry(pos->member.prev, typeof(*pos), member))


#endif

 

编译链接运行, 输出如下

posted @ 2016-02-04 23:58  zhanglong71  阅读(257)  评论(0编辑  收藏  举报