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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值