现在的位置: 首页 > 综合 > 正文

A way to implement a general purpose LinkedList in C

2019年03月11日 ⁄ 综合 ⁄ 共 2507字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.