Initialization of dynamic array
#include <iostream>
using namespace std;
struct dynamicArray{
void **pAddr;
int m_capacity;
int m_size;
}
struct dynamicArray *initdynamicArray(int capacity){
if(capacity<=0) {
return 0;
}
struct dynamicArray *array=malloc(sizeof(struct dynamicArray))
if(array=NULL){
return 0;
}
array->pAddr=malloc(sizeof(void*)*capacity);
array->m_capacity=capacity;
array->size=0;
return array;
}
Function series
```void *insert(struct dynamicArray *array,int pos,void *data){
if(array==NULL||data==NULL){
return 0;
}
if(pos<=0||pos>array->m_size){
pos=array->m_size;
}
if(array->m_size==array->capacity){
int newCapacity=array->capacity;
void **newSpace=malloc(sizeof(void*)*newCapacity);
memcpy(newSpace,array->pAddr,sizeof(void *)*array->capacity);
free(array->pAddr);
array->pAddr=newSpace;
}
for(int i=array->m_size;i<=pos;i--){
array->pAddr[i+1]=array->pAddr[i];
}
array->pAddr[pos]=data;
void foreach_Dynamicarray(struct dynamicArray *array,void(myPrint)(void*)){
for(int i=0;i<array->m_size;i++){
myPrint(array->pAddr[i]);
}
}
void myPrintPerson(void *data){
struct person *p=data;
cout<<p->blabla<p->blabuble<endl;
}
void remove(struct dynamicArray *array,int pos){
if(array==NULL||pos<=0||pos>=array->){
return 0;
}
for(int i=array->m_size;i>=pos;i--){
array->pAddr[i]=array->pAddr[i+1];
}
}
void destroy(struct dynamicArray *array){
if(array=NULL){
return 0;
}
if(array->pAddr!=NULL){
free(array->pAddr);
array->pAddr=NULL;
}
free(array);
array=NULL;
}
Read More:
- C++: Implementation of multi-channel IO transfer with select
- The principle and return value of get() function in C language
- Implementation of retrial mechanism in Java
- The thread implementation of timer in Java
- JAVE: LeetCode(189) Rotate Array
- Java callback function implementation case
- Java implementation of inputsteam to Base64
- Exception information: jsonmappingexception: out of start_ ARRAY token
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- How to Solve Error occurred during initialization of boot layer
- [Solved] The Bean Validation API is on the classpath but no implementation could be found
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- [Solved] Initialization of anonymous inner class member variable causes java.lang.stackoverflowerror
- [Solved] Java.lang.BootstrapMethodError: call site initialization exception
- [Solved] Error occurred during initialization of VM Could not reserve enough space for object heap
- [Solved] Error occurred during initialization of VM javalangNoClassDefFoundError javalangObject
- [Solved] Error occurred during initialization of VM Could not reserve enough space for 1572864KB object heap
- [Solved] Jxls error: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath
- [Solved] springboot Project Run Error: HikariPool-1 – Exception during pool initialization.
- Java: How to use itext to export PDF text absolute positioning (implementation method)