linux内核提取的链表操作函数

#include <stdio.h>
#include <stdlib.h>

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

struct node
{
	int value;
	char name[10];
	struct list_head list;
};

typedef struct node NODE;

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

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

#define list_entry(link, type, member) \
	((type *)((char *)(link)-(unsigned long)(&((type *)0)->member)))

#define list_head(list, type, member)       \
	list_entry((list)->next, type, member)

#define list_tail(list, type, member)       \
	list_entry((list)->prev, type, member)

#define list_next(elm, member)                  \
	list_entry((elm)->member.next, typeof(*elm), member)

#define list_for_each_entry(pos, list, member)          \
	for (pos = list_head(list, typeof(*pos), member);   \
			&pos->member != (list);                \
			pos = list_next(pos, member))

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

static inline void
list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}

static inline void
list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}

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

static inline void
list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
}

static inline int list_empty(const struct list_head *head)
{
	return head->next == head;
}

static inline void __list_del_entry(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
}

static inline void list_move_tail(struct list_head *list,
		struct list_head *head)
{
	__list_del_entry(list);
	list_add_tail(list, head);
}

static inline void list_rotate_left(struct list_head *head)
{
	struct list_head *first;

	if (!list_empty(head)) {
		first = head->next;
		list_move_tail(first, head);
	}
}

static inline struct list_head *
list_reverse(struct list_head *head)
{
	struct list_head *prev = NULL;
	struct list_head *cur = NULL;
	struct list_head *next = head;

	for (; next != NULL; )
	{
		cur = next;
		next = cur->next;
		cur->next = prev;
		prev = cur;
	}

	return prev;
}

int
main(int argc, char *argv[])
{
	NODE p[30], *cur;
	int i, j;

	LIST_HEAD(head);

	for (i = 0, j = 0; i < 30; i ++, j += 9) {
		p[i].value = j;
		list_add_tail(&p[i].list, &head);
	}

	i = 0;
	list_for_each_entry(cur, &head, list) {
		printf("%d(%d)\n", cur->value, i++);
	}
	list_reverse(&head);
	printf("===========================\n");
	list_for_each_entry(cur, &head, list) {
		printf("%d\n", cur->value);
	}

	printf("empty %d\n", list_empty(&head));

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值