一、普通链表弊端
普通链表概念简单,操作方便,但存在有致命的缺陷,即:每一条链表都是特殊的,不具有通用性。因为对每一种不同的数据,所构建出来的链表都是跟这些数据相关的,所有的操作函数也都是数据密切相关的,换一种数据节点,则所有的操作函数都需要一一重写编写,这种缺陷对于一个具有成千上万种数据节点的工程来说是灾难性的。
1.问题分析
比如下面的操作函数,函数只能操作指定的参数类型:
// 普通链表的插入函数,与数据节点node密切相关
// 换一种数据节点,该函数就无法使用了
void insert(node *head, node *new)
{
// ...
}
// 普通链表的删除函数,与数据节点node密切相关
// 换一种数据节点,该函数就无法使用了
node * remove(node *head)
{
// ...
}
形态各异的普通链表
在普通链表的节点设计中,不同的链表所使用的指针不同,就直接导致操作函数的参数不同,在C语言的环境下,无法统一这些所有的操作,这给编程开发带来了很大的麻烦,尤其在节点种类众多的场合。
2.原因分析
分析上述问题,其产生的根本原因是链表节点的设计,没有把数据和逻辑分开,也就是将具体的数据与组织这些数据的链表揉在一起,导致链表的操作不得已绑定了某个固定类型的数据。
节点中的数据和逻辑
3. 解决思路
既然是因为数据和链表指针混在一起导致了通用性问题,那么解决的思路就是将它们分开。将链表逻辑单独抽出来,去掉节点内的具体数据,让节点只包含双向指针。这样的节点连接起来形成一条单纯的链表如下所示:
接着,将这样的不含任何数据的链表,镶嵌在具体要用串起来的数据节点之中,这样一来,就可以将任何节点的链表操作完全统一了。
镶嵌了标准链表的用户节点
如上图所示,不管用户节点是什么类型的节点,也不管它里面包含什么数据,都跟链表本身没有关系。如下图所示,在 A 和 C 中插入 B 节点,和在 X 与 Z 中插入 Y 节点,完全可以用相同的函数来达到。此时,就已经成功地将数据与组织这些数据的逻辑分开了。这就是内核链表的基本思路。
二、内核链表实现功能
1.实现功能
2.main.c
#include <stdio.h>
#include <stdlib.h>
#include "kernel_list.h"
//定义大结构体
typedef struct node
{
int data; //存数据
struct list_head list; //存小结构体
} node; //node = struct node
//初始化结构体
node *initlist(void)
{
node *new = malloc(sizeof(node));
if (new == NULL)
{
printf("malloc fail\n");
return NULL;
}
new->data = 0;
INIT_LIST_HEAD(&new->list);
return new;
}
//显示数据
void display(node *head)
{
node *p = NULL; //大结构体
struct list_head *pos = NULL; //小结构体
list_for_each(pos, &head->list)
{
p = list_entry(pos, node, list);
printf("%d\t", p->data);
}
printf("\n");
}
//查找数据
node *find_node(node *head, int data)
{
node *p = NULL; //大结构体
struct list_head *pos = NULL; //小结构体
list_for_each(pos, &head->list)
{
p = list_entry(pos, node, list);
if (p->data == data)
{
return p;
}
}
return NULL;
}
//删除数据
void del_node(node *head, int data)
{
node *del = NULL;
del = find_node(head, data);
if (del == NULL)
{
printf("链表中无此数据\n");
}
else
{
list_del(&del->list);
free(del);
printf("删除完成\n");
}
}
//移动前插
void move_f(node *head, int move_data, int basic_data)
{
node *move = NULL, *basic = NULL;
move = find_node(head, move_data);
basic = find_node(head, basic_data);
if (move != NULL && basic != NULL)
{
list_move_tail(&move->list, &basic->list);
printf("移动完成\n");
}
}
//移动后插
void move_b(node *head, int move_data, int basic_data)
{
node *move = NULL, *basic = NULL;
move = find_node(head, move_data);
basic = find_node(head, basic_data);
if (move != NULL && basic != NULL)
{
list_move(&move->list, &basic->list);
printf("移动完成\n");
}
}
//销毁数据
void destroy(node *head)
{
node *p = NULL;
struct list_head *pos = NULL;
struct list_head *n = NULL;
list_for_each_safe(pos, n, &head->list)
{
list_del(pos);
p = list_entry(pos, node, list);
free(p);
}
INIT_LIST_HEAD(&head->list);
printf("销毁完成\n");
}
int main(void)
{
//定义局部变量
node *head = NULL, *new = NULL, *find = NULL;
char ch = '\0';
int data, move_data, basic_data;
//初始化大、小结构体
head = initlist();
while (1)
{
printf("====================请输入你要操作功能====================\n");
printf("=========================内核链表========================\n");
printf("\t\t\ta:头插插入\n");
printf("\t\t\tb:尾插插入\n");
printf("\t\t\tc:删除数据\n");
printf("\t\t\td:查询数据\n");
printf("\t\t\te:移动插前\n");
printf("\t\t\tf:摧毁插后\n");
printf("\t\t\tp:显示数据\n");
printf("\t\t\tt:摧毁数据\n");
printf("\t\t\tq:退出程序\n");
printf("=========================================================\n");
//等待用户输入
printf("请输入功能:");
ch = getchar();
switch (ch)
{
case 'a':
new = initlist();
printf("输入数据:");
scanf("%d", &new->data);
list_add(&new->list, &head->list);
break;
case 'b':
new = initlist();
printf("输入数据:");
scanf("%d", &new->data);
list_add_tail(&new->list, &head->list);
break;
case 'c':
printf("输入要删除的数据:");
scanf("%d", &data);
del_node(head, data);
break;
case 'd':
printf("输入要查找的数据:");
scanf("%d", &data);
find = find_node(head, data);
if (find != NULL)
{
printf("该数据地址:%p\n", find);
}
else
{
printf("链表中无此数据\n");
}
break;
case 'e':
printf("输入要移动的数据:");
scanf("%d", &move_data);
find = find_node(head, move_data);
if (find == NULL)
{
printf("链表中无此数据\n");
break;
}
else
{
printf("输入要移动到哪个数据之前:");
scanf("%d", &basic_data);
if (move_data == basic_data)
{
printf("输入数据相同,无需移动\n");
break;
}
find = find_node(head, basic_data);
if (find == NULL)
{
printf("链表中无此数据\n");
break;
}
else
{
move_f(head, move_data, basic_data);
}
}
break;
case 'f':
printf("输入要移动的数据:");
scanf("%d", &move_data);
find = find_node(head, move_data);
if (find == NULL)
{
printf("链表中无此数据\n");
break;
}
else
{
printf("输入要移动到哪个数据之后:");
scanf("%d", &basic_data);
if (move_data == basic_data)
{
printf("输入数据相同,无需移动\n");
break;
}
find = find_node(head, basic_data);
if (find == NULL)
{
printf("链表中无此数据\n");
break;
}
else
{
move_b(head, move_data, basic_data);
}
}
break;
case 'p':
display(head);
break;
case 't':
destroy(head);
break;
case 'q':
printf("退出成功!");
return 0;
break;
default:
printf("输入格式有误,请重新输入!\n");
break;
}
//清空缓冲区的'\n'
while (getchar() != '\n');
}
return 0;
}
3.kernel_list.h
#ifndef __DLIST_H
#define __DLIST_H
/* This file is from Linux Kernel (include/linux/list.h)
* and modified by simply removing hardware prefetching of list items.
* Here by copyright, credits attributed to wherever they belong.
* Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)
*/
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions (“__xxx”) are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
/**
* 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) );})
/*
* 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 *) 0x00200)
struct list_head
{
struct list_head *prev;
struct list_head *next;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
// 宏定义语法规定只能有一条语句
// 如果需要多条语句,那就必须将多条语句放入一个do{}while(0)中使之成为一条复合语句
/* #define INIT_LIST_HEAD(ptr) \
do { \
(ptr)->next = (ptr); \
(ptr)->prev = (ptr); \
} while (0) */
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
/*
* 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 = (void *) 0;
entry->prev = (void *) 0;
}
/**
* list_del_init – deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move – delete from one list and add as another’s head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail – delete from one list and add as another’s tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_empty – tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(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);
}
/**
* list_splice_init – join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
static inline int list_is_singular( struct list_head *head)
{
return !list_empty(head) && (head->next == head->prev);
}
static inline void __list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
struct list_head *new_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = new_first;
new_first->prev = head;
}
/**
* list_cut_position - cut a list into two
* @list: a new list to add all removed entries
* @head: a list with entries
* @entry: an entry within head, could be the head itself
* and if so we won't cut the list
*
* This helper moves the initial part of @head, up to and
* including @entry, from @head to @list. You should
* pass on @entry an element you know is on @head. @list
* should be an empty list or a list you do not care about
* losing its data.
*
*/
static inline void list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
if (list_empty(head))
return;
if (list_is_singular(head) &&
(head->next != entry && head != entry))
return;
if (entry == head)
INIT_LIST_HEAD(list);
else
__list_cut_position(list, head, entry);
}
/**
* list_entry – get the struct for this entry
* @ptr: the &struct list_head pointer. 移动的小结构体对应的地址
* @type: the type of the struct this is embedded in. 大结构体类型 (struct kernel_list)
* @member: the name of the list_struct within the struct. 小结构体在大结构体里面的成员名list
*///返回值为大结构体地址
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter. // p
* @head: the head for your list.
*/
//向后遍历
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
//向前遍历
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); \
pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop counter.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
//遍历时,保留pos后缀节点
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop counter. 大结构体指针
* @head: the head for your list. 小结构体指针
* @member: the name of the list_struct within the struct. 小结构体在大结构体里面的成员名list
*/
//向后直接遍历得到大结构体
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_safe – iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop counter.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
#endif