dynamic.h文件:
#pragma once
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct dynamicArray
{
void** paddr;//维护真实在堆区创建的数组的指针
int m_capamity;//数组容量
int m_size;//数组长度
};
struct dynamicArray* init_DynamicArray(int capamity);
void insert_dynamicarray(struct dynamicArray* array, int pos, void* data);
//遍历数组
void foreachDynamicArray(struct dynamicArray* array, void(*func)(void*));
//按位置删除数组
void detePosDynamicArray(struct dynamicArray* array, int pos);
//按值删除数据
void removeByValue(struct dynamicArray* array, void* data, int (*myCompare)(void*, void*));
//销毁数组
void destory_DynamicArray(struct dynamicArray* array);
dynamic.c文件:
#include "dynamic.h"
struct dynamicArray* init_DynamicArray(int capamity) {
if (capamity == NULL)
{
return NULL;
}
struct dynamicArray* array = malloc(sizeof(struct dynamicArray));
if (array == NULL)
{
return NULL;
}
array->m_capamity = capamity;
array->m_size = 0;
array->paddr = malloc(sizeof(void*) * capamity);
return array;
}
//插入
void insert_dynamicarray(struct dynamicArray* array, int pos, void* data) {
if (array == NULL)
{
return;
}
if (data == NULL)
{
return;
}
//如果插入数据位置无效插到末尾
if (pos < 0 || pos > array->m_size)
{
pos = array->m_size;
}
//如果数组已经满了,动态扩容
if (array->m_size == array->m_capamity)
{
//计算新空间大小
int newCapacity = array->m_capamity * 2;
//开辟新的数组空间
void** newSpace = malloc(sizeof(void*) * newCapacity);
//将旧的数据拷贝到新的数组中
memcpy(newSpace, array->paddr, sizeof(void*) * array->m_capamity);
//释放旧空间
free(array->paddr);
//更新新的空间
array->paddr = newSpace;
//更新新的容量
array->m_capamity = newCapacity;
}
//插入新元素,pos后元素一一向后移动
for (int i = array->m_size - 1; i >= pos; i--)
{
array->paddr[i + 1] = array->paddr[i];
}
//将新元素插入到指定位置
array->paddr[pos] = data;
//更新大小
array->m_size++;
}
//遍历数组
void foreachDynamicArray(struct dynamicArray* array, void(*func)(void*)) {
if (array == NULL)
{
return;
}
if (func == NULL)
{
return;
}
for (int i = 0; i < array->m_size; i++)
{
func(array->paddr[i]);
}
}
//按位置删除数组
void detePosDynamicArray(struct dynamicArray* array, int pos) {
if (array == NULL)
{
return;
}
if (pos < 0 || pos > array->m_size - 1)
{
return;
}
for (int i = pos; i <= array->m_size - 1; i++)
{
array->paddr[i] = array->paddr[i + 1];
}
array->m_size--;
}
//按值删除数据
void removeByValue(struct dynamicArray* array, void* data, int (*myCompare)(void*, void*)) {
if (array == NULL || data == NULL)
{
return;
}
for (int i = 0; i < array->m_size; i++)
{
if (myCompare(array->paddr[i], data)) {
detePosDynamicArray(array, i);
break;
}
}
}
//销毁数组
void destory_DynamicArray(struct dynamicArray* array) {
if (array == NULL)
{
return;
}
if (array->paddr != NULL)
{
free(array->paddr);
array->paddr = NULL;
}
free(array);
array = NULL;
}
main.c文件:
#include "dynamic.h"
//测试
struct Person
{
char name[64];
int age;
};
//回调函数
void myPrint(void* data) {
struct Person* p = data;
printf("姓名:%s\t年龄:%d\n", p->name, p->age);
}
int myCompare(void* d1, void* d2) {
struct Person* p1 = d1;
struct Person* p2 = d2;
return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age;
}
int main() {
//初始化数组
struct dynamicArray *array = init_DynamicArray(5);
printf("数组容量:%d\t数组大小:%d\n", array->m_capamity, array->m_size);
struct Person p1 = { "a",3 };
struct Person p2 = { "b",4 };
struct Person p3 = { "c",235 };
struct Person p4 = { "d",6 };
struct Person p5 = { "e",34 };
struct Person p6 = { "f",7 };
insert_dynamicarray(array, 0, &p1);
insert_dynamicarray(array, 4, &p2);
insert_dynamicarray(array, 8, &p3);
insert_dynamicarray(array, -1, &p4);
insert_dynamicarray(array, 0, &p5);
insert_dynamicarray(array, 0, &p6);
printf("数组容量:%d\t数组大小:%d\n", array->m_capamity, array->m_size);
//f,e,a,b,c,d
foreachDynamicArray(array, myPrint);
printf("--------------------------\n");
//测试按位置删除
detePosDynamicArray(array, 2);
foreachDynamicArray(array, myPrint);//f e b c d
printf("--------------------------\n");
//测试按值删除
struct Person p = { "f",7 };
removeByValue(array, &p, myCompare);
foreachDynamicArray(array, myPrint);//f e b c d
//销毁数组
destory_DynamicArray(array);
array = NULL;
system("pause");
return 0;
}