首页 > 代码库 > 数据结构 动态数组实现
数据结构 动态数组实现
头文件:
#pragma once #include<stdlib.h> #include<string.h> struct DynamicArray{ void **data; //数据空间首地址 int capacity;//容量,当前数据空间最大能够容纳多少个元素 int size; //大小,当前数据空间有多少元素 }; #ifdef __cplusplus extern "C"{ #endif typedef int(compare)(void*, void*); //初始化函数 void *Init_DynamicArray(); //指定位置插入 int Insert_DynamicArray(void *arr, int pos, void *data); //头部插入 int PushFront_DynamicArray(void *arr, void *data); //尾部插入 int PushBack_DynamicArray(void *arr, void *data); //指定位置删除 int RemoveByPos_DynamicArray(void *arr, int pos); //根据值来删除 int RemoveByVal_DynamicArray(void *arr, void *data, compare *mycompare); //头部删除 int PopFront_DynamicArray(void *arr); //尾部删除 int PopBack_DynamicArray(void *arr); //获得数组大小 int Size_DynamicArray(void *arr); //获得容量 int Capacity_DynamicArray(void *arr); //打印函数 void Foreach_DynamicArray(void *arr,void(*foreach)(void *)); //销毁数组 int Destroy_DynamicArray(void *arr); #ifdef __cplusplus } #endif
头文件实现:
#include"DynamicArray.h" //初始化函数 void *Init_DynamicArray(){ struct DynamicArray *arr = malloc(sizeof(struct DynamicArray)); if (NULL == arr){ return NULL; } arr->capacity = 10; //默认数组创建,容量是10 arr->data = http://www.mamicode.com/malloc(sizeof(void *)* arr->capacity); arr->size = 0; return arr; } //指定位置插入 int Insert_DynamicArray(void *arr, int pos, void *data){ if (NULL == arr){ return -1; } if (NULL == data){ return -2; } struct DynamicArray *myarr = (struct DynamicArray *)arr; if (pos < 0 || pos > myarr->size){ pos = myarr->size; } //判断空间是否足够 if (myarr->size == myarr->capacity){ //根据增长策略,计算新空间大小 int newcapacity = myarr->size * 2; //开辟新空间 void **newspace = (void **)malloc(sizeof(void *)* newcapacity); //将旧空间的数据拷贝到新空间 memcpy(newspace, myarr->data, sizeof(void *)* myarr->capacity); //释放旧空间内存 free(myarr->data); //更新数组核心数据 myarr->data =http://www.mamicode.com/ newspace; myarr->capacity = newcapacity; } //将元素插入到新空间 for (int i = myarr->size - 1; i >= pos;i --){ myarr->data[i + 1] = myarr->data[i]; } //元素插入到pos位置 myarr->data[pos] = data; //维护size大小 myarr->size++; return 0; } //头部插入 int PushFront_DynamicArray(void *arr, void *data){ return Insert_DynamicArray(arr, 0 , data); } //尾部插入 int PushBack_DynamicArray(void *arr, void *data){ struct DynamicArray *myarr = (struct DynamicArray *)arr; return Insert_DynamicArray(arr, myarr->size, data); } //指定位置删除 int RemoveByPos_DynamicArray(void *arr, int pos){ if (NULL == arr){ return -1; } struct DynamicArray *myarr = (struct DynamicArray *)arr; if (myarr->size == 0){ return 0; } if (pos < 0 || pos > myarr->size - 1){ return 0; } for (int i = pos; i < myarr->size - 1; i ++){ myarr->data[i] = myarr->data[i + 1]; } myarr->size--; return 0; } //根据值来删除 int RemoveByVal_DynamicArray(void *arr, void *data, compare *mycompare){ if (NULL == arr){ return -1; } if (NULL == data){ return -2; } if (NULL == mycompare){ return -3; } struct DynamicArray *myarr = (struct DynamicArray *)arr; if (myarr->size == 0){ return 0; } for (int i = 0; i < myarr->size - 1;i ++){ if (mycompare(myarr->data[i], data)){ //说明找到了 RemoveByPos_DynamicArray(arr, i); break; } } return 0; } //头部删除 int PopFront_DynamicArray(void *arr){ return RemoveByPos_DynamicArray(arr, 0); } //尾部删除 int PopBack_DynamicArray(void *arr){ struct DynamicArray *myarr = (struct DynamicArray *)arr; return RemoveByPos_DynamicArray(arr, myarr->size - 1); } //获得数组大小 int Size_DynamicArray(void *arr){ struct DynamicArray *myarr = (struct DynamicArray *)arr; return myarr->size; } //获得容量 int Capacity_DynamicArray(void *arr){ struct DynamicArray *myarr = (struct DynamicArray *)arr; return myarr->capacity; } //打印函数 void Foreach_DynamicArray(void *arr, void(*foreach)(void *)){ if (NULL == arr){ return; } if (NULL == foreach){ return; } struct DynamicArray *myarr = (struct DynamicArray *)arr; for (int i = 0; i < myarr->size; i++){ foreach(myarr->data[i]); } } //销毁数组 int Destroy_DynamicArray(void *arr){ if (NULL == arr){ return -1; } struct DynamicArray *myarr = (struct DynamicArray *)arr; if (myarr->data != NULL){ myarr->capacity = 0; myarr->size = 0; free(myarr->data); myarr->data =http://www.mamicode.com/ NULL; } free(myarr); myarr = NULL; return 0; }
测试文件:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> #include"DynamicArray.h" struct Person{ char name[64]; int age; }; void myPrint(void *data){ struct Person *person = (struct Person *)data; printf("Name:%s Age:%d\n",person->name,person->age); } int myComapre(void *d1, void *d2){ struct Person *p1 = (struct Person *)d1; struct Person *p2 = (struct Person *)d2; return (strcmp(p1->name, p2->name) == 0) && p1->age == p2->age; } void test(){ struct Person p1 = { "aaa", 10 }; struct Person p2 = { "bbb", 20 }; struct Person p3 = { "ccc", 30 }; struct Person p4 = { "ddd", 40 }; struct Person p5 = { "eee", 50 }; struct Person p6 = { "fff", 60 }; struct Person p7 = { "ggg", 70 }; struct Person p8 = { "hhh", 80 }; struct Person p9 = { "iii", 90 }; struct Person p10 = { "jjj", 100 }; struct Person p11 = { "kkk", 110 }; //初始化数组 void *myarray = Init_DynamicArray(); //插入元素 Insert_DynamicArray(myarray, 0, &p1); Insert_DynamicArray(myarray, 0, &p2); Insert_DynamicArray(myarray, 0, &p3); Insert_DynamicArray(myarray, 0, &p4); Insert_DynamicArray(myarray, 0, &p5); Insert_DynamicArray(myarray, 100, &p6); printf("Capacity:%d\n", Capacity_DynamicArray(myarray)); PushFront_DynamicArray(myarray, &p7); PushFront_DynamicArray(myarray, &p8); PushFront_DynamicArray(myarray, &p9); PushBack_DynamicArray(myarray, &p10); PushBack_DynamicArray(myarray, &p11); //遍历 //9 8 7 5 4 3 2 1 6 10 11 Foreach_DynamicArray(myarray, myPrint); printf("Capacity:%d\n",Capacity_DynamicArray(myarray)); printf("Size:%d\n", Size_DynamicArray(myarray)); printf("--------头删和尾删之后-------------\n"); //删除 PopFront_DynamicArray(myarray); PopBack_DynamicArray(myarray); Foreach_DynamicArray(myarray, myPrint); printf("--------删除2位置之后-------------\n"); RemoveByPos_DynamicArray(myarray, 2); Foreach_DynamicArray(myarray, myPrint); printf("--------删除age = 10 name = aaa位置之后-------------\n"); struct Person pDel = { "aaa", 10 }; RemoveByVal_DynamicArray(myarray, &pDel, myComapre); Foreach_DynamicArray(myarray, myPrint); //销毁 Destroy_DynamicArray(myarray); } int main(){ test(); system("pause"); return EXIT_SUCCESS; }
数据结构 动态数组实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。