闲来无事,将自己曾经写的双向循环链表源码(Ring的实现)发于此处,供今后老了,给下一代看看,呵呵。
#ifndef HSW_RING_H
#define HSW_RING_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __cplusplus
typedef unsigned short bool ;
#endif
#ifndef NULL
#define NULL (void*)0
#endif
#ifndef tbool
typedef char tbool,*ptbool;
#endif
//
// Doubly linked ring structure. Can be used as either a ring head, or
// as link words.
//
typedef struct TAG_HSW_LIST_ENTRY
{
struct TAG_HSW_LIST_ENTRY *Flink;
struct TAG_HSW_LIST_ENTRY *Blink;
TAG_HSW_LIST_ENTRY()
{
Flink = Blink = NULL;
}
}HSW_RING_ENTRY, *PHSW_RING_ENTRY, HSW_RING_HEAD, *PHSW_RING_HEAD;
//
// Doubly-linked ring manipulation routines. Implemented as macros
// but logically these are procedures.
//
//
// VOID
// HswInitializeRingHead(
// PLIST_ENTRY ListHead
// );
//
#define HswInitializeRingHead(ListHead) (\
(ListHead)->Flink = (ListHead)->Blink = (ListHead))
//
// BOOLEAN
// HswIsRingEmpty(
// PLIST_ENTRY ListHead
// );
//
#define HswIsRingEmpty(ListHead) \
((ListHead)->Blink == (ListHead))
//
// PLIST_ENTRY
// RemoveHeadRing(
// PLIST_ENTRY ListHead
// );
//
#define RemoveHeadRing(ListHead) \
(ListHead)->Flink;\
{RemoveEntry((ListHead)->Flink)}
//
// PLIST_ENTRY
// RemoveTailRing(
// PLIST_ENTRY ListHead
// );
//
#define RemoveTailRing(ListHead) \
(ListHead)->Blink;\
{RemoveEntry((ListHead)->Blink)}
//
// VOID
// RemoveEntry(
// PLIST_ENTRY Entry
// );
//
#define RemoveEntry(Entry) {\
PHSW_RING_ENTRY _EX_Blink;\
PHSW_RING_ENTRY _EX_Flink;\
_EX_Flink = (Entry)->Flink;\
_EX_Blink = (Entry)->Blink;\
_EX_Blink->Flink = _EX_Flink;\
_EX_Flink->Blink = _EX_Blink;\
}
//
// VOID
// HswInsertTailRing(
// PLIST_ENTRY ListHead,
// PLIST_ENTRY Entry
// );
//
#define HswInsertTailRing(ListHead,Entry) {\
PHSW_RING_ENTRY _EX_Blink;\
PHSW_RING_ENTRY _EX_ListHead;\
_EX_ListHead = (ListHead);\
_EX_Blink = _EX_ListHead->Blink;\
(Entry)->Flink = _EX_ListHead;\
(Entry)->Blink = _EX_Blink;\
_EX_Blink->Flink = (Entry);\
_EX_ListHead->Blink = (Entry);\
}
#define HswRemove(remove) {\
PHSW_RING_ENTRY _EX_Blink;\
PHSW_RING_ENTRY _EX_Flink;\
_EX_Flink=(remove)->Flink;\
_EX_Blink=(remove)->Blink;\
_EX_Blink->Flink=_EX_Flink;\
_EX_Flink->Blink=_EX_Blink;\
}
#define InsertAfterEntry(Entry, Entry1) {\
(Entry1)->Flink = (Entry)->Flink;\
(Entry)->Flink = (Entry1); \
(Entry1)->Flink->Blink = (Entry1);\
(Entry1)->Blink = (Entry); \
}
#define InsertBeforeEntry(Entry, Entry1) {\
(Entry1)->Flink = (Entry);\
(Entry1)->Blink = (Entry)->Blink;\
(Entry)->Blink = (Entry1);\
(Entry1)->Blink->Flink = (Entry1);\
}
//
// VOID
// InsertHeadList(
// PLIST_ENTRY ListHead,
// PLIST_ENTRY Entry
// );
//
#define HswInsertHeadRing(ListHead,Entry) {\
PHSW_RING_ENTRY _EX_Flink;\
PHSW_RING_ENTRY _EX_ListHead;\
_EX_ListHead = (ListHead);\
_EX_Flink = _EX_ListHead->Flink;\
(Entry)->Flink = _EX_Flink;\
(Entry)->Blink = _EX_ListHead;\
_EX_Flink->Blink = (Entry);\
_EX_ListHead->Flink = (Entry);\
}
//
// PLIST_ENTRY NextEntry(PLIST_ENTRY entry);
#define NextEntry(entry) (entry)->Flink;
//
//PLIST_ENTRY PrvEntry(PLIST_ENTRY entry);
#define PrvEntry(entry) (entry)->Blink;
//backward traversal the ring //added by zhujw
#define __list_for_each_B(Entry, Listhead) \
for (Entry = (Listhead)->Blink; Entry != (Listhead); Entry = Entry->Blink)
////forward traversal the ring //added by zhujw
#define __list_for_each_F(Entry, Listhead)\
for (Entry = (Listhead)->Flink; Entry != (Listhead); Entry = Entry->Flink)
//typedef bool (*__pfnIsEqual)(void* Node, void* key);
//{{
typedef void (*__pfnIsEqual)(void* Node, void* key, ptbool retVal);
void* AfxFindNodeByKey(PHSW_RING_ENTRY listhead, void* key, __pfnIsEqual pfnisequal);
void* AfxRemoveNodeByKey(PHSW_RING_ENTRY listhead, void* key, __pfnIsEqual pfnisequal);
void* AfxRemoveNode(PHSW_RING_ENTRY delNode);
void* AfxRemoveHeadList(PHSW_RING_ENTRY listhead);
void* AfxSearchNode(PHSW_RING_ENTRY listhead,PHSW_RING_ENTRY node);
unsigned int AfxListCounts(PHSW_RING_ENTRY listhead);
void AfxInsertNodeBehindOfKey(PHSW_RING_ENTRY lisrhead,void* key,__pfnIsEqual pfnisequal);
void AfxInsertNodeFrontOfKey(PHSW_RING_ENTRY listhead,void* key,__pfnIsEqual pfnisequal);
PHSW_RING_ENTRY AfxListSplice(PHSW_RING_ENTRY listhead1,PHSW_RING_ENTRY listhead2);
//}}added by zhujw
#ifdef __cplusplus
}
#endif
template <class T>
class HswRing
{
public:
typedef struct tagClassTemplate
{
HSW_RING_ENTRY Link;
T data;
}Node,*PNode;
HswRing()
{
HswInitializeRingHead(&m_list_head);
m_icount = 0;
}
virtual ~HswRing()
{
if (m_icount>0)
{
free();
}
}
private:
PHSW_RING_ENTRY find(const T& _node)const
{
PHSW_RING_ENTRY searchF = NULL,searchB = NULL;
//tbool ret = 0 ;
if (HswIsRingEmpty(&m_list_head))
{
return NULL;
}
searchF = m_list_head.Flink;//指向head前一个节点
searchB = m_list_head.Blink;//指向head后一个节点
for(;;) //这个链表乃一个双向循环链表,哨兵节点即为m_list_head
{
if (_node == (PNode(searchB))->data)//当只有一个节点时,如果找到
{
return searchB;
}
else
{
if (searchB == searchF)
{
return NULL;//odd node当有奇数个结点时,前向结点和后向结点指向同一个结点时,即查找失败!
}
if (_node == (PNode(searchF))->data)
{
return searchF;
}
if (searchB->Blink == searchF)
{
return NULL;//even node当有偶数个结点时,前向结点和后向结点相邻时,即查找失败!
}
searchB=searchB->Blink,searchF=searchF->Flink;
continue;
}
}
}
public:
T* find_if(const T& _node)const
{
PNode p = (PNode)find(_node);
if (p != NULL)
{
return &p->data;
}
else
return NULL;
}
bool erase(const T& _node)
{
PHSW_RING_ENTRY delNode = find(_node);
PHSW_RING_ENTRY del_F,del_B;
if (delNode != NULL)
{
if (NULL == delNode->Blink && NULL == delNode->Flink)
{
delNode->Blink = delNode->Flink = delNode;
delete (PNode)delNode;
m_icount--;
return true;
}
del_F = delNode->Flink,del_B = delNode->Blink;
if (NULL != del_F && NULL != del_B)
{
del_F->Blink = del_B;
del_B->Flink = del_F;
delNode->Blink = delNode->Flink = delNode;
delete (PNode)delNode;
m_icount--;
return true;
}
}
return false;
}
bool empty()
{
return (0==m_icount);
}
int size()
{
return m_icount;
}
int count()
{
int counts = 0;
PHSW_RING_ENTRY ptmp = NULL;
if (HswIsRingEmpty(&m_list_head))
{
return 0;
}
ptmp=m_list_head.Blink;
while (ptmp->Blink!=m_list_head.Blink)
{
++counts;
ptmp=ptmp->Blink;
}
return counts;
}
void push_back(const T& n)
{
PNode p = new Node;
p->data = n;
HswInsertHeadRing(&m_list_head,&p->Link);
m_icount++;
}
void push_front(const T& n)
{
PNode p = new Node;
p->data = n;
HswInsertTailRing(&m_list_head,&p->Link);
m_icount++;
}
tbool pop_front(T& n)
{
if(HswIsRingEmpty(&m_list_head))
{return 0;}
else
{
PNode node = (PNode)RemoveTailRing(&m_list_head);
n = node->data;
delete node;
m_icount--;
return 1;
}
}
tbool pop_back(T& n)
{
if(HswIsRingEmpty(&m_list_head))
{return 0;}
else
{
PNode node = (PNode)RemoveHeadRing(&m_list_head);
n = node->data;
delete node;
m_icount--;
return 1;
}
}
void free()
{
PNode list_proc;
while (!HswIsRingEmpty(&m_list_head))
{
list_proc = (PNode)RemoveHeadRing(&m_list_head);
{
delete list_proc;
m_icount--;
}
}
}
T* operator[](int index)const
{
if (index < 0 || index >= m_icount)
{
return NULL;
}
PHSW_RING_ENTRY pos;
int i=0;
__list_for_each_B(pos,&m_list_head)
{
if (i == index)
{
T* ret = &((PNode)pos)->data;
return ret;
}
i++;
}
return NULL;
}
private:
HSW_RING_ENTRY m_list_head;
int m_icount;
};
#endif