目录
1.算法的特性:
2.数据结构的分类
2.1逻辑结构
2.2物理结构
3.动态数组
②动态数组初始化
③插入数组
④遍历数组
⑤删除数组
⑥销毁数组
1.算法的特性:
- 输入输出
- 有穷性
- 确定性
- 可行性
2.数据结构的分类
2.1逻辑结构
- 集合结构
- 线性结构
- 树形结构
- 图形结构
2.2物理结构
- 顺序存储
- 链式存储
3.动态数组
①结构体设计
struct dynamicArray
{void** pAddr;//在堆区开批数组的指针int m_Capacity;//能容纳多少int m_Size; //当前存放所有数据的大小
};
②动态数组初始化
struct dynamicArray* initData(int capacity)
{struct dynamicArray* re = malloc(sizeof(struct dynamicArray));if (re == NULL){return NULL;}re->pAddr = malloc(sizeof(void*) * capacity);re->m_Capacity = capacity;re->m_Size = 0;return re;
}
③插入数组
void insert_Array(struct dynamicArray* a, void* data, int pos)
{if (a == NULL || data == NULL){return;}//空间满了if (a->m_Capacity == a->m_Size){int newCapacity = a->m_Capacity * 2;void** newSize = malloc(sizeof(void*) * newCapacity);memcpy(newSize, a->pAddr, a->m_Size);free(a->pAddr);a->pAddr = NULL;a->pAddr = newSize;a->m_Size = newCapacity;}for (int i = a->m_Size; i >= 0; i--){a[i + 1] = a[i]; //整体元素向后移}a->pAddr[pos] = data;a->m_Size++;
}
④遍历数组
void* printArray(struct dynamicArray* array)
{if (NULL == array){return;}for (int i = 0; i = array->m_Size; i++){printf("%d", array->pAddr[i]);}
}
⑤删除数组
通过下标删除:
void removeByPos(struct dynamicArray* a, int pos)
{if (a == NULL){return;}for (int i = pos; i <a->m_Size; i++){a[i] = a[i + 1]; } a->m_Size--;
}
通过值删除:
void removeByValue(struct dynamicArray* a,void* data,int((*compare)(void*,void*)))
{if (a == NULL){return;}for (int i = 0; i = a->m_Size; i++){if (compare(a, data)){removeBypos(a, i);}}
}
int compare(void* data1, void* data2)
{struct dynamicArray* data11 = (struct dynamicArray*)data1;struct dynamicArray* data21 = (struct dynamicArray*)data1;return strcmp(data11->pAddr, data21->pAddr);
}
⑥销毁数组
void destory(struct dynamicArray* a)
{if (a == NULL){return;}if (a->pAddr == NULL){free(a->pAddr);a->pAddr = NULL;}free(a);a = NULL;
}