问一个关于数据结构的有关问题
问一个关于数据结构的问题
有什么方法可以实现链表与存储数据的完全解耦?现在需要用一个list,Linux内核的list又不让用,就想自己写一个,最好能用纯c实现,不太想用类和模版,除了linux内核的方法,还有其他容易实现和理解一点的方法吗?
------解决方案--------------------
想要 链表 跟 数据 解耦 , 又不想用模板 ,
那么这是一个 插入式的双链表
------解决方案--------------------
给你一个我用的。
我也是从其他代码修改过来的。
有什么方法可以实现链表与存储数据的完全解耦?现在需要用一个list,Linux内核的list又不让用,就想自己写一个,最好能用纯c实现,不太想用类和模版,除了linux内核的方法,还有其他容易实现和理解一点的方法吗?
------解决方案--------------------
想要 链表 跟 数据 解耦 , 又不想用模板 ,
那么这是一个 插入式的双链表
------解决方案--------------------
给你一个我用的。
我也是从其他代码修改过来的。
typedef struct List
{
struct List *_Next;
struct List *_Prev;
} List;
/* Define a list like so:
*
* struct gadget
* {
* List entry; <-- doesn't have to be the first item in the struct
* int a, b;
* };
*
* static List global_gadgets = LIST_INIT( global_gadgets );
*
* or
*
* struct some_global_thing
* {
* List gadgets;
* };
*
* Init( &some_global_thing->gadgets );
*
* Manipulate it like this:
*
* AddHead( &global_gadgets, &new_gadget->entry );
* Remove( &new_gadget->entry );
* AddAfter( &some_random_gadget->entry, &new_gadget->entry );
*
* And to iterate over it:
*
* struct gadget *gadget;
* LIST_FOR_EACH_ENTRY( gadget, &global_gadgets, struct gadget, entry )
* {
* ...
* }
*
*/
/* add an element after the specified one */
__inline static void AddAfter(List *elem, List *to_add)
{
to_add->_Next = elem->_Next;
to_add->_Prev = elem;
elem->_Next->_Prev = to_add;
elem->_Next = to_add;
}
/* add an element before the specified one */
__inline static void AddBefore(List *elem, List *to_add)
{
to_add->_Next = elem;
to_add->_Prev = elem->_Prev;
elem->_Prev->_Next = to_add;
elem->_Prev = to_add;
}
/* add element at the head of the list */
__inline static void AddHead(List *list, List *elem)
{
AddAfter(list, elem);
}
/* add element at the tail of the list */
__inline static void AddTail(List *list, List *elem)
{
AddBefore(list, elem);
}
/* remove an element from its list */
__inline static void Remove(List *elem)
{
elem->_Next->_Prev = elem->_Prev;
elem->_Prev->_Next = elem->_Next;
}
/* get the _Next element */
__inline static List *Next(List const *list, List const *elem)
{
List *ret = elem->_Next;
if (elem->_Next == list) ret = NULL;
return ret;
}
/* get the previous element */
__inline static List *Prev(List const *list, List const *elem)
{
List *ret = elem->_Prev;
if (elem->_Prev == list) ret = NULL;
return ret;
}
/* get the first element */
__inline static List *Head(List const *list)
{
return Next(list, list);
}
/* get the last element */
__inline static List *Tail(List const *list)
{
return Prev(list, list);
}
/* check if a list is empty */
__inline static int Empty(List const *list)
{
return list->_Next == list;
}
/* initialize a list */
__inline static void Init(List *list)
{
list->_Next = list->_Prev = list;
}
/* count the elements of a list */
__inline static unsigned int Count(List const *list)
{
unsigned count = 0;
List const *ptr;
for (ptr = list->_Next; ptr != list; ptr = ptr->_Next)
{
count++;
}
return count;
}
/* move all elements from src to the tail of dst */