像单向链表、双向链表、堆、栈等这些基本的数据结构在大型软件中均有很广泛的使用,所以今天看一下AMPS中单向链表的操作函数库,其定义了单向链表的非常多的操作API,且提供了回调函数接口,从而使链表结构用于不同类型的结点,且可以使用户自定义的函数来实现链表排序、结点操作。所以这部分的代码可以做为常用的工具代码,可以在其他项目中广泛应用。
先看看AMPS提供的单链表操作接口有哪些?如下,由函数名称就可以看出,有非常丰富的函数。
最后,看一下单向链表的实现源代码:
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"
int SList_InsertSortedPrepend(void** r_ppvList, void* r_pvKey, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
int SList_InsertSortedAppend(void** r_ppvList, void* r_pvKey, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
void SList_SortPrepend(void** r_ppvList, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
void SList_SortAppend(void** r_ppvList, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
int SList_Traverse(void* r_ppvList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvArg);
int SList_Copy(void** r_ppvDest, void* r_pvSrc, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback, AMPS_LListCloneCallback r_pfAMPS_LListCloneCallback);
int SList_RemoveKey(void** r_ppvList, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
void* SList_FindData (void* r_pvList, void* r_pvData);
void* SList_GetNextNode(void* r_pvList);
void* SList_GetNodeData(void* r_pvNode);
void SList_AppendGivenNode(void** r_ppvList, void* r_pvSListNode);
void SList_PrependGivenNode(void** r_ppvList, void* r_pvGivenNode, void* r_pvNewNode);
t_AMPSSList* SList_Prepend(t_AMPSSList** r_ppoSList, void* r_pvData);
t_AMPSSList* SList_AppendEx(t_AMPSSList** r_ppoSList, void* r_pvData);
int SList_Append(void** r_ppvList, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
t_AMPSSList* SList_Last(t_AMPSSList* r_poSList);
void* SList_Alloc(void* r_pvData);
t_AMPSSList* SList_Search(t_AMPSSList* r_poSList, AMPS_LListCompareLinkDataCallback r_pfAMPS_LListCompareCallback, void* r_pvSrcData);
t_AMPSSList* SList_Find (t_AMPSSList* r_poSList, t_AMPSSList* r_poSListNode);
int SList_Remove(t_AMPSSList** r_ppoSList, t_AMPSSList* r_poSListNode, AMPS_LListFreeLinkDataCallback r_pfAMPS_LListFreeLinkDataCallback);
int SList_Free(t_AMPSSList** r_ppoSList, AMPS_LListFreeLinkDataCallback r_pfAMPS_LListFreeLinkDataCallback);
int SList_FreeEx(t_AMPSSList** r_ppoSList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvData);
unsigned int SList_Count(const t_AMPSSList* r_cpoSList);
int SList_Concat(t_AMPSSList** r_ppoSListSrc, t_AMPSSList* r_poSListDst);
#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"
int SList_InsertSortedPrepend(void** r_ppvList, void* r_pvKey, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
int SList_InsertSortedAppend(void** r_ppvList, void* r_pvKey, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
void SList_SortPrepend(void** r_ppvList, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
void SList_SortAppend(void** r_ppvList, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
int SList_Traverse(void* r_ppvList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvArg);
int SList_Copy(void** r_ppvDest, void* r_pvSrc, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback, AMPS_LListCloneCallback r_pfAMPS_LListCloneCallback);
int SList_RemoveKey(void** r_ppvList, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
void* SList_FindData (void* r_pvList, void* r_pvData);
void* SList_GetNextNode(void* r_pvList);
void* SList_GetNodeData(void* r_pvNode);
void SList_AppendGivenNode(void** r_ppvList, void* r_pvSListNode);
void SList_PrependGivenNode(void** r_ppvList, void* r_pvGivenNode, void* r_pvNewNode);
t_AMPSSList* SList_Prepend(t_AMPSSList** r_ppoSList, void* r_pvData);
t_AMPSSList* SList_AppendEx(t_AMPSSList** r_ppoSList, void* r_pvData);
int SList_Append(void** r_ppvList, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback);
t_AMPSSList* SList_Last(t_AMPSSList* r_poSList);
void* SList_Alloc(void* r_pvData);
t_AMPSSList* SList_Search(t_AMPSSList* r_poSList, AMPS_LListCompareLinkDataCallback r_pfAMPS_LListCompareCallback, void* r_pvSrcData);
t_AMPSSList* SList_Find (t_AMPSSList* r_poSList, t_AMPSSList* r_poSListNode);
int SList_Remove(t_AMPSSList** r_ppoSList, t_AMPSSList* r_poSListNode, AMPS_LListFreeLinkDataCallback r_pfAMPS_LListFreeLinkDataCallback);
int SList_Free(t_AMPSSList** r_ppoSList, AMPS_LListFreeLinkDataCallback r_pfAMPS_LListFreeLinkDataCallback);
int SList_FreeEx(t_AMPSSList** r_ppoSList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvData);
unsigned int SList_Count(const t_AMPSSList* r_cpoSList);
int SList_Concat(t_AMPSSList** r_ppoSListSrc, t_AMPSSList* r_poSListDst);
#ifdef __cplusplus
}
#endif
#endif /* __HEADER_AMPS_LINKLIST_H__ */
AMPS_LinkList.c
[cpp]
/*****************************************************************
文件名称: AMPS_LinkList.c
功能描述: 链表操作API函数(单向链表和双向链表)
*****************************************************************/
#include "AMPS_LinkList.h"
#include "AMPS_MemMgt.h"
/*****************************************************************
函数名称: SList_Append
功能描述: 在链表后追加结点
入参::
void **list 原链表
void* pvData 待追加的结点
AMPS_LListCompareCallback compare 链表比较回调,此函数中没有使用
出参:
void **list 追加后的链表
返回值: