A way to implement a general purpose LinkedList in C

本文介绍了一种使用Linux内核方法实现高效链表操作的技术,包括定义链表节点、链表操作宏简化及通过特殊宏将链表元素融入用户结构中,以减少宏使用带来的代码复杂度,提高代码可读性和维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C is not C++, so we cannot use template of C++ to achieve this.

There're more than one way to achieve this. For example, define ListItem with void* pointer.

struct ListItem{
struct ListItem *next;
struct ListItem *prior;
void* data;
}

In this way, we are thinking from data structure's perspective, not from user's perspective. 

Another example, define all operations of LinkedList using macro. 

#define LIST_HEADER(atype) atype *prior; atype *next
#define LIST_ADD(head,elem) ...
#define LIST_DELETE(head,elem) ...

But so many macros make the codes hard to read and maintain. 

In this article, I introduce a way. It is easy to use, and it uses less macros. The way is used in Linux kernel. 

First define ListItem. 

struct ListItem {
struct ListItem* next;
};

Second define operations of LinkedList based on ListItem. Take addItemToList as a example. 

Last define necessary macros. Take ADD_ITEM_TO_LIST and GET_LIST_ITEM_USER as examples. GET_LIST_ITEM_USER macro is the key. With this macro, we can put ListItem element in the user's structure anywhere, and we can get the user structure variable's address from ListItem element. "(int)(&((USER_STRUCT*)0)->listItem)" in the macro is for getting the relative offset of listItem in the user's structure. With this information, it is easy to reason out the macro. 

After these steps, we can use the general purpose LinkedList. 

ListUser and ListUser2 are examples of using it. We just need to put a ListItem element anywhere in the user's structure.


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


struct ListItem {
	struct ListItem* next;
};


ListItem* addItemToList(ListItem* head, ListItem* item)
{
	if(NULL==head){
		item->next=NULL;
		return item;
	}


	if(NULL==item){
		return head;
	}
	item->next=head;
	head=item;
	return head;
}


struct ListUser {
	int userId;
	char userName[100];
	struct ListItem listItem;
};


struct ListUser2 {
	char c;
	char c1;
	struct ListItem listItem;
};


#define ADD_ITEM_TO_LIST(listItemHead,userItem) addItemToList(listItemHead,&((userItem)->listItem))
#define GET_LIST_ITEM_USER(aListItemPtr,USER_STRUCT) (USER_STRUCT*)( (char *)(aListItemPtr) - (int)(&((USER_STRUCT*)0)->listItem))


int main(int argc, char* argv[])
{
	ListUser *puser;
	ListUser2 *puser2;
	ListItem *item,*phead;
	int i;


	phead=NULL;
	/* add element */
	for(i=0;i<10;i++){
		puser= (ListUser*)malloc(sizeof(struct ListUser));
		puser->userId=i;
		phead=ADD_ITEM_TO_LIST(phead,puser);
	}


	/* iterate element */
	item=phead;
	while(item!=NULL){
		puser=GET_LIST_ITEM_USER(item,struct ListUser);
		printf("%d\n",puser->userId);
		item=item->next;
	}


	phead=NULL;
	/* add element */
	for(i=0;i<10;i++){
		puser2= (ListUser2*)malloc(sizeof(struct ListUser2));
		puser2->c = 'a'+i;
		phead=ADD_ITEM_TO_LIST(phead, puser2);
	}


	/* iterate element */
	item=phead;
	while(item!=NULL){
		puser2=GET_LIST_ITEM_USER(item,struct ListUser2);
		printf("%c\n",puser2->c);
		item=item->next;
	}


	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值