c语言动态数组的实现

动态数组是在程序运行时动态分配内存空间的数组,可以根据需要随时改变大小。在C语言中,动态数组通常通过指针和malloc函数来实现。

  1. 使用malloc函数动态分配内存空间:
cpp 复制代码
int *arr;
int size = 10;
arr = (int*)malloc(size * sizeof(int));
  1. 使用realloc函数调整动态数组的大小
cpp 复制代码
int new_size = 20;
arr = (int*)realloc(arr, new_size * sizeof(int));
  1. 使用free函数释放动态数组占用的内存
cpp 复制代码
free(arr);
  1. 使用指针操作动态数组元素:

    arr[0] = 10;
    arr[1] = 20;

需要注意的是,动态数组在使用完毕后应该及时释放内存,避免内存泄漏。另外,使用动态数组时需要注意数组下标的范围,避免访问越界导致程序崩溃。

动态数组的实现

模块化的编程方式:将头文件,函数,和main文件分离出来

函数头文件:对函数和结构体进行声明

函数头文件的代码

cpp 复制代码
#ifndef __MALLOC_H_
#define __MALLOC_H_
#include <stdbool.h>
// 定义符号常量,确定数组拥有的元素个数
#define VECTOR_INIT_CAPACITY 1
// 定义学生结构体:包含学生 id 名字,性别...
struct student {
	int id;
	char name[20];
	int gender;
	int mark;

};

// 动态数组vector包含动态数组的4个方法
struct vector {
	bool (*append) (struct vector* pVec, struct student data);
	struct student(*get)(struct vector* pVec, int index);
	void (*clear)(struct vector* pVec);
	void (*remove)(struct vector* pVec, int index);

	struct student* pData;// 数组首元素指针
	int size; // 数组已经存放的大小
	int capacity; // 数组容器的容量


};
void vectorInit(struct vector* pVec); // 初始化函数
bool vectorAppend(struct vector* pVec, struct student data);// 函数的添加
struct student vectorGet(struct vector* pVec, int index); // 获取数据
void vectorRemove(struct vector* pVec, int index); // 移除数组中的元素
void vectorClear(struct vector* pVec); // 清空数组
void vectorDestroy(struct vector* pVec); // 销毁数组

#endif

数组实现函数UTIL类别

函数具体实现代码

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "MallocLineData.h"
// 初始化函数
void vectorInit(struct vector* pVec) {
    // 初始化对象的方法
    pVec->get = vectorGet;
    pVec->append = vectorAppend;
    pVec->remove = vectorRemove;
    pVec->clear = vectorClear;


    pVec->pData = (struct student*)malloc(sizeof(struct student) * VECTOR_INIT_CAPACITY);
    pVec->size = 0;
    pVec->capacity = VECTOR_INIT_CAPACITY;
}
// 添加函数的代码
bool vectorAppend(struct vector* pVec, struct student data) {
    if (pVec->size >= pVec->capacity) {
        struct student* newData = (struct student*)
            realloc(pVec->pData, pVec->capacity * sizeof(struct student) * 2);
        if (newData == NULL) {
            return false;
        }
        pVec->pData = newData;
        pVec->capacity = 2 * pVec->capacity;
    }
    pVec->pData[pVec->size] = data;
    pVec->size++;
    return true;
}
// 获取数组元素的首地址
struct student vectorGet(struct vector* pVec, int index) {
    return pVec->pData[index];
}
// 移除数组中的元素
void vectorRemove(struct vector* pVec, int index) {
    for (int i = index; i < pVec->size - 1; i++) {
        pVec->pData[i] = pVec->pData[i + 1];
    }
    pVec->size -= 1;
}
//清除数组中元素的方法
void vectorClear(struct vector* pVec) {
    if (pVec->pData != NULL) {
        free(pVec->pData);
    }
    pVec->pData = (struct student*)malloc
    (sizeof(struct student) * VECTOR_INIT_CAPACITY);
    pVec->size = 0;
    pVec->capacity = VECTOR_INIT_CAPACITY;
}
// 销毁不需要使用的数组
void vectorDestroy(struct vector* pVec) {
    if (pVec->pData == NULL) {
        return;

    }
    free(pVec->pData);
    pVec->pData = NULL;
    pVec->size = 0;
    pVec->capacity = 0;
}

main函数代码

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "MallocLineData.h"

int main()
{
   // 对象的声明
	struct vector vec;
	vectorInit(&vec);
	struct student s1 = { 1,"小黄",1,90 };
	vec.append(&vec, s1);
	for (int i = 0; i < vec.size; i++) {
		struct student s = vec.get(&vec, i);
		printf("%d %s %d %d\n", s.id, s.name, s.gender, s.mark);
	}
	printf("size:%d\n", vec.size);
	printf("capacity:%d\n", vec.capacity);

	// remove方法的测试
	vec.remove(&vec, 0);
	for (int i = 0; i < vec.size; i++) {
		struct student s = vec.get(&vec, i);
		printf("%d %s %d %d\n", s.id, s.name, s.gender, s.mark);
	}
	printf("size:%d\n", vec.size);
	printf("capacity:%d\n", vec.capacity);

	// 调用清除数据的方法
	vec.clear(&vec);
	for (int i = 0; i < vec.size; i++) {
		struct student s = vec.get(&vec, i);
		printf("%d %s %d %d\n", s.id, s.name, s.gender, s.mark);
	}
	printf("size:%d\n", vec.size);
	printf("capacity:%d\n", vec.capacity);

	// 调用销毁数据的方法
	vectorDestroy(&vec);
	for (int i = 0; i < vec.size; i++) {
		struct student s = vec.get(&vec, i);
		printf("%d %s %d %d\n", s.id, s.name, s.gender, s.mark);
	}
	printf("size:%d\n", vec.size);
	printf("capacity:%d\n", vec.capacity);
}

动态数组运行结果展示

相关推荐
xu_yule1 天前
算法基础-(单调队列)
算法·单调队列
EXtreme351 天前
【数据结构】建堆操作:向上调整与向下调整的数学推导与性能对比
c语言·数据结构··时间复杂度·topk问题·算法分析
代码不停1 天前
Java递归综合练习
java·开发语言·算法·回归
前端小白在前进1 天前
力扣刷题:删除排序链表的重复元素Ⅱ
算法·leetcode·链表
ChoSeitaku1 天前
NO16数据结构选择题考点|树
数据结构
qq_214782611 天前
Hadley Wickham长文回顾:R语言tidyverse过去20年的演进之路、现状与未来展望!
python·算法·线性回归
霍田煜熙1 天前
C++ 部署小型图书管理系统
开发语言·c++·算法
ywwwwwwv1 天前
力扣300
算法·leetcode·职场和发展
来自于狂人1 天前
HCIE云计算超长考点精析
算法·贪心算法·云计算
@小码农1 天前
2025年厦门市小学生信息学竞赛C++(初赛)真题-附答案
开发语言·c++·python·算法·蓝桥杯