像單向鏈表、雙向鏈表、堆、棧等這些基本的數據結構在大型軟件中均有很廣泛的使用,所以今天看一下AMPS中單向鏈表的操作函數庫,其定義了單向鏈表的非常多的操作API,且提供了回調函數接口,從而使鏈表結構用於不同類型的結點,且可以使用戶自定義的函數來實現鏈表排序、結點操作。所以這部分的代碼可以做為常用的工具代碼,可以在其他項目中廣泛應用。 先看看AMPS提供的單鏈表操作接口有哪些?如下,由函數名稱就可以看出,有非常豐富的函數。 最後,看一下單向鏈表的實現源代碼: AMPS_LinkList.h [cpp] #ifndef __HEADER_AMPS_LINKLIST_H__ #define __HEADER_AMPS_LINKLIST_H__ #ifdef __cplusplus extern "C" { #endif #include "AMPS_API.h" #include "AMPS_Defines.h" int SList_InsertSortedPrepend(void** r_ppvList, void* r_pvKey, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); int SList_InsertSortedAppend(void** r_ppvList, void* r_pvKey, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); void SList_SortPrepend(void** r_ppvList, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); void SList_SortAppend(void** r_ppvList, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); int SList_Traverse(void* r_ppvList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvArg); int SList_Copy(void** r_ppvDest, void* r_pvSrc, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback, AMPS_LListCloneCallback r_pfAMPS_LListCloneCallback); int SList_RemoveKey(void** r_ppvList, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); void* SList_FindData (void* r_pvList, void* r_pvData); void* SList_GetNextNode(void* r_pvList); void* SList_GetNodeData(void* r_pvNode); void SList_AppendGivenNode(void** r_ppvList, void* r_pvSListNode); void SList_PrependGivenNode(void** r_ppvList, void* r_pvGivenNode, void* r_pvNewNode); t_AMPSSList* SList_Prepend(t_AMPSSList** r_ppoSList, void* r_pvData); t_AMPSSList* SList_AppendEx(t_AMPSSList** r_ppoSList, void* r_pvData); int SList_Append(void** r_ppvList, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); t_AMPSSList* SList_Last(t_AMPSSList* r_poSList); void* SList_Alloc(void* r_pvData); t_AMPSSList* SList_Search(t_AMPSSList* r_poSList, AMPS_LListCompareLinkDataCallback r_pfAMPS_LListCompareCallback, void* r_pvSrcData); t_AMPSSList* SList_Find (t_AMPSSList* r_poSList, t_AMPSSList* r_poSListNode); int SList_Remove(t_AMPSSList** r_ppoSList, t_AMPSSList* r_poSListNode, AMPS_LListFreeLinkDataCallback r_pfAMPS_LListFreeLinkDataCallback); int SList_Free(t_AMPSSList** r_ppoSList, AMPS_LListFreeLinkDataCallback r_pfAMPS_LListFreeLinkDataCallback); int SList_FreeEx(t_AMPSSList** r_ppoSList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvData); unsigned int SList_Count(const t_AMPSSList* r_cpoSList); int SList_Concat(t_AMPSSList** r_ppoSListSrc, t_AMPSSList* r_poSListDst); #ifdef __cplusplus } #endif #endif /* __HEADER_AMPS_LINKLIST_H__ */ #ifndef __HEADER_AMPS_LINKLIST_H__ #define __HEADER_AMPS_LINKLIST_H__ #ifdef __cplusplus extern "C" { #endif #include "AMPS_API.h" #include "AMPS_Defines.h" int SList_InsertSortedPrepend(void** r_ppvList, void* r_pvKey, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); int SList_InsertSortedAppend(void** r_ppvList, void* r_pvKey, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); void SList_SortPrepend(void** r_ppvList, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); void SList_SortAppend(void** r_ppvList, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); int SList_Traverse(void* r_ppvList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvArg); int SList_Copy(void** r_ppvDest, void* r_pvSrc, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback, AMPS_LListCloneCallback r_pfAMPS_LListCloneCallback); int SList_RemoveKey(void** r_ppvList, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); void* SList_FindData (void* r_pvList, void* r_pvData); void* SList_GetNextNode(void* r_pvList); void* SList_GetNodeData(void* r_pvNode); void SList_AppendGivenNode(void** r_ppvList, void* r_pvSListNode); void SList_PrependGivenNode(void** r_ppvList, void* r_pvGivenNode, void* r_pvNewNode); t_AMPSSList* SList_Prepend(t_AMPSSList** r_ppoSList, void* r_pvData); t_AMPSSList* SList_AppendEx(t_AMPSSList** r_ppoSList, void* r_pvData); int SList_Append(void** r_ppvList, void* r_pvData, AMPS_LListCompareCallback r_pfAMPS_LListCompareCallback); t_AMPSSList* SList_Last(t_AMPSSList* r_poSList); void* SList_Alloc(void* r_pvData); t_AMPSSList* SList_Search(t_AMPSSList* r_poSList, AMPS_LListCompareLinkDataCallback r_pfAMPS_LListCompareCallback, void* r_pvSrcData); t_AMPSSList* SList_Find (t_AMPSSList* r_poSList, t_AMPSSList* r_poSListNode); int SList_Remove(t_AMPSSList** r_ppoSList, t_AMPSSList* r_poSListNode, AMPS_LListFreeLinkDataCallback r_pfAMPS_LListFreeLinkDataCallback); int SList_Free(t_AMPSSList** r_ppoSList, AMPS_LListFreeLinkDataCallback r_pfAMPS_LListFreeLinkDataCallback); int SList_FreeEx(t_AMPSSList** r_ppoSList, AMPS_LListProcessCallback r_pfAMPS_LListProcessCallback, void* r_pvData); unsigned int SList_Count(const t_AMPSSList* r_cpoSList); int SList_Concat(t_AMPSSList** r_ppoSListSrc, t_AMPSSList* r_poSListDst); #ifdef __cplusplus } #endif #endif /* __HEADER_AMPS_LINKLIST_H__ */ AMPS_LinkList.c [cpp] /***************************************************************** 文件名稱: AMPS_LinkList.c 功能描述: 鏈表操作API函數(單向鏈表和雙向鏈表) *****************************************************************/ #include "AMPS_LinkList.h" #include "AMPS_MemMgt.h" /***************************************************************** 函數名稱: SList_Append 功能描述: 在鏈表後追加結點 入參:: void **list 原鏈表 void* pvData 待追加的結點 AMPS_LListCompareCallback compare 鏈表比較回調,此函數中沒有使用 出參: void **list 追加後的鏈表 返回值: int *****************************************************************/ int SList_Append(void **list, void* pvData, AMPS_LListCompareCallback compare) { t_AMPSSList** slist = (t_AMPSSList**)list; { t_AMPSSList *new_list = NULL; // pointer to newly created node t_AMPSSList *last = NULL; // pointer to last node of link list t_AMPSSList *list_ptr = *slist; /*在鏈表中分配新結點所要使用的內存空間*/ new_list = SList_Alloc(pvData); if (NULL == new_list) { return -1; } if (list_ptr) { /*找到當前鏈表結束*/ last = SList_Last(list_ptr); if (NULL == last) { free(new_list); return -1; } /*將待追加的結點連接到當前鏈表結尾*/ last->poAMPSSListNext = new_list; new_list->poAMPSSListPrev = last; } else { /*如果當前鏈表為空,則待追加結點做為鏈表頭結點*/ *slist = new_list; } return 0; } } /***************************************************************** 函數名稱: SList_InsertSortedPrepend 功能描述: 在有序鏈表中前向插入新結點 入參:: void **list 原鏈表 void* key 未使用 void* pvData 待插入結點 AMPS_LListCompareCallback compare 結點比較回調函數,由調用者實現 出參: void **list 追加後的鏈表 返回值: int *****************************************************************/ int SList_InsertSortedPrepend(void **list, void* key, void* pvData, AMPS_LListCompareCallback compare) { t_AMPSSList** slist = (t_AMPSSList**)list; /*判斷要添加的結點是否已在鏈表中存在,已存在,直接返回成功*/ if(NULL != SList_FindData(*slist,pvData) ) { printf("slist_insert_sorted: node already found in list\n"); return 0; } { t_AMPSSList *new_node = NULL; // pointer to newly created node t_AMPSSList *list_ptr = *slist, *curr; new_node = SList_Alloc(pvData); if (NULL == new_node) { return -1; } if (list_ptr) { /*遍歷鏈表*/ while (list_ptr) { /*如果當前鏈表結點不大於待插入結點,將其插入此結點之後*/ if (AMPS_LLIST_KEY_IS_GREATER != compare(list_ptr->pvData, pvData)) { break; } curr = list_ptr; list_ptr = list_ptr->poAMPSSListNext; } if(!list_ptr) { /*如果已到鏈表結尾,剛將新結點插入到最後*/ SList_AppendGivenNode((void **)&curr, (void*)new_node); } else { /*未到結尾已找到插入位置,剛插入到鏈表中間*/ SList_PrependGivenNode(list, (void*)list_ptr,(void*)new_node); } } else { *slist = new_node; } return 0; } } /***************************************************************** 函數名稱: SList_InsertSortedAppend 功能描述: 在遞增的有序鏈表中插入新結點 入參:: void **list 原鏈表 void* key 未使用 void* pvData 待插入結點 AMPS_LListCompareCallback compare 結點比較回調函數,由調用者實現 出參: void **list 追加後的鏈表 返回值: int *****************************************************************/ int SList_InsertSortedAppend(void **list, void* key, void* pvData, AMPS_LListCompareCallback compare) { t_AMPSSList** slist = (t_AMPSSList**)list; if(NULL != SList_FindData(*slist,pvData) ) { printf("slist_insert_sorted: node already found in list\n"); return 0; } { t_AMPSSList *new_node = NULL; // pointer to newly created node t_AMPSSList *list_ptr = *slist, *curr; new_node = SList_Alloc(pvData); if (NULL == new_node) { return -1; } if (list_ptr) { while (list_ptr) { if (AMPS_LLIST_KEY_IS_SMALLER == compare(list_ptr->pvData, pvData)) { break; } curr = list_ptr; list_ptr = list_ptr->poAMPSSListNext; } if(!list_ptr) { SList_AppendGivenNode((void **)&curr, (void*)new_node); } else { SList_PrependGivenNode(list, (void*)list_ptr,(void*)new_node); } } else { *slist = new_node; } return 0; } } /***************************************************************** 函數名稱: SList_SortPrepend 功能描述: 鏈表排序 入參:: void **list 原鏈表 AMPS_LListCompareCallback compare 結點比較回調函數,由調用者實現 出參: void **list 排序後的鏈表 返回值: void *****************************************************************/ void SList_SortPrepend(void** list,AMPS_LListCompareCallback compare) { t_AMPSSList** slist = (t_AMPSSList**)list; t_AMPSSList *list_ptr = *slist; if (list_ptr) { while (list_ptr) { t_AMPSSList* poAMPSSListNext = list_ptr->poAMPSSListNext; /*保存待刪除結點的內容*/ void* pvData = list_ptr->pvData; /*從鏈表中刪除結點*/ SList_Remove(slist,list_ptr,NULL); /*將已刪除結點的內容插入到排序後的鏈表*/ SList_InsertSortedPrepend((void **)slist,NULL,pvData,compare); list_ptr = poAMPSSListNext; } } } /***************************************************************** 函數名稱: SList_SortAppend 功能描述: 鏈表排序(嚴格遞增) 入參:: void **list 原鏈表 AMPS_LListCompareCallback compare 結點比較回調函數,由調用者實現 出參: void **list 排序後的鏈表 返回值: void *****************************************************************/ void SList_SortAppend(void** list,AMPS_LListCompareCallback compare) { t_AMPSSList** slist = (t_AMPSSList**)list; t_AMPSSList *list_ptr = SList_Last(*slist); if (list_ptr) { while (list_ptr) { t_AMPSSList* poAMPSSListPrev = list_ptr->poAMPSSListPrev; void* pvData = list_ptr->pvData; SList_Remove(slist,list_ptr,NULL); SList_InsertSortedAppend((void **)slist,NULL,pvData,compare); list_ptr = poAMPSSListPrev; } } } /***************************************************************** 函數名稱: SList_Traverse 功能描述: 遍歷並通過用戶自定義的回調函數處理各結點 入參:: void **list 原鏈表 AMPS_LListProcessCallback compare 結點處理回調函數,由調用者實現 void* arg 回調函數參數 出參: void **list 處理之後的鏈表 返回值: int *****************************************************************/ int SList_Traverse(void* list,AMPS_LListProcessCallback func,void* arg) { t_AMPSSList *slist = (t_AMPSSList*)list,*tempList = NULL; if(!slist) { return 0; } for(tempList = slist;tempList;tempList = tempList->poAMPSSListNext) { func(tempList->pvData,arg); } return 0; } /***************************************************************** 函數名稱: SList_Traverse 功能描述: 復制一份原鏈表,在復制過程中可以處理各結點,以實現對於不同類型 結點的操作。 入參:: void** dest 目標鏈表 void* src 源鏈表 AMPS_LListCompareCallback compare 結點比較回調,沒有使用 AMPS_LListCloneCallback clone 結點處理函數回調 出參: void** dest 處理之後的鏈表 返回值: int *****************************************************************/ int SList_Copy(void** dest,void* src,AMPS_LListCompareCallback compare,AMPS_LListCloneCallback clone) { t_AMPSSList* srcList = (t_AMPSSList*)src; void* pvData; if(!srcList) { return 0; } for(;srcList;srcList = srcList->poAMPSSListNext) { /*逐一處理各結點,比如結點數據為char*,則需要strcpy,如為int,直接賦值*/ pvData = clone(srcList->pvData); /*將處理後的結點插入目標鏈表,最後,兩鏈表長度相同,但結點內容可以不同*/ SList_Append(dest,pvData,NULL); } return 0; } /***************************************************************** 函數名稱: SList_AppendEx 功能描述: 向鏈表後追加結點,返回最終鏈表 入參:: t_AMPSSList** list 原鏈表 void* pvData 待追加結點 出參: NA 返回值: t_AMPSSList* 處理之後的鏈表 *****************************************************************/ t_AMPSSList* SList_AppendEx(t_AMPSSList** list, void* pvData) { t_AMPSSList** slist = (t_AMPSSList**)list; t_AMPSSList* new_list = NULL; // pointer to newly created node t_AMPSSList* last = NULL; // pointer to last node of link list t_AMPSSList* list_ptr = *slist; new_list = SList_Alloc(pvData); if(NULL == new_list) { return NULL; } if(list_ptr) { last = SList_Last(list_ptr); if(NULL == last) { free(new_list); return NULL; } last->poAMPSSListNext = new_list; new_list->poAMPSSListPrev = last; } else { *slist = new_list; } return new_list; } /***************************************************************** 函數名稱: SList_Prepend 功能描述: 向鏈表頭追加結點,返回最終鏈表 入參:: t_AMPSSList** list 原鏈表 void* pvData 待追加結點 出參: NA 返回值: t_AMPSSList* 處理之後的鏈表 *****************************************************************/ t_AMPSSList* SList_Prepend(t_AMPSSList **list, void* pvData) { t_AMPSSList *new_list = NULL; t_AMPSSList *list_ptr = *list; new_list = (t_AMPSSList*)SList_Alloc(pvData); if (NULL == new_list) { return NULL; } new_list->poAMPSSListNext = list_ptr; new_list->poAMPSSListPrev = NULL; if (list_ptr) { list_ptr->poAMPSSListPrev = new_list; } *list = new_list; return new_list; } /***************************************************************** 函數名稱: SList_Last 功能描述: 查找鏈表結尾 入參:: t_AMPSSList** list 原鏈表 出參: NA 返回值: t_AMPSSList* 處理之後的鏈表 *****************************************************************/ t_AMPSSList* SList_Last(t_AMPSSList *list) {/* if (NULL != list) { */ if (list) { while (list->poAMPSSListNext) { list = list->poAMPSSListNext; } } //} return list; } /***************************************************************** 函數名稱: SList_Alloc 功能描述: 分配新的鏈表結點,並賦值 入參:: void* pvData 結點 出參: NA 返回值: void* 處理之後的鏈表 *****************************************************************/ void* SList_Alloc(void* pvData) { t_AMPSSList* new_list = NULL; new_list = AMPS_InternalMalloc(sizeof(t_AMPSSList)); if(NULL == new_list) { printf("=>>>>>>>>>>>>>>>>>>>>> malloc failed.\n\n\n\n\n\n\n\n"); return NULL; } new_list->pvData = pvData; new_list->poAMPSSListNext = NULL; new_list->poAMPSSListPrev = NULL; return (void*)new_list; } /***************************************************************** 函數名稱: SList_RemoveKey 功能描述: 刪除鏈表中的指定結點 入參:: void** list 原鏈表 void* pvData 待刪除結點 AMPS_LListCompareCallback compare 結點比較回調函數 出參: void** list 返回值: int *****************************************************************/ int SList_RemoveKey(void** list,void* pvData,AMPS_LListCompareCallback compare) { t_AMPSSList** slist = (t_AMPSSList**)list; t_AMPSSList *list_ptr = (t_AMPSSList*)*slist; while (list_ptr) { /*使用用戶自定義的回調函數比較結點,相等,則從鏈表中刪除*/ if (AMPS_LLIST_KEYS_ARE_EQUAL == compare(list_ptr->pvData,pvData)) { SList_Remove(slist, (void*)list_ptr, NULL); break; } list_ptr = list_ptr->poAMPSSListNext; } return 0; } /***************************************************************** 函數名稱: SList_Search 功能描述: 在鏈表中查找符合條件的結點 入參:: t_AMPSSList *list 原鏈表 AMPS_LListCompareCallback func_ptr 結點查找回調函數 void *src_data 指定的查找條件 出參: void** list 返回值: t_AMPSSList* 找到的結點指針 *****************************************************************/ t_AMPSSList* SList_Search (t_AMPSSList *list, AMPS_LListCompareLinkDataCallback func_ptr, void *src_data) { while (list) { if (!func_ptr(src_data, list->pvData)) { break; } list = list->poAMPSSListNext; } return list; } /***************************************************************** 函數名稱: SList_Find 功能描述: 在鏈表中查找指定的結點 入參:: t_AMPSSList *list 原鏈表 t_AMPSSList *node 指定的結點 出參: t_AMPSSList *list 返回值: t_AMPSSList* 找到的結點指針 *****************************************************************/ t_AMPSSList* SList_Find (t_AMPSSList *list, t_AMPSSList *node) { while (list) { if (list == node) { return list; } list = list->poAMPSSListNext; } return NULL; } /***************************************************************** 函數名稱: SList_Find 功能描述: 在鏈表中查找指定的結點內容 入參:: void *list 原鏈表 void *pvData 指定的結點內容 出參: t_AMPSSList *list 返回值: void* 找到的結點指針 *****************************************************************/ void* SList_FindData (void *list, void *pvData) { t_AMPSSList *slist = (t_AMPSSList*)list; while (slist) { if (slist->pvData == pvData) { return slist; } slist = slist->poAMPSSListNext; } return NULL; } /***************************************************************** 函數名稱: SList_Free 功能描述: 釋放鏈表 入參:: void *list 原鏈表 AMPS_LListFreeLinkDataCallback func_ptr 結點處理回調函數,如果結點 的內存需要顯式釋放,使用此函數達到目的。 出參: t_AMPSSList *list 返回值: int *****************************************************************/ int SList_Free(t_AMPSSList **list, AMPS_LListFreeLinkDataCallback func_ptr) { t_AMPSSList *curr = NULL; // pointer to current node t_AMPSSList *poAMPSSListPrev = NULL; // pointer to previous node curr = *list; while (curr) { poAMPSSListPrev = curr; curr = (t_AMPSSList*)curr->poAMPSSListNext; if (func_ptr) { /*釋放結點*/ if (func_ptr(&(poAMPSSListPrev->pvData))) { return AMPS_ERROR_FAILURE; } } /*if (poAMPSSListPrev->pvData) { AMPS_InternalFree(poAMPSSListPrev->pvData); }*/ AMPS_InternalFree(poAMPSSListPrev); } *list = NULL; return AMPS_SUCCESS; } /***************************************************************** 函數名稱: SList_FreeEx 功能描述: 釋放鏈表 入參:: void *list 原鏈表 AMPS_LListFreeLinkDataCallback func_ptr 結點處理回調函數,如果結點 的內存需要顯式釋放,使用此函數達到目的。 void* r_pvData 回調函數入參,通過它可以進行在釋放前的一些結點操作 出參: t_AMPSSList *list 返回值: int *****************************************************************/ int SList_FreeEx(t_AMPSSList **list, AMPS_LListProcessCallback func_ptr, void* r_pvData) { t_AMPSSList *curr = NULL; // pointer to current node t_AMPSSList *poAMPSSListPrev = NULL; // pointer to previous node curr = *list; while (curr) { poAMPSSListPrev = curr; curr = (t_AMPSSList*)curr->poAMPSSListNext; if (func_ptr) { func_ptr(poAMPSSListPrev->pvData, r_pvData); } AMPS_InternalFree(poAMPSSListPrev); } *list = NULL; return AMPS_SUCCESS; } /***************************************************************** 函數名稱: SList_FreeEx 功能描述: 刪除鏈表中指定的結點 入參:: t_AMPSSList **list 原鏈表 t_AMPSSList *node 待刪除結點 AMPS_LListFreeLinkDataCallback func_ptr 結點處理回調函數,如果結點 的內存需要顯式釋放,使用此函數達到目的。 出參: t_AMPSSList *list 返回值: int *****************************************************************/ int SList_Remove(t_AMPSSList **list, t_AMPSSList *node, AMPS_LListFreeLinkDataCallback func_ptr) { if ((list == NULL) || (*list == NULL) || (node == NULL)) { return AMPS_ERROR_FAILURE; } if (func_ptr) { /*處理待刪除結點*/ if (AMPS_ERROR_FAILURE == func_ptr(&(node->pvData))) { return AMPS_ERROR_FAILURE; } } /*待刪除結點為頭結點*/ // If first element is found to be the element to be deleted if (NULL == node->poAMPSSListPrev) { /*待刪除結點不是頭結點,則返回失敗*/ if (*list != node) { printf("SList_Remove: list corrupted: poAMPSSListPrev pointer NULL but node is not the first element in list\n"); return AMPS_ERROR_FAILURE; } *list = node->poAMPSSListNext; if (*list) // We have deleted the last element in the list (*list)->poAMPSSListPrev = NULL; AMPS_InternalFree(node); return AMPS_SUCCESS; } /*待刪除結點不是頭結點*/ node->poAMPSSListPrev->poAMPSSListNext = node->poAMPSSListNext; if (node->poAMPSSListNext) { node->poAMPSSListNext->poAMPSSListPrev = node->poAMPSSListPrev; } AMPS_InternalFree(node); return AMPS_SUCCESS; } /***************************************************************** 函數名稱: SList_Count 功能描述: 計算鏈表中的結點個數 入參:: const t_AMPSSList* list 原鏈表 出參: NA 返回值: int 結點數目 *****************************************************************/ unsigned int SList_Count(const t_AMPSSList* list) { unsigned int uchCount = 0; t_AMPSSList* cur_ptr = (t_AMPSSList*)list; while (cur_ptr) { uchCount++; cur_ptr = cur_ptr->poAMPSSListNext; } return uchCount; } /***************************************************************** 函數名稱: SList_Concat 功能描述: 連接兩個鏈表 入參:: t_AMPSSList** src 原鏈表 t_AMPSSList* dst 待連接的鏈表 出參: NA 返回值: int *****************************************************************/ int SList_Concat(t_AMPSSList** src, t_AMPSSList* dst) { t_AMPSSList *last = NULL; if (src == NULL || dst == NULL) { return AMPS_ERROR_FAILURE; } if (*src == NULL) { *src = dst; } else { last = SList_Last(*src); last->poAMPSSListNext = dst; dst->poAMPSSListPrev = last; } return AMPS_SUCCESS; } /***************************************************************** 函數名稱: SList_AppendGivenNode 功能描述: 在鏈表頭結點後插入指定結點 入參:: void **list 原鏈表 void* sListNode 待插入結點 出參: void **list 返回值: void *****************************************************************/ void SList_AppendGivenNode(void **list, void* sListNode) { t_AMPSSList **slist = (t_AMPSSList**)list; t_AMPSSList* list_ptr = (t_AMPSSList*)*slist; t_AMPSSList* snode = (t_AMPSSList*)sListNode; if(!list_ptr) { *slist = snode; return; } snode->poAMPSSListNext = list_ptr->poAMPSSListNext; snode->poAMPSSListPrev = list_ptr; list_ptr->poAMPSSListNext = snode; } /***************************************************************** 函數名稱: SList_AppendGivenNode 功能描述: 在指定的結點後插入指定新結點 入參:: void **list 原鏈表 void* givenNode 指定的結點 void* newNode 待插入結點 出參: void **list 返回值: void *****************************************************************/ void SList_PrependGivenNode(void **list, void* givenNode, void* newNode) { t_AMPSSList **slist = (t_AMPSSList**)list; t_AMPSSList* node_ptr = (t_AMPSSList*)givenNode; t_AMPSSList* snode = (t_AMPSSList*)newNode; if(!node_ptr) { *slist = snode; return; } snode->poAMPSSListNext = node_ptr; snode->poAMPSSListPrev = node_ptr->poAMPSSListPrev; if(NULL == node_ptr->poAMPSSListPrev) { *slist = snode; } else { node_ptr->poAMPSSListPrev->poAMPSSListNext = snode; } node_ptr->poAMPSSListPrev = snode; } /***************************************************************** 函數名稱: SList_RemoveWithOutFree 功能描述: 刪除鏈表中指定的結點,但不釋放結點內容 入參:: t_AMPSSList** r_ppoAMPSSListHead 原鏈表 t_AMPSSList* r_poAMPSSListNode 待刪除結點 出參: t_AMPSSList** r_ppoAMPSSListHead 返回值: int *****************************************************************/ int SList_RemoveWithOutFree(t_AMPSSList** r_ppoAMPSSListHead, t_AMPSSList* r_poAMPSSListNode) { if((NULL == r_ppoAMPSSListHead) || (NULL == *r_ppoAMPSSListHead) || (NULL == r_poAMPSSListNode)) { return AMPS_ERROR_FAILURE; } /*待刪除結點為頭結點*/ // If first element is found to be the element to be deleted if(NULL == r_poAMPSSListNode->poAMPSSListPrev) { if(*r_ppoAMPSSListHead != r_poAMPSSListNode) { printf("SList_RemoveWithOutFree: list corrupted: head's previous pointer is NULL but node is not the first element in the list\n"); return AMPS_ERROR_FAILURE; } *r_ppoAMPSSListHead = r_poAMPSSListNode->poAMPSSListNext; if(NULL != *r_ppoAMPSSListHead) // Is the last element in the list deleted? { (*r_ppoAMPSSListHead)->poAMPSSListPrev = NULL; } } else { r_poAMPSSListNode->poAMPSSListPrev->poAMPSSListNext = r_poAMPSSListNode->poAMPSSListNext; if(NULL != r_poAMPSSListNode->poAMPSSListNext) { r_poAMPSSListNode->poAMPSSListNext->poAMPSSListPrev = r_poAMPSSListNode->poAMPSSListPrev; } } r_poAMPSSListNode->poAMPSSListNext = NULL; r_poAMPSSListNode->poAMPSSListPrev = NULL; return AMPS_SUCCESS; } /***************************************************************** 函數名稱: SList_GetNextNode 功能描述: 取鏈表下一結點 入參:: void* list 原鏈表 出參: t_AMPSSList** r_ppoAMPSSListHead 返回值: void* list *****************************************************************/ void* SList_GetNextNode(void* list) { t_AMPSSList *slist = (t_AMPSSList*)list; if(!slist) { return NULL; } return slist->poAMPSSListNext; } /***************************************************************** 函數名稱: SList_GetNodeData 功能描述: 取指定結點內容 入參:: void* node 原鏈表 出參: NA 返回值: void* 結點內容 *****************************************************************/ void* SList_GetNodeData(void* node) { t_AMPSSList *slist = (t_AMPSSList*)node; if(!slist) { return NULL; } return slist->pvData; }