woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

linux中的链表

//include/linux/list.h
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 inline void __list_add(struct list_head * newnode,
                                 
struct list_head * prev,
                                 
struct list_head * next)
{
    next->prev = newnode;
    newnode->next = next;
    newnode->prev = prev;
    prev->next = newnode;
}

//添加一个node
static inline void list_add(struct list_head *newnode, struct list_head *head)
{
    __list_add(newnode, head, head->next);
}


static inline void list_add_tail(struct list_head *newnode, struct list_head *head)
{
    __list_add(newnode, head->prev, head);
}

static inline int list_empty(struct list_head *head)
{
   
return head->next == head;
}

static inline void __list_del(struct list_head * prev,
                                 
struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}
//删除一个node
static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = entry->prev =
0;
}

#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(pos, head) \
   
for (pos = (head)->next; pos != (head); \
            pos = pos->next)

#define list_entry(ptr, type, member) \
    ((type *)((
char *)(ptr)-(unsigned long)(&((type *)0)->member)))

/*******************************
**
指针ptr指向结构体type中的成员member
**
通过指针ptr,返回结构体type的起始地址
            type

        |----------|
        |           |
        |           |
        |----------|
ptr-->| member --|
        |----------|
        |              |
        |              |
        |----------|
********************************/


//test_list.c

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

//#include
"ilist.h"


struct my_list{
   
struct list_head list;
   
char value[10];
};

int main(int argc, char **argv){
   
   
struct my_list *tmp;
   
struct list_head *pos, *q;
    unsigned
int i;
   
   
struct my_list mylist;
    INIT_LIST_HEAD(&mylist.list);
/*初始化链表头*/
   
   
/* mylist增加元素 */
   
for(i=3; i!=0; --i){
        tmp= (
struct my_list *)malloc(sizeof(struct my_list));
       
       
/* 或者INIT_LIST_HEAD(&tmp->list); */
        printf(
"enter value:");
        scanf(
"%s", tmp->value);
       
       
        list_add(&(tmp->list), &(mylist.list));
       
/* 也可以用list_add_tail() 在表尾增加元素*/
    }
    printf(
"\n");
   
    printf(
"traversing the list using list_for_each()\n");
    list_for_each(pos, &mylist.list){
       
   
/* 在这里 pos->next 指向next 节点, pos->prev指向前一个节点.这里的节点是
        struct my_list
类型. 但是,我们需要访问节点本身,而不是节点中的list字段,
       
list_entry()正是为此目的。*/
    
       
    tmp= list_entry(pos,
struct my_list, list);
    printf(
"%s ", tmp->value);
    }
    printf(
"\n");


    printf(
"deleting the list using list_for_each_safe()\n");
    list_for_each_safe(pos, q,&mylist.list){
        tmp= list_entry(pos,
struct my_list, list);
        printf(
"%s ", tmp->value);
        list_del(pos);
        free(tmp);
    }
}

 

posted on 2009-06-20 03:27 肥仔 阅读(229) 评论(0)  编辑 收藏 引用 所属分类: C++ 基础


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理