c语言动态数组的实现

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

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

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

动态数组的实现

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

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

函数头文件的代码

#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类别

函数具体实现代码

#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函数代码

#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);
}

动态数组运行结果展示

相关推荐

  1. C++(2) 结构体和动态实现

    2024-02-08 18:38:01       31 阅读
  2. C语言初始化

    2024-02-08 18:38:01       40 阅读
  3. C语言三维创建

    2024-02-08 18:38:01       23 阅读
  4. C语言实现加法

    2024-02-08 18:38:01       35 阅读
  5. Js中实用语法

    2024-02-08 18:38:01       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-08 18:38:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-08 18:38:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-08 18:38:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-08 18:38:01       20 阅读

热门阅读

  1. Python并发

    2024-02-08 18:38:01       38 阅读
  2. Jgit Packfile is truncated解决方案

    2024-02-08 18:38:01       29 阅读
  3. leetcode-206-翻转链表

    2024-02-08 18:38:01       32 阅读
  4. 210 springcloud常见面试题

    2024-02-08 18:38:01       30 阅读
  5. 2.5作业

    2024-02-08 18:38:01       25 阅读
  6. 51单片机基础(C语言):定时器时钟

    2024-02-08 18:38:01       36 阅读
  7. 使用Spring AOP实现对外接口的日志自动打印

    2024-02-08 18:38:01       36 阅读