玩轉C鏈表

作者: wwang  來源: 博客園  發布時間: 2010-12-05 15:23  閱讀: 3338 次  推薦: 3   原文鏈接   [收藏]  

  鏈表是C語言編程中常用的數據結構,比如我們要建一個整數鏈表,一般可能這么定義:

struct int_node {
        int val;
        struct int_node *next;
};

  為了實現鏈表的插入、刪除、遍歷等功能,另外要再實現一系列函數,比如:

void insert_node(struct int_node *head, struct int_node *current);

void delete_node(struct int_node *head, struct int_node *current);

void access_node(struct int_node *head)
{
        struct int_node *node;
        for (node = head; node != NULL; node = node->next) {
                // do something here
        }
}

  如果我們的代碼里只有這么一個數據結構的話,這樣做當然沒有問題,但是當代碼的規模足夠大,需要管理很多種鏈表,難道需要為每一種鏈表都要實現一套插入、刪除、遍歷等功能函數嗎?熟悉C++的同學可能會說,我們可以用標準模板庫啊,但是,我們這里談的是C,在C語言里有沒有比較好的方法呢?

  Mr.Dave在他的博客里介紹了自己的實現,這個實現是個很好的方案,各位不妨可以參考一下。在本文中,我們把目光投向當今開源界最大的C項目--Linux Kernel,看看Linux內核如何解決這個問題。

  Linux內核中一般使用雙向鏈表,聲明為struct list_head,這個結構體是在include/linux/types.h中定義的,鏈表的訪問是以宏或者內聯函數的形式在include/linux/list.h中定義。

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

  Linux內核為鏈表提供了一致的訪問接口。

void INIT_LIST_HEAD(struct list_head *list);
void list_add(struct list_head *new, struct list_head *head);
void list_add_tail(struct list_head *new, struct list_head *head);
void list_del(struct list_head *entry);
int list_empty(const struct list_head *head);

   以上只是從Linux內核里摘選的幾個常用接口,更多的定義請參考Linux內核源代碼。我們先通過一個簡單的實作來對Linux內核如何處理鏈表建立一個感性的認識。

#include <stdio.h>
#include "list.h"

struct int_node {
        int val;
        struct list_head list;
};

int main()
{
        struct list_head head, *plist;
        struct int_node a, b;

        a.val = 2;
        b.val = 3;

        INIT_LIST_HEAD(&head);
        list_add(&a.list, &head);
        list_add(&b.list, &head);

        list_for_each(plist, &head) {
                struct int_node *node = list_entry(plist, struct int_node, list);
                printf("val = %d\n", node->val);
        }

        return 0;
}

  看完這個實作,是不是覺得在C代碼里管理一個鏈表也很簡單呢?代碼中包含的頭文件list.h是我從Linux內核里抽取出來并做了一點修改的鏈表處理代碼,現附在這里給大家參考,使用的時候只要把這個頭文件包含到自己的工程里即可。

 
#ifndef __C_LIST_H
#define __C_LIST_H

typedef unsigned char u8;
typedef unsigned
short u16;
typedef unsigned
int u32;
typedef unsigned
long size_t;

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

/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*

*/
#define container_of(ptr, type, member) (type *)((char *)ptr -offsetof(type,member))

/*
* 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;
};


/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.

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


#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)

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


/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.

*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)

/**
* list_for_each_r - iterate over a list reversely
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.

*/
#define list_for_each_r(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)

/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!

*/
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_empty - tests whether a list is empty
* @head: the list to test.

*/
static inline int list_empty(const struct list_head *head)
{

return head->next == head;
}



static inline void __list_splice(struct list_head *list,
struct list_head *head)
{

struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;

first
->prev = head;
head
->next = first;

last
->next = at;
at
->prev = last;
}


/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.

*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{

if (!list_empty(list))
__list_splice(list, head);
}



#endif // __C_LIST_H

  list_head通常是嵌在數據結構內使用,在上文的實作中我們還是以整數鏈表為例,int_node的定義如下:

struct int_node {
        int val;
        struct list_head list;
};

  使用list_head組織的鏈表的結構如下圖所示:

  遍歷鏈表是用宏list_for_each來完成。

#define list_for_each(pos, head) \
	for (pos = (head)->next; prefetch(pos->next), pos != (head); \
        	pos = pos->next)

  在這里,pos和head均是struct list_head。在遍歷的過程中如果需要訪問節點,可以用list_entry來取得這個節點的基址。

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

  我們來看看container_of是如何實現的。如下圖所示,我們已經知道TYPE結構中MEMBER的地址,如果要得到這個結構體的地址,只需要知道MEMBER在結構體中的偏移量就可以了。如何得到這個偏移量地址呢?這里用到C語言的一個小技巧,我們不妨把結構體投影到地址為0的地方,那么成員的絕對地址就是偏移量。得到偏移量之后,再根據ptr指針指向的地址,就可以很容易的計算出結構體的地址。

  list_entry就是通過上面的方法從ptr指針得到我們需要的type結構體。

  Linux內核代碼博大精深,陳莉君老師曾把它形容為“覆壓三百余里,隔離天日”(摘自《阿房宮賦》),可見其內容之豐富、結構之龐雜。內核里有著眾多重要的數據結構,具有相關性的數據結構之間很多都是用本文介紹的鏈表組織在一起,看來list_head結構雖小,作用可真不小。

  Linux內核是個偉大的工程,其源代碼里還有很多精妙之處,值得C/C++程序員認真去閱讀,即使我們不去做內核相關的工作,閱讀精彩的代碼對程序員自我修養的提高也是大有裨益的。

3
0
 
標簽:C 鏈表
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()