注意:来自Linux内核链表头文件!
头文件代码list.h(代码就不分析了,网上很多。)
#ifndef _LIST_H
#define _LIST_H
#include <string.h>
/*
* * This is a simple doubly linked list implementation that matches the
* * way the Linux kernel doubly linked list implementation works.
* */
struct list_head {
struct list_head *next; /* next in chain */
struct list_head *prev; /* previous in chain */
};
/* Initialise a list head to an empty list */
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
#define prefetch(x) __builtin_prefetch(x)
/*
* * 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_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);
}
/**
* * * 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);
}
/* Take an element out of its current list, with or without
* * reinitialising the links.of the entry*/
static inline void list_del(struct list_head *entry)
{
struct list_head *list_next = entry->next;
struct list_head *list_prev = entry->prev;
list_next->prev = list_prev;
list_prev->next = list_next;
}
/**
* * 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)
/**
* * 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 offsetof(type,member) ((size_t)&((type *)0)->member)
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member)*__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); })
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)
/**
* * list_for_each_safe - iterate over a list safe against removal of list entry
* * @pos:the &struct list_head to use as a loop cursor.
* * @n:another &struct list_head to use as temporary storage
* * @head:</span>the head for your list.
* */
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
实例main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "list.h"
typedef struct device
{
int ID;
char deviceId[32];
char meteId[32];
int value;
struct list_head list;
}Device;
Device* init_list()
{
Device *head=(Device *)malloc(sizeof(Device));
if(head==NULL)
{
perror("malloc head");
exit(1);
}
INIT_LIST_HEAD(&(head->list));
return head;
}
Device *new_node(int ID,char *deviceId,char *meteId,int value)
{
Device* new=(Device *)malloc(sizeof(Device));
if(new==NULL)
{
perror("new node");
exit(1);
}
new->ID=ID;
strcpy(new->deviceId,deviceId);
strcpy(new->meteId,meteId);
new->value=value;
return new;
}
int delete_node(Device *head,char *deviceId)
{
Device *p=NULL;
list_for_each_entry(p,&(head->list),list)
{
if(!strcmp(p->deviceId,deviceId))
{
list_del(&(p->list));
}
}
return 0;
}
Device *find_node(Device *head,char *deviceId)
{
Device *p=NULL;
list_for_each_entry(p,&(head->list),list)
{
if(!strcmp(p->deviceId,deviceId))
{
return p;
}
}
return NULL;
}
int show_list(Device *head)
{
Device *p=NULL;
list_for_each_entry(p,&(head->list),list)
{
printf("ID:%d,deviceId:%s,meteId:%s,value:%d\n",p->ID,p->deviceId,p->meteId,p->value);
}
return 0;
}
int main(int agc,char *argv[])
{
Device *mylist=init_list();
Device *new=new_node(1,"00123456","1123456",0);
list_add_tail(&(new->list),&(mylist->list));
show_list(mylist);
return 0;
}
记录一个错误!!!!!
error: expected declaration specifiers or '...' before '(' token
const typeof(((type *) NULL)->member) *__mptr = (ptr); \
note: in expansion of macro 'container_of'
#define list_entry(ptr, type, field) container_of(ptr, type, field)
error: '__mptr' undeclared (first use in this function)
(type *) ((char *) __mptr - offsetof(type, member)); \
^
note: each undeclared identifier is reported only once for each function it appears in
(type *) ((char *) __mptr - offsetof(type, member)); \
这个错误找了很久,后来发现原项目指定了-std=c99编译,用c99编译会出现这种鸟问题,原因不知。。。。
为了方便以后项目使用到链表,特此记录一个实例。