结构体的链表操作,创建增加删除查询

 

/***************************** Include Files *********************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "sp_list.h"

/************************** Constant Definitions *****************************/

/**************************** Type Definitions *******************************/
typedef struct tag_STRU_SP_LIST_ELEMENT
{
    void *pvUserElement;
    struct tag_STRU_SP_LIST_ELEMENT *pstruNext;
}STRU_SP_LIST_ELEMENT;

typedef struct tag_STRU_SP_LIST
{
    u32 u32ElementSize;
    u32 u32ElementNum;
    PFUNC_CONSTRUCTOR pFuncConstructor;
    PFUNC_DESTRUCTOR pFuncDestructor;
    STRU_SP_LIST_ELEMENT *pstruListHead;
    STRU_SP_LIST_ELEMENT *pstruListTail;
}STRU_SP_LIST;


/***************** Macros (Inline Functions) Definitions *********************/

/************************** Function Prototypes ******************************/
extern STRU_SP_LIST_ELEMENT * SP_LIST_CreateNewElement(PLIST pList, void *pvUserElement);
extern void SP_LIST_DestroyElement(PLIST pList, STRU_SP_LIST_ELEMENT * pstruListElement);
extern STRU_SP_LIST_ELEMENT * SP_LIST_GetListElementAt(PLIST pList, u32 u32Pos);
/************************** Variable Definitions *****************************/

/************************** Function Definitions *****************************/
PLIST SP_LIST_Create(u32 u32ElementSize, PFUNC_CONSTRUCTOR pFuncConstructor, PFUNC_DESTRUCTOR pFuncDestructor)
{
    PLIST pNewList = (PLIST)malloc(sizeof(STRU_SP_LIST));
    if(C_SYS_NULL != pNewList)
    {
        pNewList->u32ElementSize = u32ElementSize;
        pNewList->u32ElementNum = 0;
        pNewList->pstruListHead = C_SYS_NULL;
        pNewList->pstruListTail = C_SYS_NULL;
        if (C_SYS_NULL == pFuncConstructor)
        {
            pNewList->pFuncConstructor = SP_LIB_DefaultConstructor;
        }
        else
        {
            pNewList->pFuncConstructor = pFuncConstructor;
        }

        if (C_SYS_NULL == pFuncDestructor)
        {
            pNewList->pFuncDestructor = SP_LIB_DefaultDestructor;
        }
        else
        {
            pNewList->pFuncDestructor = pFuncDestructor;
        }

    }
    return pNewList;
}


void SP_LIST_Destroy(PLIST pList)
{
    if(C_SYS_NULL != pList)
    {
        while (!SP_LIST_IsEmpty(pList))
        {
            SP_LIST_Remove(pList, 0);
        }
        free(pList);
    }
}
u32 SP_LIST_Append(PLIST pList, void *pvNewElement)
{
    STRU_SP_LIST_ELEMENT *pstruNewListElement = C_SYS_NULL;
    u32 u32NewElementPos = 0;

    if ((C_SYS_NULL == pList) || (C_SYS_NULL == pvNewElement))
    {
        return C_SP_LIB_INVALID_POSITION;
    }

    pstruNewListElement = SP_LIST_CreateNewElement(pList, pvNewElement);

    if (C_SYS_NULL != pstruNewListElement)
    {
        if(SP_LIST_IsEmpty(pList))
        {
            pList->pstruListHead = pstruNewListElement;
            pList->pstruListTail = pstruNewListElement;
        }
        else
        {
            pList->pstruListTail->pstruNext = pstruNewListElement;
            pList->pstruListTail = pstruNewListElement;
        }
        u32NewElementPos = pList->u32ElementNum;
        pList->u32ElementNum++;
        return u32NewElementPos;
    }
    else
    {
        return C_SP_LIB_INVALID_POSITION;
    }
}


STRU_SP_LIST_ELEMENT * SP_LIST_CreateNewElement(PLIST pList, void *pvUserElement)
{
    STRU_SP_LIST_ELEMENT *pstruNewListElement = C_SYS_NULL;

    if ((C_SYS_NULL == pList) || (C_SYS_NULL == pvUserElement))
    {
        return C_SYS_NULL;
    }

    pstruNewListElement = (STRU_SP_LIST_ELEMENT *)malloc(sizeof(STRU_SP_LIST_ELEMENT));

    if(C_SYS_NULL != pstruNewListElement)
    {
        pstruNewListElement->pstruNext = C_SYS_NULL;
        pstruNewListElement->pvUserElement = (*pList->pFuncConstructor)(pvUserElement, pList->u32ElementSize);
    }

    return pstruNewListElement;
}


u32 SP_LIST_Size(PLIST pList)
{
    if (C_SYS_NULL != pList)
    {        
        return pList->u32ElementNum;
    }
    else
    {
        return C_SP_LIB_INVALID_POSITION;
    }
}

SYS_BOOL SP_LIST_IsEmpty(PLIST pList)
{
    if (C_SYS_NULL != pList)
    {
        if (0 == pList->u32ElementNum)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        return TRUE;
    }
}


SYS_BOOL SP_LIST_Remove(PLIST pList, u32 u32Pos)
{
    STRU_SP_LIST_ELEMENT *pstruListElement = C_SYS_NULL;
    STRU_SP_LIST_ELEMENT *pstruFrontElement = C_SYS_NULL;
    STRU_SP_LIST_ELEMENT *pstruNextElement = C_SYS_NULL;

    if (C_SYS_NULL == pList)
    {
        return FALSE;
    }
    if (0 == pList->u32ElementNum)
    {
        return FALSE;
    }
    if (u32Pos >= pList->u32ElementNum)
    {
        return FALSE;
    }
    if (1 != pList->u32ElementNum)
    {
        pstruListElement = SP_LIST_GetListElementAt(pList, u32Pos);

        if (0 == u32Pos)
        {
            pstruNextElement = SP_LIST_GetListElementAt(pList, u32Pos + 1);
            SP_LIST_DestroyElement(pList, pList->pstruListHead);
            pList->pstruListHead = pstruNextElement;
 
        }
        else if (u32Pos == (pList->u32ElementNum - 1))
        {
            pstruFrontElement = SP_LIST_GetListElementAt(pList, u32Pos - 1);
            SP_LIST_DestroyElement(pList, pList->pstruListTail);
            pstruFrontElement->pstruNext = C_SYS_NULL;
            pList->pstruListTail = pstruFrontElement;
        }
        else
        {
            pstruFrontElement = SP_LIST_GetListElementAt(pList, u32Pos - 1);
            pstruNextElement = SP_LIST_GetListElementAt(pList, u32Pos + 1);

            SP_LIST_DestroyElement(pList, pstruListElement);
            pstruFrontElement->pstruNext = pstruNextElement;

        }
        pList->u32ElementNum --;

    }
    else
    {
        /*只有一个元素,就把链表头和尾都清空*/
        SP_LIST_DestroyElement(pList, pList->pstruListHead);
        pList->pstruListHead = C_SYS_NULL;
        pList->pstruListTail = C_SYS_NULL;
        pList->u32ElementNum = 0;
    }
    

    return TRUE;
}


void * SP_LIST_GetAt(PLIST pList, u32 u32Pos)
{
    STRU_SP_LIST_ELEMENT *pstruListElement = C_SYS_NULL;
   
    if (C_SYS_NULL == pList)
    {
        return C_SYS_NULL;
    }
    if (u32Pos >= pList->u32ElementNum)
    {
        return C_SYS_NULL;
    }

    pstruListElement = SP_LIST_GetListElementAt(pList, u32Pos);

    if (C_SYS_NULL != pstruListElement)
    {
        return pstruListElement->pvUserElement;
    }
    else
    {
        return C_SYS_NULL;
    }
}


STRU_SP_LIST_ELEMENT * SP_LIST_GetListElementAt(PLIST pList, u32 u32Pos)
{
    STRU_SP_LIST_ELEMENT *pstruListElement = C_SYS_NULL;
    u32 u32Count = 0;
    if (C_SYS_NULL == pList)
    {
        return C_SYS_NULL;
    }
    if (u32Pos >= pList->u32ElementNum)
    {
        return C_SYS_NULL;
    }
    
    pstruListElement = pList->pstruListHead;

    while (u32Count < u32Pos)
    {
        pstruListElement = pstruListElement->pstruNext;
        u32Count++;
    }

    return pstruListElement;

}



void SP_LIST_DestroyElement(PLIST pList, STRU_SP_LIST_ELEMENT * pstruListElement)
{
    if ((C_SYS_NULL == pList) || (C_SYS_NULL == pstruListElement))
    {
        return;
    }

    (*pList->pFuncDestructor)(pstruListElement->pvUserElement);
    pstruListElement->pvUserElement = C_SYS_NULL;
    free(pstruListElement);
}


u32 SP_LIST_Find(PLIST pList, PFUNC_COMPARE pFuncCompare, const void *pcvElementOutsight)
{
    STRU_SP_LIST_ELEMENT *pstruListElement = C_SYS_NULL;
    u32 u32Postion = 0;
    if((C_SYS_NULL == pList) 
        || (C_SYS_NULL == pFuncCompare)
        || (C_SYS_NULL == pcvElementOutsight))
    {
        return C_SP_LIB_INVALID_POSITION;
    }

    pstruListElement = pList->pstruListHead;
    while(C_SYS_NULL != pstruListElement)
    {
        if(0 == (*pFuncCompare)(pcvElementOutsight, pstruListElement->pvUserElement, pList->u32ElementSize))
        {
            return u32Postion;
        }
        u32Postion++;
        pstruListElement = pstruListElement->pstruNext;
    }
    return C_SP_LIB_INVALID_POSITION;
}


SYS_BOOL SP_LIST_Insert(PLIST pList, u32 u32Pos, void *pvElement)
{
    STRU_SP_LIST_ELEMENT *pstruNewListElement = C_SYS_NULL;
    STRU_SP_LIST_ELEMENT *pstruFrontElement = C_SYS_NULL;
    STRU_SP_LIST_ELEMENT *pstruListElement = C_SYS_NULL;

    if (C_SYS_NULL == pList)
    {
        return FALSE;
    }
    if (u32Pos >= pList->u32ElementNum)
    {
        return FALSE;
    }

    if (0 == u32Pos)
    {
        pstruNewListElement = SP_LIST_CreateNewElement(pList, pvElement);
        if (C_SYS_NULL != pstruNewListElement)
        {
            pstruNewListElement->pstruNext = pList->pstruListHead;
            pList->pstruListHead = pstruNewListElement;
            pList->u32ElementNum++;
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        pstruFrontElement = SP_LIST_GetListElementAt(pList, u32Pos-1);
        pstruListElement = SP_LIST_GetListElementAt(pList, u32Pos);
        pstruNewListElement = SP_LIST_CreateNewElement(pList, pvElement);

        if ((C_SYS_NULL != pstruNewListElement)
            && (C_SYS_NULL != pstruFrontElement)
            && (C_SYS_NULL != pstruListElement))
        {
            pstruFrontElement->pstruNext = pstruNewListElement;
            pstruNewListElement->pstruNext = pstruListElement;
            pList->u32ElementNum++;
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
}


void SP_LIST_Traversal(PLIST pList, PFUNC_TRAVERSAL pFuncTraversal, void *pvParam)
{
    STRU_SP_LIST_ELEMENT *pstruListElement = C_SYS_NULL;
    if (C_SYS_NULL != pList)
    {
        pstruListElement = pList->pstruListHead;
        while(C_SYS_NULL != pstruListElement)
        {
            (*pFuncTraversal)(pstruListElement->pvUserElement, pvParam);
            pstruListElement = pstruListElement->pstruNext;
        }
    }

}

void * SP_LIST_GetNext(PLIST pList, u32 u32Pos)
{
    return SP_LIST_GetAt(pList, u32Pos + 1);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值