当前位置:编程学习 > C/C++ >>

AMPS:双向链表源码解读

本节在上节单向链表的基础上看看AMPS中对双向链表的实现,与单向链表相同,双向链表在软件中的使用也相对比较广泛,在后面要讲到的Hash表、定时器、内存管理等模块中都会见到以双向链表作为基本数据结构。其实,双向链表在实现上使用了很多单向链表的操作,仅在插入、删除结点时需要多操作几步,所以理解了单向链表,这块就比较好理解了。
 
  同样,AMPS提供了以下API对双向链表进行操作:
 
  \
 
AMPS_LinkList.h
 
 
 
[cpp]  
#ifndef __HEADER_AMPS_LINKLIST_H__   
#define __HEADER_AMPS_LINKLIST_H__   
  
#ifdef __cplusplus   
extern "C"  
{  
#endif   
  
#include "AMPS_API.h"   
#include "AMPS_Defines.h"   
  
typedef struct _AMPSSList       t_AMPSSList;  
typedef struct _AMPSDList       t_AMPSDList;  
  
/*单向链表结构*/  
struct _AMPSSList  
{  
    void*           pvData;  
    t_AMPSSList*    poAMPSSListNext;  
    t_AMPSSList*    poAMPSSListPrev;  
};  
  
/*双向链表结构*/  
struct _AMPSDList  
{  
    unsigned char   uchCount;        /*结点个数*/  
    t_AMPSSList*    poAMPSSListHead;  
};  
  
t_AMPSDList* DList_Init(t_AMPSDList** r_ppoDList);  
int DList_Concat(t_AMPSDList** r_ppoDListSrc, t_AMPSDList* r_poDListDst);  
t_AMPSSList* DList_Append(t_AMPSDList* list, void* r_pvData);  
t_AMPSSList* DList_Prepend(t_AMPSDList* r_poDList, void* r_pvData);  
void DList_PrependGivenNode(t_AMPSDList* r_poDList, void* r_pvData, t_AMPSSList* r_poSListNode);  
void DList_AppendGivenNode(t_AMPSDList* r_poDList, void* r_pvData, t_AMPSSList* r_poSListNode);  
t_AMPSSList* DList_Search(t_AMPSDList* r_poDList, AMPS_LListCompareLinkDataCallback r_pfAMPS_LListCompareCallback, void* r_pvData);  
t_AMPSSList* DList_Find (t_AMPSDList* r_poDList, t_AMPSSList* r_poSListNode);  
int DList_Remove(t_AMPSDList** r_ppoDList, t_AMPSSList* r_poSListNode, AMPS_LListRemoveLinkDataCallback r_pfAMPS_LListRemoveLinkDataCallback);  
int DList_Sort(t_AMPSDList** r_ppoDList, AMPS_LListCompareLinkDataCallback r_pfAMPS_LListCompareCallback);  
void DList_SwapNodesData(t_AMPSSList* r_poNodeOne, t_AMPSSList* r_poNodeTwo);  
int DList_RemoveFirstNode(t_AMPSDList** r_ppoDList, AMPS_LListRemoveLinkDataCallback AMPS_LListRemoveLinkDataCallback);  
void DList_RemoveNthNode(t_AMPSDList** r_ppoDList, int r_nLocation, AMPS_LListRemoveLinkDataCallback AMPS_LListRemoveLinkDataCallback);  
t_AMPSSList* DList_InsertAfter (t_AMPSDList* r_poDList, t_AMPSSList* r_poSListPositionNode, void* r_pvData);  
t_AMPSSList* DList_InsertBefore (t_AMPSDList* r_poDList, t_AMPSSList* r_poSListPositionNode, void* r_pvData);  
void DList_Free(t_AMPSDList** r_ppoDList, AMPS_LListRemoveLinkDataCallback AMPS_LListRemoveLinkDataCallback);  
void DList_FreeEx(t_AMPSDList** r_ppoDList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvData);  
void DList_FreeNodes(t_AMPSDList** r_ppoDList, AMPS_LListRemoveLinkDataCallback AMPS_LListRemoveLinkDataCallback);  
void DList_Traverse(t_AMPSDList* r_poDList, AMPS_LListProcessCallback r_fpDList_ProcessCallback, void* r_pvArg);  
int DList_RemoveWithOutFree(t_AMPSDList* r_poDList, t_AMPSSList* r_poSListNode);  
int SList_RemoveWithOutFree(t_AMPSSList** r_ppoAMPSSListHead, t_AMPSSList* r_poAMPSSListNode);  
  
void* DList_GetNthNode(t_AMPSDList* r_poAMPSDList, int r_nNodeLocation);  
int DList_Copy(t_AMPSDList* r_poAMPSDListSrc, t_AMPSDList* r_poAMPSDListDest, AMPS_LListDuplicate r_pfAMPS_LListDuplicate);  
int DList_RemoveFromData(t_AMPSDList* r_poDList, void* r_pvData, AMPS_LListRemoveLinkDataCallback r_pfAMPS_LListRemoveLinkDataCallback);  
  
#ifdef __cplusplus   
  
}  
#endif   
  
#endif /* __HEADER_AMPS_LINKLIST_H__ */  
 
#ifndef __HEADER_AMPS_LINKLIST_H__
#define __HEADER_AMPS_LINKLIST_H__
 
#ifdef __cplusplus
extern "C"
{
#endif
 
#include "AMPS_API.h"
#include "AMPS_Defines.h"
 
typedef struct _AMPSSList t_AMPSSList;
typedef struct _AMPSDList t_AMPSDList;
 
/*单向链表结构*/
struct _AMPSSList
{
void* pvData;
t_AMPSSList* poAMPSSListNext;
t_AMPSSList* poAMPSSListPrev;
};
 
/*双向链表结构*/
struct _AMPSDList
{
unsigned char uchCount;        /*结点个数*/
t_AMPSSList* poAMPSSListHead;
};
 
t_AMPSDList* DList_Init(t_AMPSDList** r_ppoDList);
int DList_Concat(t_AMPSDList** r_ppoDListSrc, t_AMPSDList* r_poDListDst);
t_AMPSSList* DList_Append(t_AMPSDList* list, void* r_pvData);
t_AMPSSList* DList_Prepend(t_AMPSDList* r_poDList, void* r_pvData);
void DList_PrependGivenNode(t_AMPSDList* r_poDList, void* r_pvData, t_AMPSSList* r_poSListNode);
void DList_AppendGivenNode(t_AMPSDList* r_poDList, void* r_pvData, t_AMPSSList* r_poSListNode);
t_AMPSSList* DList_Search(t_AMPSDList* r_poDList, AMPS_LListCompareLinkDataCallback r_pfAMPS_LListCompareCallback, void* r_pvData);
t_AMPSSList* DList_Find (t_AMPSDList* r_poDList, t_AMPSSList* r_poSListNode);
int DList_Remove(t_AMPSDList** r_ppoDList, t_AMPSSList* r_poSListNode, AMPS_LListRemoveLinkDataCallback r_pfAMPS_LListRemoveLinkDataCallback);
int DList_Sort(t_AMPSDList** r_ppoDList, AMPS_LListCompareLinkDataCallback r_pfAMPS_LListCompareCallback);
void DList_SwapNodesData(t_AMPSSList* r_poNodeOne, t_AMPSSList* r_poNodeTwo);
int DList_RemoveFirstNode(t_AMPSDList** r_ppoDList, AMPS_LListRemoveLinkDataCallback AMPS_LListRemoveLinkDataCallback);
void DList_RemoveNthNode(t_AMPSDList** r_ppoDList, int r_nLocation, AMPS_LListRemoveLinkDataCallback AMPS_LListRemoveLinkDataCallback);
t_AMPSSList* DList_InsertAfter (t_AMPSDList* r_poDList, t_AMPSSList* r_poSListPositionNode, void* r_pvData);
t_AMPSSList* DList_InsertBefore (t_AMPSDList* r_poDList, t_AMPSSList* r_poSListPositionNode, void* r_pvData);
void DList_Free(t_AMPSDList** r_ppoDList, AMPS_LListRemoveLinkDataCallback AMPS_LListRemoveLinkDataCallback);
void DList_FreeEx(t_AMPSDList** r_ppoDList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvData);
void DList_FreeNodes(t_AMPSDList** r_ppoDList, AMPS_LListRemoveLinkDataCallback AMPS_LListRemoveLinkDataCallback);
void DList_Traverse(t_AMPSDList* r_poDList, AMPS_LListPr
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,