C语言动态数组的实现

这段代码用于增加对动态数组的了解, 功能包括:动态数组的初始化、增删改查、逆序操作等。

使用到的知识点有: 指针、内存空间的申请和释放、typedef的使用以及回调函数的使用。

#include
#include
#include

typedef struct DynamicArray
{
    void **m_pAddress;
    int m_Capacity;
    int m_Size; 
}DyARR;

#ifdef __cplusplus
extern "C"{
#endif

    void  *Init_DynamicArray(int m_Capacity);

    void  Insert_DynamicArray(void *darr, int pos, void *data);

    void  Foreach_DynamicArray(void *darr, void(*_callback)(void *));

    int   GetSize_DynamicArray(void *darr);

    int   GetCapacity_DynamicArray(void *darr);

    void  RemoveByPos_DynamicArray(void *darr, int pos);

    void  RemoveByValue_DynamicArray(void *darr, void *data, int(*compare)(void*, void *));

    void  Reverse_DynamicArray(void *darr);

    void  Destroy_DynamicArray(void *darr);

#ifdef __cplusplus
}
#endif
#include"dynamicArray.h"

void *Init_DynamicArray(int capacity)
{
    if (capacity <= 0)
    {
        return NULL;
    }

    DyARR *arr = (DyARR *)malloc(sizeof(DyARR));
    if (NULL == arr)
    {
        return NULL;
    }

    arr->m_Capacity = capacity;
    arr->m_pAddress = (void **)malloc(sizeof(void *) * arr->m_Capacity);
    arr->m_Size = 0;

    return arr;
}


void Insert_DynamicArray(void *darr, int pos, void *data)
{
    if (NULL == darr || NULL == data)
    {
        return;
    }
    
    DyARR *arr = (DyARR *)darr;
    if (pos < 0 || pos > arr->m_Size)
    {
        pos = arr->m_Size;
    }

    if (arr->m_Size == arr->m_Capacity)
    {
        int newcapacity = arr->m_Capacity * 2;
        void **newspace = (void **)malloc(sizeof(void *)* newcapacity);
        if(NULL == newspace)
        {
            return;
        }
        memcpy(newspace, arr->m_pAddress, sizeof(void *)* arr->m_Capacity);
        
        if(arr->m_pAddress)
        {
            free(arr->m_pAddress);
        }

        arr->m_pAddress = newspace;
        arr->m_Capacity = newcapacity;
    }

    int i;
    for (i = arr->m_Size - 1; i >= pos; --i)
    {
        arr->m_pAddress[i + 1] = arr->m_pAddress[i];
    }

    arr->m_pAddress[pos] = data;
    arr->m_Size++;
}

void Foreach_DynamicArray(void *darr, void(*_callback)(void *))
{
    if (NULL == darr || NULL == _callback)
    {
        return;
    }

    DyARR *arr = (DyARR *)darr;
    int i = 0;
    for (i = 0; i < arr->m_Size; ++i)
    {
        _callback(arr->m_pAddress[i]);
    }
}

int GetSize_DynamicArray(void *darr)
{
    if(NULL == darr)
    {
        return 0;
    }
    
    DyARR *arr = (DyARR *)darr;
    return arr->m_Size;
}

int GetCapacity_DynamicArray(void *darr)
{
    if(NULL == darr)
    {
        return 0;
    }
    
    DyARR *arr = (DyARR *)darr;
    return arr->m_Capacity;
}

void RemoveByPos_DynamicArray(void *darr, int pos)
{
    if (NULL == darr)
    {
        return;
    }
    
    DyARR *arr = (DyARR *)darr;
    
    if (pos < 0 || pos > arr->m_Size - 1)
    {
        return;
    }

    int i;
    for (i = pos; i < arr->m_Size - 1; ++i)
    {
        arr->m_pAddress[i] = arr->m_pAddress[i + 1];
    }

    arr->m_Size--;
}

void RemoveByValue_DynamicArray(void *darr, void *data, int(*compare)(void*, void *))
{
    if (NULL == darr || NULL == data || NULL == compare)
    {
        return;
    }
    DyARR *arr = (DyARR *)darr;

    int i = 0;
    for (i = 0; i < arr->m_Size; ++i)
    {
        if (compare(arr->m_pAddress[i], data))
        {
            RemoveByPos_DynamicArray(arr, i);
            break;
        }
    }
}

void Reverse_DynamicArray(void *darr)
{
    if(NULL == darr)
    {
        return;
    }
    
    DyARR *arr = (DyARR *)darr;
    int i;
    void **tmp;
    for(i = 0; i < arr->m_Size/2; i++)
    {
        tmp = arr->m_pAddress[i];
        arr->m_pAddress[i] = arr->m_pAddress[arr->m_Size - 1 - i];
        arr->m_pAddress[arr->m_Size - 1 - i] = tmp;
    }
} 

void Destroy_DynamicArray(void *darr)
{
    if (NULL == darr)
    {
        return;
    }
    DyARR *arr = (DyARR *)darr;
    if (arr->m_pAddress != NULL)
    {
        free(arr->m_pAddress);
        arr->m_pAddress = NULL;
    }

    free(arr);
    arr = NULL;
}
#include
#include
#include
#include"dynamicArray.h"

typedef struct Employee
{
    char  name[64];
    int   age;
    int   jobNumber;
}EMP;

void myPrint(void *data)
{
    if(data)
    {
        EMP *ep = (EMP *)data;
        printf("Name:%s, Age:%d, jobNumber:%d\n", ep->name, ep->age, ep->jobNumber);
    }
}

int MyCompare(void *d1,void *d2)
{
    if(NULL == d1 || NULL == d2)
    {
        return -1;
    }

    EMP *ep1 = (EMP *)d1;
    EMP *ep2 = (EMP *)d2;

    return strcmp(ep1->name, ep2->name) == 0 && ep1->age == ep2->age && ep1->jobNumber == ep2->jobNumber;
}

void test()
{
    EMP *ep = (EMP *)malloc(sizeof(EMP) * 6);
    if(NULL == ep)
    {
        return;
    }

    int i;
    for(i = 0; i < 5; i++)
    {
        sprintf(ep[i].name, "name_%c", i+65);
        ep[i].age = 21 + i;
        ep[i].jobNumber = 2160 + i;
    }
    
    printf("##init dynamic array: \n");
    void *pArr = Init_DynamicArray(3);
   
    printf("\n##insert 3 elememt into arr: \n");
    Insert_DynamicArray(pArr, 0, &ep[0]);    
    Insert_DynamicArray(pArr, 0, &ep[1]);    
    Insert_DynamicArray(pArr, 1, &ep[2]);    
    
    printf("size = %d, capacity=%d\n", GetSize_DynamicArray(pArr), GetCapacity_DynamicArray(pArr));
    
    
    printf("\n##insert 2 elememt into arr: \n");
    Insert_DynamicArray(pArr, 1, &ep[3]);    
    Insert_DynamicArray(pArr, 1, &ep[4]);    
    printf("size = %d, capacity=%d\n", GetSize_DynamicArray(pArr), GetCapacity_DynamicArray(pArr));

    printf("\n##foreach dynamic array:\n");
    Foreach_DynamicArray(pArr, myPrint);

    printf("\n##remove the 4th elememt: \n");
    RemoveByPos_DynamicArray(pArr,4);
    Foreach_DynamicArray(pArr, myPrint);

    printf("\n##remove the data : name_a, 21, 2160:\n");
    RemoveByValue_DynamicArray(pArr, &ep[0], MyCompare);
    Foreach_DynamicArray(pArr, myPrint);

    printf("\n##reverse dynamic array:\n");
    Reverse_DynamicArray(pArr);
    Foreach_DynamicArray(pArr, myPrint);


    printf("\n##Destroy dynamic Array: \n");
    Destroy_DynamicArray(pArr);
}

int main(void)
{
    test();

    return EXIT_SUCCESS;
}

 

 

 

 

 




 

 

 

 

你可能感兴趣的:(C语言,c语言,数据结构)