linux中的鏈表

來源:互聯網
上載者:User
//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);
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.