c语言 链表 库,玩转C链表
鏈表是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
#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_Htypedef unsignedcharu8;
typedef unsignedshortu16;
typedef unsignedintu32;
typedef unsignedlongsize_t;#defineoffsetof(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.
**/#definecontainer_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.*/#defineLIST_POISON1 ((void *) 0x00100100)#defineLIST_POISON2 ((void *) 0x00200200)structlist_head {structlist_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.*/#definelist_entry(ptr, type, member) \container_of(ptr, type, member)#defineLIST_HEAD_INIT(name) { &(name), &(name) }#defineLIST_HEAD(name) \structlist_head name=LIST_HEAD_INIT(name)staticinlinevoidINIT_LIST_HEAD(structlist_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.*/#definelist_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.*/#definelist_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!*/staticinlinevoid__list_add(structlist_head*new,structlist_head*prev,structlist_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.*/staticinlinevoidlist_add(structlist_head*new,structlist_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.*/staticinlinevoidlist_add_tail(structlist_head*new,structlist_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!*/staticinlinevoid__list_del(structlist_head*prev,structlist_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.*/staticinlinevoidlist_del(structlist_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.*/staticinlineintlist_empty(conststructlist_head*head)
{returnhead->next==head;
}staticinlinevoid__list_splice(structlist_head*list,structlist_head*head)
{structlist_head*first=list->next;structlist_head*last=list->prev;structlist_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.*/staticinlinevoidlist_splice(structlist_head*list,structlist_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++程序員認真去閱讀,即使我們不去做內核相關的工作,閱讀精彩的代碼對程序員自我修養的提高也是大有裨益的。
總結
以上是生活随笔為你收集整理的c语言 链表 库,玩转C链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两个整数百分百C语言,2011年9月份计
- 下一篇: 太原理工软件学院c语言2020,太原理工