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;
}
相关推荐
乐鑫科技 Espressif13 分钟前
ESP-KeyBoard:基于 ESP32-S3 的三模客制化机械键盘
计算机外设·iot·乐鑫科技·机械键盘
weixin_4143219819 分钟前
Linux 编译Ubuntu24内核
linux·运维·服务器
drinow2 小时前
【纸飞机串口调试工具】预设曲线名称
arm开发·驱动开发·单片机·mcu·dsp开发·fpga·iot
xiaozhiwise2 小时前
Makefile 之 join
linux
儿时可乖了3 小时前
Linux 定时任务全解析
linux·oracle
程序员奇奥3 小时前
CentOS操作系统下安装Nacos
linux·运维·centos
Cedric74 小时前
韩顺平 一周学会Linux | Linux 实操篇-实用指令
linux
快乐飒男4 小时前
Linux基础05
linux·笔记·学习
Who_Mr.Lin5 小时前
【虚拟机】VMWare的CentOS虚拟机断电或强制关机出现问题
linux·运维·centos
我是唐青枫5 小时前
Linux nc 命令详解
linux·运维·服务器