📄 list.c
字号:
/*************************************************************************** * 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 = (TSListNode*)malloc(sizeof(TSListNode)); if (pListNode) { pListNode->data = pData; pListNode->next = NULL; if (pList->tail) { pList->tail->next = pListNode; } if (!pList->head) { pList->head = 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){ 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){ 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->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 = pList->head; TSListNode* pPrev = NULL; 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -