源码
DynamicArray.h
//动态数组
//特征:1.元素数据类型不定
//特征:2.数组元素个数不定
#ifndef _DynamicArray_H
#define _DynamicArray_H
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
//动态数组结构
struct DynamicArry
{
void** p_array; //动态数组本体
int capacity;
int have_amount;
};
//创建动态数组
struct DynamicArry* creat_DynamicArry(int capacity);
//按位置往动态数组插入数据指针
void insert_pointer(struct DynamicArry* DynamicArry_p, int position, void* pointer_insert);
//遍历动态数组
void traverse_DynamicArry(struct DynamicArry* DynamicArry_p, void(*callback)(void*));
//按位置删除动态数组内数据指针
void dellet_pointerOFDynamicArry(struct DynamicArry* DynamicArry_p, int position);
//按值删除动态数组内数据指针
void DelletSpecificPointerOFDynamicArry(struct DynamicArry* DynamicArry_p, void* data, int (*callback)(void*, void*));
//销毁动态数组
void DestroyDynamicArray(struct DynamicArry* DynamicArry_p);
#endif
DynamicArray.c
#include "DynamicArray.h"
struct DynamicArry* creat_DynamicArry(int capacity)
{
if (capacity <= 0)
{
return(NULL);
}
struct DynamicArry* DynamicArry_p = (struct DynamicArry*)malloc(sizeof(struct DynamicArry));
if (DynamicArry_p == NULL)
{
return(NULL);
}
DynamicArry_p->capacity = capacity;
DynamicArry_p->have_amount = 0;
DynamicArry_p->p_array = (void**)malloc(sizeof(void*) * (sizeof(DynamicArry_p->capacity)));
return(DynamicArry_p);
}
void insert_pointer(struct DynamicArry* DynamicArry_p, int position, void* pointer_insert)
{
if (DynamicArry_p == NULL)
{
return;
}
if (pointer_insert == NULL)
{
return;
}
if (position<0 || position>(DynamicArry_p->have_amount))
{
position = DynamicArry_p->have_amount;
}
if (DynamicArry_p->have_amount >= DynamicArry_p->capacity)
{
int new_capacity = DynamicArry_p->capacity * 2;
void(**newspace) = (void**)malloc(sizeof(new_capacity));
memcpy(newspace, DynamicArry_p->p_array, sizeof(void*) * (sizeof(DynamicArry_p->capacity)));
free(DynamicArry_p->p_array);
DynamicArry_p->p_array = newspace;
DynamicArry_p->capacity = new_capacity;
for (int i = DynamicArry_p->have_amount; i >= position; i--)
{
DynamicArry_p->p_array[i + 1] = DynamicArry_p->p_array[i];
}
}
DynamicArry_p->p_array[position] = pointer_insert;
DynamicArry_p->have_amount++;
}
//
void traverse_DynamicArry(struct DynamicArry* DynamicArry_p, void(*callback)(void*))
{
if (DynamicArry_p == NULL)
{
return;
}
if (callback == NULL)
{
return;
}
for (int i = 0; i < DynamicArry_p->have_amount; i++)
{
callback(DynamicArry_p->p_array[i]);
}
}
void dellet_pointerOFDynamicArry(struct DynamicArry* DynamicArry_p, int position)
{
if (DynamicArry_p == NULL)
{
return;
}
if ((position < 0) || (position >= (DynamicArry_p->have_amount)))
{
return;
}
for (int i = 0; i < (DynamicArry_p->have_amount - position - 1); i++)
{
DynamicArry_p->p_array[position + i] = DynamicArry_p->p_array[position + i + 1];
}
DynamicArry_p->have_amount--;
}
void DelletSpecificPointerOFDynamicArry(struct DynamicArry* DynamicArry_p, void* data, int (*callback)(void*, void*))
{
if (DynamicArry_p == NULL)
{
return;
}
if (data == NULL)
{
return;
}
for (int i = 0; i < DynamicArry_p->have_amount; i++)
{
if (1 == callback(data, DynamicArry_p->p_array[i]))
{
dellet_pointerOFDynamicArry(DynamicArry_p, i);
break;
}
}
}
void DestroyDynamicArray(struct DynamicArry* DynamicArry_p)
{
if (DynamicArry_p == NULL)
{
return;
}
if ((DynamicArry_p->p_array) != NULL)
{
free(DynamicArry_p->p_array);
DynamicArry_p->p_array = NULL;
}
free(DynamicArry_p);
DynamicArry_p = NULL;
}
main.c(测试用)
#include"DynamicArray.h"
struct person
{
char name[64];
int age;
};
void PrintCallback(void* data)
{
puts("enter my_callback\n");
struct person* p;
p = (struct person*)data;
printf("name:%s,age:%d\n", p->name, p->age);
puts("leaving my callback\n");
}
int DeletePointerCallback(void* data1, void* data2)
{
struct person* pp1;
pp1 = (struct person*)data1;
struct person* pp2;
pp2 = (struct person*)data2;
if ((strcmp(pp1->name, pp2->name) == 0) && (pp1->age == pp2->age))
{
return(1);
}
else
{
return(0);
}
}
void test0(void)
{
struct DynamicArry* DynamicArry_p;
DynamicArry_p = creat_DynamicArry(5);
printf("capacity of DynamicArry:%d\n", DynamicArry_p->capacity);
struct person p1 = { "aaa",20 };
struct person p2 = { "bbb",19 };
struct person p3 = { "ccc",18 };
struct person p4 = { "ddd",17 };
struct person p5 = { "eee",16 };
struct person p6 = { "fff",15 };
insert_pointer(DynamicArry_p, 0, &p1);
insert_pointer(DynamicArry_p, 1, &p2);
insert_pointer(DynamicArry_p, 2, &p3);
insert_pointer(DynamicArry_p, 3, &p4);
insert_pointer(DynamicArry_p, 4, &p5);
traverse_DynamicArry(DynamicArry_p, PrintCallback);
puts("-------------");
dellet_pointerOFDynamicArry(DynamicArry_p, 2);
traverse_DynamicArry(DynamicArry_p, PrintCallback);
puts("------------");
struct person pdelete = { "eee",16 };
DelletSpecificPointerOFDynamicArry(DynamicArry_p,&pdelete, DeletePointerCallback);
traverse_DynamicArry(DynamicArry_p, PrintCallback);
}
int main(void)
{
test0();
return(0);
}