⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 com_list.c

📁 将链表的插入删除操作改为通用队列
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#include "Com_List.h"

struct ComListItem_t* ComListCreate()
{
	struct ComListItem_t* pHead;

	pHead = (struct ComListItem_t*)malloc(sizeof(struct ComListItem_t));
	if ( NULL == pHead )
	{ 
		return NULL;
	}

	pHead->pFront = pHead;
	pHead->pNext  = pHead;
	
	return pHead;
}

int ComListInsert(struct ComListItem_t* pHead, void* pNode, int len )
{
	struct ComListItem_t* pItem;

	if ((NULL == pHead) || (NULL == pNode))
	{
		return 0;
	}
		
	pItem = (struct ComListItem_t*)malloc(len + sizeof(struct ComListItem_t));
	if (NULL == pItem)
	{
		return 0;
	}
	memcpy(pItem->Data, pNode, len );
	
	pItem->pFront = pHead->pFront;
	pHead->pFront->pNext = pItem;
	pHead->pFront = pItem;
	pItem->pNext = pHead;

	return 1;
}

int ComListSearch(struct ComListItem_t* pHead, void* pNode, int len, COMPAREFUNC pCompare)
{
	struct ComListItem_t* pItem;

	if ( (NULL == pHead) || (NULL == pNode) )
	{
		return 0;
	}
	
	if ( NULL == pCompare )
	{
		for (pItem = pHead->pNext; pItem != pHead; pItem = pItem->pNext)
		{
			if (!memcmp( (void*)pItem->Data, pNode, len))
			{
				return 1;
			}
		}
		return 0;
	}
	else
	{
		for (pItem = pHead->pNext; pItem != pHead; pItem = pItem->pNext)
		{
			if (pCompare(pItem->Data, pNode))
			{
				return 1;
			}
		}
		return 0;
	}
}

int ComListDelete(struct ComListItem_t* pHead, void* pNode, int len, COMPAREFUNC pCompare)
{
	struct ComListItem_t* pItem;

	if ( (NULL == pHead) || (NULL == pNode) )
	{
		return 0;
	}
	if (len < sizeof(struct ComListItem_t))
	{
		return 0;
	}

	if ( NULL == pCompare )
	{
		for (pItem = pHead->pNext; pItem != pHead; pItem = pItem->pNext)
		{
			if (!memcmp( (void*)pItem->Data, pNode, len))
			{
				pItem->pFront->pNext = pItem->pNext;
				pItem->pNext->pFront = pItem->pFront;
				free(pItem);
				return 1;
			}
		}
		return 0;
	}
	else
	{
		for (pItem = pHead->pNext; pItem != pHead; pItem = pItem->pNext)
		{
			if (pCompare(pItem, pNode))
			{
				pItem->pFront->pNext = pItem->pNext;
				pItem->pNext->pFront = pItem->pFront;
				free(pItem);
				return 1;
			}
		}
		return 0;
	}
}

int ComListErase(struct ComListItem_t* pHead)
{
	struct ComListItem_t *pItem, *pNext;
	
	if(NULL == pHead)
		return 1;

	pItem = pHead->pNext;
	while(pItem != pHead)
	{
		pNext = pItem->pNext;
		free(pItem);
		pItem = pNext;
	}

	/* delete the head node */
	free(pHead);

	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -