- 论坛徽章:
- 0
|
#ifndef _NB_LIST_H
#define _NB_LIST_H
#define offsetof(TYPE,MEMBER) (int)(((char *)&(((TYPE*)0)->MEMBER)) -((char *)((TYPE *)0)))
#define containof(TYPE,MEMBER,PTR) ({\
const typeof(((TYPE *)0)->MEMBER) *_mPTR = (PTR); \
(TYPE *)( (char *)_mPTR -offsetof(TYPE,MEMBER) ); \
})
typedef struct _node {
struct _node *next;
struct _node *prev;
}node_t;
typedef struct _list {
node_t list;
int ListNum;
}list_t;
int init_list(list_t *head);
int push_node(list_t *head,node_t *node);
node_t * pop_node(list_t *head);
int add_node_head(list_t *head,node_t *node);
int del_node_head(list_t *head);
int add_node_tail(list_t *head,node_t *node);
int del_node_tail(list_t *head);
#endif /*end of list.h*/
#include "list.h"
int init_list(list_t *head)
{
if (NULL == head)
return -1;
head->list.next =&head->list;
head->list.prev= &head->list;
head->ListNum =0;
return 0;
}
//www.cnxhacker.com
int push_node(list_t *head,node_t *node)
{
if (NULL == head || NULL == node)
return -1;
head->list.prev->next= node;
node->prev = head->list.prev;
node->next = &head->list;
head->list.prev = node;
head->ListNum++;
return 0;
}
node_t * pop_node(list_t *head)
{
node_t * RetNode=NULL;
if (NULL == head)
return NULL;
RetNode = head->list.next;
RetNode->next->prev = &head->list;
head->list.next = RetNode->next;
head->ListNum--;
return RetNode;
}
int add_node_head(list_t *head,node_t *node)
{
if (NULL == head || NULL == node)
return -1;
head->list.next->prev = node;
node->next = head->list.next;
node->prev= &head->list;
head->list.next = node;
head->ListNum++;
return 0;
}
int del_node_head(list_t *head)
{
node_t * TempNode=NULL;
if (NULL == head)
return -1;
TempNode = head->list.next;
TempNode->next->prev = &head->list;
head->list.next = TempNode->next;
head->ListNum--;
return 0;
}
int add_node_tail(list_t *head,node_t *node)
{
if (NULL == head || NULL == node)
return -1;
node->prev = head->list.prev;
node->next = &head->list;
head->list.prev->next = node;
head->list.prev = node;
head->ListNum++;
return 0;
}
int del_node_tail(list_t *head)
{
node_t *TempNode=NULL;
if (NULL == head)
return -1;
TempNode = head->list.prev;
TempNode->next->prev= &head->list;
head->list.next= TempNode->next;
head->ListNum--;
return 0;
}
int add_node_ByIndex(list_t *head,node_t *node,unsigned int Index)
{
node_t *InsertNode=NULL;
if (NULL == head || NULL == node)
return -1;
if (Index >= head->ListNum)
return add_node_tail(head,node);
InsertNode = head->list.next;
while(Index-1) {
InsertNode = InsertNode->next;
Index--;
}
node->next= InsertNode->next;
node->prev = InsertNode;
InsertNode->next->prev = node;
InsertNode->next = node;
head->ListNum++;
return 0;
}
node_t * Search_node_ByIndex(list_t *head,unsigned int Index)
{
node_t * TempNode=NULL;
if (NULL == head)
return NULL;
if (Index >= head->ListNum)
return head->list.prev;
TempNode= head->list.next;
while(Index) {
TempNode= TempNode->next;
Index--;
}
return TempNode;
}
int del_node_ByIndex(list_t *head,unsigned int Index)
{
node_t * TempNode=NULL;
if (NULL == head)
return -1;
if (Index < 0 || Index >head->ListNum)
return -1;
TempNode= head->list.next;
while(Index-1) {
TempNode= TempNode->next;
Index--;
}
TempNode->prev->next = TempNode->next;
TempNode->next->prev = TempNode->prev;
head->ListNum--;
return 0;
}
这个链表用起来很爽~参照linux内核中的链表 |
|