wow-slist文件说明

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;
}
相关推荐
a26637896几秒前
解决yum命令报错“Could not resolve host: mirrorlist.centos.org
linux·运维·centos
北京天拓四方1 小时前
5G工业网关的主要功能有哪些?天拓四方
物联网·其他·5g·边缘计算·iot
2739920291 小时前
Ubuntu20.04 安装build-essential问题
linux
wowocpp5 小时前
查看 linux ubuntu 分区 和 挂载 情况 lsblk
linux·运维·ubuntu
wowocpp5 小时前
查看 磁盘文件系统格式 linux ubuntu blkid ext4
linux·数据库·ubuntu
龙鸣丿6 小时前
Linux基础学习笔记
linux·笔记·学习
耶啵奶膘7 小时前
uniapp-是否删除
linux·前端·uni-app
2401_850410838 小时前
文件系统和日志管理
linux·运维·服务器
XMYX-09 小时前
使用 SSH 蜜罐提升安全性和记录攻击活动
linux·ssh
二十雨辰11 小时前
[linux]docker基础
linux·运维·docker