线性表的存储–动态数组
1、基本数据类型
typedef struct DYNAMICARRAY{
int* pAddr;
int size;
int capacity;
}Dynamic_Array;
2、动态数组的初始化
Dynamic_Array* Init_Array(){
Dynamic_Array* myArray = (Dynamic_Array*)malloc(sizeof(Dynamic_Array));
myArray->size = 0;
myArray->capacity = 20;
myArray->pAddr = (int*)malloc(sizeof(int)*myArray->capacity);
return myArray;
}
3、动态数组插入
void PushBack_Array(Dynamic_Array* arr, int value){
if (arr == NULL){
return;
}
if (arr->size == arr->capacity){
int* newSpace = (int*)malloc(sizeof(int) * arr->capacity * 2);
memcpy(newSpace,arr->pAddr,arr->capacity * sizeof(int));
free(arr->pAddr);
arr->capacity = arr->capacity * 2;
arr->pAddr = newSpace;
}
arr->pAddr[arr->size] = value;
arr->size++;
}
4、动态数组的删除(分为按值删除与按位置删除)
void RemoveByPos_Array(Dynamic_Array* arr, int pos){
if (arr == NULL){
return;
}
if(pos < 0 || pos >= arr->size){
return;
}
for (int i = pos; i < arr->size -1; i ++){
arr->pAddr[i] = arr->pAddr[i + 1];
}
arr->size--;
}
void RemoveByValue_Array(Dynamic_Array* arr, int value){
if (arr == NULL){
return;
}
int pos = Find_Array(arr,value);
RemoveByPos_Array(arr, pos);
}
5、动态数组的查找
int Find_Array(Dynamic_Array* arr, int value){
if (arr == NULL){
return -1;
}
int pos = -1;
for (int i = 0; i < arr->size; i++){
if (arr->pAddr[i] == value){
pos = i;
break;
}
}
return pos;
}
6、打印
void Print_Array(Dynamic_Array* arr){
if (arr == NULL){
return;
}
for (int i = 0; i < arr->size;i++){
printf("%d ",arr->pAddr[i]);
}
printf("\n");
}
7、释放内存
void FreeSpace_Array(Dynamic_Array* arr){
if (arr == NULL){
return;
}
if (arr->pAddr != NULL){
free(arr->pAddr);
}
free(arr);
}
8、其他函数(清空数组、获得最大容量、获得当前元素个数、根据位置获得某个位置元素)
void Clear_Array(Dynamic_Array* arr){
if (arr == NULL){
return;
}
arr->size = 0;
}
int Capacity_Array(Dynamic_Array* arr){
if (arr == NULL){
return -1;
}
return arr->capacity;
}
int Size_Array(Dynamic_Array* arr){
if (arr == NULL){
return -1;
}
return arr->size;
}
int At_Array(Dynamic_Array* arr, int pos){
return arr->pAddr[pos];
}