list.c

来自「mgcp协议源代码和测试程序,还有一个编译器」· C语言 代码 · 共 564 行

C
564
字号
/******************************************************************************  Copyright(C) 2005,2006 Frank ZHANG  All Rights Reserved.      This program is free software; you can redistribute it and/or modify it  under the terms of the GNU General Public License as published by the Free  Software Foundation; either version 2 of the License, or (at your option)  any later version.  This program is distributed in the hope that it will be useful, but WITHOUT  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for   more details.      You should have received a copy of the GNU General Public License along with  this program; if not, write to the Free Software Foundation, Inc., 59 Temple  Place, Suite 330, Boston, MA 02111-1307 USA. ******************************************************************************//*******************************************************************************      Authors                   :  Frank ZHANG (openmgcp@gmail.com)*      Description               :  ***      Date of creation          :  04/03/2005***      History                   :*      2005/04/03 Frank ZHANG    : - Creation******************************************************************************/#include <stdlib.h>#include <string.h>#include "list.h"#include "debg.h"/*************************************************************************** * Function          : SListInit * * Description       : Initialize single-linked list *                      * Input parameters  : pList - pointer to single-linked list *                      * Output parameters : pList - initialized list * * Return value      : None * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/void SListInit (TSList* pList){	if (pList)	{		pList->count = 0;		pList->cur =   NULL;		pList->pre =   NULL;		pList->head =  NULL;		pList->tail =  NULL;	}}/*************************************************************************** * Function          : SListReset * * Description       : Reset the current pointer in single-linked list to be *                     the header node pointer and the previous pointer to be *                     NULL; *                      * Input parameters  : pList - pointer to single-linked list *                      * Output parameters : pList - current pointer is same as header pointer * * Return value      : None * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/void SListReset(TSList* pList){	if (pList)	{		pList->pre = NULL;		pList->cur = pList->head;	}}/*************************************************************************** * Function          : SListNextNode * * Description       : Return the next node pointer to the current pointer *                      * Input parameters  : pList - pointer to single-linked list *                      * Output parameters : pList - current pointer pointer to the next node * * Return value      : The next node pointer of current pointer * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/TSListNode* SListNextNode(TSList* pList){	TSListNode *pNode = NULL;	if (pList && pList->cur)	{		pList->pre = pList->cur;		pNode = pList->cur->next;		pList->cur = pList->cur->next;	}	return pNode;}/*************************************************************************** * Function          : SListGetCurData * * Description       : Get the data pointer of current node *                      * Input parameters  : pList - pointer to single-linked list *                      * Output parameters : None * * Return value      : The pointer to current node's data * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/void* SListGetCurData(TSList* pList){	if (pList && pList->cur)	{		return pList->cur->data;	}	else	{		return NULL;	}}/*************************************************************************** * Function          : SListFreeAll * * Description       : Free each node and its data in single-linked list *                      * Input parameters  : pList - pointer to single-linked list *                      * Output parameters : pList - all nodes and their data are freed * * Return value      : None * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/void SListFreeAll(TSList* pList){	if (pList)	{		TSListNode *p1 = NULL;		TSListNode *p2 = pList->head;		while (p2)		{			p1 = p2;			p2 = p1->next;			free(p1->data);			free(p1);		}		SListInit(pList);	}}/*************************************************************************** * Function          : SListDel * * Description       : Delete all the nodes in single-linked list but don't  *                     free the node data *                      * Input parameters  : pList - pointer to single-linked list *                      * Output parameters : pList - all nodes are deleted * * Return value      : None * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/void SListDel(TSList* pList){	if (pList)	{		TSListNode *p1 = NULL;		TSListNode *p2 = pList->head;		while (p2)		{			p1 = p2;			p2 = p1->next;			free(p1);		}		SListInit(pList);	}}/*************************************************************************** * Function          : SListAppend * * Description       : Insert a node into the head of single-linked list *                      * Input parameters  : pList - pointer to single-linked list *                     pData - pointer to the data associated with the added *                             node * * Output parameters : pList - new node is added * * Return value      : The pointer of the new added node * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/TSListNode* SListAdd(TSList* pList, void* pData){	TSListNode* pListNode = NULL;	Assert(pList);	pListNode = (TSListNode*)malloc(sizeof(TSListNode));	if (pListNode != NULL) 	{		memset(pListNode, 0, sizeof(TSListNode));		pListNode->data = pData;		pListNode->next = pList->head;		pList->head = pListNode;		pList->count++;	}	return pListNode;}/*************************************************************************** * Function          : SListAppend * * Description       : Add a node into single-linked list *                      * Input parameters  : pList - pointer to single-linked list *                     pData - pointer to the data associated with the added *                             node * * Output parameters : pList - new node is added * * Return value      : The pointer of the new added node * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/TSListNode* SListAppend(TSList* pList, void* pData){	TSListNode* pListNode = NULL;	Assert(pList);	pListNode = (TSListNode*)malloc(sizeof(TSListNode));	if (pListNode) 	{		memset(pListNode, 0, sizeof(TSListNode));		pListNode->data = pData;		pListNode->next =  NULL;				if (pList->tail)		{			pList->tail->next = pListNode;		}		if (!pList->head)		{			pList->head = pListNode;			pList->cur = pListNode;		}		pList->tail = pListNode;		pList->count++;	}	return pListNode;}/*************************************************************************** * Function          : SListFindByPointer * * Description       : Find the node by its data pointer, and store the target *                     node pointer. *                      * Input parameters  : pList - pointer to single-linked list *                     pData - pointer to the data associated with the target *                             node * * Output parameters :  * * Return value      : The pointer to the tarfet node * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/TSListNode* SListFindByPointer(TSList* pList, void* pData){	Assert(pList);	pList->pre = NULL;	pList->cur = pList->head;	while (NULL != pList->cur) 	{		if (pList->cur->data == pData)		{			return pList->cur;		}		else		{			pList->pre = pList->cur;			pList->cur = pList->cur->next;		}	}	/* Found no target, reset the current and previous node pointer to be NULL */	pList->pre = pList->cur = NULL;	return NULL;}/*************************************************************************** * Function          : SListFind * * Description       : Find the node by its data, and store the target *                     node pointer into current node pointer. *                      * Input parameters  : pList - pointer to single-linked list *                     pData - pointer to the data associated with the target *                             node *                     CompFunc - Compare function, it return 0 if find the *                             target node. * * Output parameters :  * * Return value      : The pointer to the tarfet node * * Comments          :  * * History           : *  2005/09/13       : Creation * * Date              : Sep 13 2005, Frank Zhang **************************************************************************/TSListNode* SListFind(TSList* pList, void* pData, CompareEqualFunc CompFunc){	Assert(pList);	Assert(CompFunc);	pList->pre = NULL;	pList->cur = pList->head;	while (NULL != pList->cur)	{		if (CompFunc(pList->cur->data, pData) == 0)		{			return pList->cur;		}		else		{			pList->pre = pList->cur;			pList->cur = pList->cur->next;		}	}	/* Found no target, reset the current and previous node pointer to be NULL */	pList->pre = pList->cur = NULL;	return NULL;}/*************************************************************************** * Function          : SListDelCurNode * * Description       : Delete current node, but don't free the data associate *                     with the node *                      * Input parameters  : pList - pointer to single-linked list *                      * Output parameters : pList - current node is deleted and current node point *                             to the previous node of original curren node * * Return value      : None * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/void SListDelCurNode(TSList* pList){  	Assert(pList);	Assert(pList->cur);	if (pList->cur != NULL)	{		if (pList->pre == NULL)		{			/* The current node is the head node */			Assert(pList->head == pList->cur);			if (pList->head == pList->tail)			{				/* List only contain one element */				free(pList->cur);				SListInit(pList);				return;			}			pList->head = pList->cur->next;			free(pList->cur);			pList->cur = pList->head;		}		else		{			/* Check if the deleted node is tail */			if (pList->tail == pList->cur)			{				pList->tail = pList->pre;			}			pList->pre->next = pList->cur->next;			free(pList->cur);			pList->cur = pList->pre->next;		}		pList->count--;	}}/****************************************************************************** * Function          : SListDelNodeByData * * Description       : Find the node by its associated date and delete it, but *                     donn't free the data *                      * Input parameters  : pList - pointer to single-linked list *                     pData - ponter to the data the target node associated * * Output parameters : pList - target node is deleted if found * * Return value      : None * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang ******************************************************************************/void SListDelNodeByData(TSList* pList, void* pData){	TSListNode* pNode = NULL;	TSListNode* pPrev = NULL;	Assert(pList);	pNode = pList->head;	while (NULL != pNode)	{		if (pNode->data == pData)		{			break;		}		else		{			pPrev = pNode;			pNode = pNode->next;		}	}	if (NULL != pNode)	{		if (NULL == pPrev)		{			/* head node */			pList->head = pNode->next;		}		else 		{			pPrev->next = pNode->next;		}		if (pList->tail == pNode)		{			pList->tail = pPrev;		}		pList->count--;		free(pNode);	}}/*************************************************************************** * Function          : SListMerger * * Description       : Merger the source list into the destination list and                       reset the source list  *                      * Input parameters  : pDesList - pointer to destination single-linked list *                     pSrcList - ponter to source single-linked list * * Output parameters : pDesList - Source list data node is appended * * Return value      : None * * Comments          :  * * History           : *  2005/04/03       : Creation * * Date              : April 03 2005, Frank Zhang **************************************************************************/ void SListMerger(TSList* pDesList, TSList* pSrcList){	Assert(pDesList);	Assert(pSrcList);	if (pSrcList->count > 0)	{		if (pDesList->count == 0)		{			pDesList->head = pSrcList->head;			pDesList->tail = pSrcList->tail;		}		else		{			pDesList->tail->next = pSrcList->head;			pDesList->tail = pSrcList->tail;		}		pDesList->count += pSrcList->count;		SListInit(pSrcList);	}}

⌨️ 快捷键说明

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