wow-slist文件说明
- 项目地址:https://gitee.com/wow-iot/wow-iot7
- 本文件的的功能主要用于链表相关操作,主要涉及创建、销毁、插入、查找、移除、替换、获取、清空、遍历;
创建与销毁:
Slist_T* wow_slist_create(void)
{
Slist_T *slist = CALLOC(1,Slist_T);
CHECK_RET_VAL_ERRNO_P(slist,-SYSTEM_MALLOC_FAILED,"malloc Slist_T failed!\n");
memset(slist,0,sizeof(Slist_T));
slist->head = NULL;
slist->tail = NULL;
slist->entries = 0;
return (Slist_T*)slist;
}
void wow_slist_destroy(Slist_T** pptSlist, slist_destroy_func_t destroy)
{
CHECK_RET_VOID(pptSlist && *pptSlist);
Slist_T* slist = (Slist_T*)*pptSlist;
wow_slist_clear(slist, destroy);
FREE(slist);
*pptSlist = NULL;
}
在索引出插入
int wow_slist_insert_by_index(Slist_T* ptSlist, int nIdx, void *pData)
{
SlistEntry_T *new_entry = NULL;
CHECK_RET_VAL_P(ptSlist,-PARAM_INPUT_STRUCT_IS_NULL,"param input struct invalid!\n");
CHECK_RET_VAL_P(pData,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
new_entry = CALLOC(1, SlistEntry_T);
CHECK_RET_VAL_P(new_entry,-SYSTEM_MALLOC_FAILED,"malloc SlistEntry_T faild!\n");
memset(new_entry,0,sizeof(SlistEntry_T));
new_entry->data = pData;
if(nIdx <= 0){
new_entry->next = ptSlist->head;
ptSlist->head = new_entry;
if (ptSlist->tail == NULL){
ptSlist->tail = new_entry;
}
}else if(nIdx > ptSlist->entries){
if (ptSlist->tail){
ptSlist->tail->next = new_entry;
}
ptSlist->tail = new_entry;
if (ptSlist->head == NULL){
ptSlist->head = new_entry;
}
}else{
//查找索引位置
SlistEntry_T *entry = NULL;
for (entry = ptSlist->head; entry && nIdx > 1; entry = entry->next,nIdx--);
if(entry == NULL){
FREE(new_entry);
return -WOW_FAILED;
}
new_entry->next = entry->next;
if (!entry->next)
ptSlist->tail = new_entry;
entry->next = new_entry;
}
ptSlist->entries++;
return WOW_SUCCESS;
}
获取内容
void* wow_slist_peek_by_match(Slist_T* ptSlist,slist_match_func_t fMatch,const void *pArg)
{
SlistEntry_T *entry;
CHECK_RET_VAL_ERRNO_P(ptSlist,-PARAM_INPUT_STRUCT_IS_NULL,"param input struct invalid!\n");
CHECK_RET_VAL_ERRNO_P(fMatch,-PARAM_INPUT_DATA_IS_NULL,"param input func invalid!\n");
CHECK_RET_VAL_ERRNO_P(pArg,-PARAM_INPUT_DATA_IS_NULL,"param input data invalid!\n");
for (entry = ptSlist->head; entry; entry = entry->next)
if (fMatch(entry->data, pArg))
return entry->data;
return NULL;
}
移除内容
int wow_slist_remove_by_index(Slist_T* ptSlist, int nIdx)
{
SlistEntry_T *entry = NULL;
SlistEntry_T *prev = NULL;
CHECK_RET_VAL_P(ptSlist,PARAM_INPUT_STRUCT_IS_NULL,"param input struct invalid!\n");
CHECK_RET_VAL_P(nIdx >= 0 && nIdx < ptSlist->entries,PARAM_INPUT_ARG_INVALID,"param input arg invalid!\n");
for (entry = ptSlist->head; entry && nIdx--;prev = entry, entry = entry->next);
CHECK_RET_VAL_P(entry,-1,"remove nIdx entry failed!\n");
if (prev)
prev->next = entry->next;
else
ptSlist->head = entry->next;
if (!entry->next) ptSlist->tail = prev;
FREE(entry);
ptSlist->entries--;
return WOW_SUCCESS;
}
替换链表数据内容
int wow_slist_replace_by_index(Slist_T* ptSlist,int nIdx,void *pData)
{
SlistEntry_T *entry = NULL;
CHECK_RET_VAL_P(ptSlist,PARAM_INPUT_STRUCT_IS_NULL,"param input struct invalid!\n");
CHECK_RET_VAL_P(nIdx >= 0 && nIdx < ptSlist->entries,PARAM_INPUT_ARG_INVALID,"param input arg invalid!\n");
for (entry = ptSlist->head; entry && nIdx--; entry = entry->next);
CHECK_RET_VAL_P(entry,-1,"peek nIdx entry failed!\n");
entry->data = pData;
return WOW_SUCCESS;
}
清空链表
int wow_slist_clear(Slist_T* ptSlist, slist_destroy_func_t fDestroy)
{
SlistEntry_T *entry = NULL;
SlistEntry_T *tmp = NULL;
CHECK_RET_VAL_P(ptSlist,-PARAM_INPUT_STRUCT_IS_NULL,"param input struct invalid!\n");
CHECK_RET_VAL(ptSlist->head,0);
for (entry = ptSlist->head; entry; entry = tmp){
if(fDestroy) {
fDestroy(entry->data);
}
tmp = entry->next;
FREE(entry);
}
memset(ptSlist, 0, sizeof(Slist_T));
ptSlist->head = NULL;
ptSlist->tail = NULL;
ptSlist->entries = 0;
return WOW_SUCCESS;
}