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;
}
相关推荐
007php0074 小时前
linux服务器上CentOS的yum和Ubuntu包管理工具apt区别与使用实战
linux·运维·服务器·ubuntu·centos·php·ai编程
djykkkkkk4 小时前
ubuntu编译遇到的问题
linux·运维·ubuntu
qq_429856575 小时前
linux 查看服务是否开机自启动
linux·运维·服务器
7yewh6 小时前
Linux驱动开发 IIC I2C驱动 编写APP访问EEPROM AT24C02
linux·arm开发·驱动开发·嵌入式硬件·嵌入式
dessler6 小时前
Docker-Dockerfile讲解(三)
linux·运维·docker
KevinRay_7 小时前
命令行之巅:Linux Shell编程的至高艺术(中)
linux·运维·服务器·重定向·shell编程
程序员JerrySUN7 小时前
Yocto 项目 - 共享状态缓存 (Shared State Cache) 机制
linux·嵌入式硬件·物联网·缓存·系统架构
林农8 小时前
C05S16-MySQL高可用
linux·mysql·云计算
码中小白鼠8 小时前
Ubuntu系统部署Mysql8.0后设置不区分大小写
linux·mysql·ubuntu·adb
gz94568 小时前
Virtualbox安装ubuntu20虚拟机无法打开终端
java·linux·开发语言