c语言动态数组的实现以及相关增删的功能的实现

目录

一.动态数组:

二.动态数组的初始化

三:动态数组的数据插入:

四:数据的删除

五:动态数组的销毁:

六:完整代码

最后,如果这篇文章对你有所启发或者没什么启发那么就点点赞,收收藏,转转发,你们的支持就是我前进的动力(一个赞一道题),下次再见。


一.动态数组:

动态数组首要的是动态,而且为了这个数组能够适配任意类型,所以我们这里使用的是void*型的指针数组来存放数据的指针,通过指针来访问各种类型的数组。er


typedef struct x
{
	void** ptr;//指向void*类型的数组的指针
	int capacity;//数组的容量大小
	int num;//数组元素的个数
}D_array;

二.动态数组的初始化

首先申请一块空间,并且让ptr指向那块空间,然后将容量和数组元素个数进行初始化。


D_array* inti_data(int capacity)//初始化动态数组,返回的是array的指针
{
	D_array* array = (D_array*)malloc(sizeof(D_array));
	if (array == NULL)//如果开辟空间失败就返回空指针
		return  NULL;
	array->ptr = (void**)malloc(sizeof(void*) * capacity);//申请一片空间,用于存放数据的地址
	if (array->ptr == NULL)//判断是否为空
		return NULL;
	array->capacity = capacity;
	array->num = 0;
	return array;
}

三:动态数组的数据插入:

这里使用的方法是按位置插入,所以当给定的位置不合理时就会退出函数,

如果数组的大小已经不够的话就要对数组进行扩容

如果给定的位置大于现在数组有的元素个数,那么我们可以实行尾插,如果不是尾插,那么就要向后移动元素,给目的空间腾出一个位置来插入数据。


void insert_data(D_array* array, int pos, void* data)
{
	if (pos < 0)
	{
		printf("位置错误\n");
		return;
	}
	if (array->capacity == array->num)//扩容操作
	{
		int newcapacity = array->capacity + plus_capacity;
		void** newspace = (void**)malloc(sizeof(void*) * newcapacity);
		if (newspace == NULL)
		{
			printf("扩容失败\n");
			return;
		}
		memcpy(newspace, array->ptr, sizeof(void*) * array->capacity);
		free(array->ptr);//释放掉原来的空间
		array->ptr = newspace;
		array->capacity = newcapacity;
		printf("扩容成功\n");
	}	
	//末尾插入
		if (pos >= array->num)
		{
			pos = array->num;
		}
		//移动元素
		for (int i = array->num - 1; i >= pos; i--)
		{
			array->ptr[i + 1] = array->ptr[i];
		}
		//指定位置插入
		memcpy(array->ptr + pos, &data, 4);
		array->num++;
	}

四:数据的删除

(这里使用的是按位置来删除,也可以实现按值的方式来删除。)


void del_data(D_array *array,int pos)
{
    if(pos>=array->num)
        return; 
	for (int i = pos; i < array->num - 1; i++)
	{
		array->ptr[i] = array->ptr[i + 1];//把要删除的位置之后的所有元素都往前面移动一位,要删除的数据就会被覆盖
	}
	array->num--;//最后只需将数组元素个数减1即可
}

五:动态数组的销毁:


void destroy(D_array* array)//空间释放、指针置空、数据清零
{
	free(array->ptr);
	array->ptr = NULL;
	array->num = 0;
	array->capacity = 0;
}

六:完整代码


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define start_capacity 5
#define plus_capacity 5
typedef struct x
{
	void** ptr;
	int capacity;
	int num;
}D_array;
D_array* inti_data(int capacity)//初始化动态数组
{
	D_array* array = (D_array*)malloc(sizeof(D_array));
	if (array == NULL)
		return  NULL;
	array->ptr = (void**)malloc(sizeof(void*) * capacity);
	if (array->ptr == NULL)
		return NULL;
	array->capacity = capacity;
	array->num = 0;
	return array;
}
void insert_data(D_array* array, int pos, void* data)
{
	if (pos < 0)
	{
		printf("位置错误\n");
		return;
	}
	if (data == NULL)
		return;
	if (array->capacity == array->num)//扩容
	{
		int newcapacity = array->capacity + plus_capacity;
		void** newspace = (void**)malloc(sizeof(void*) * newcapacity);
		if (newspace == NULL)
		{
			printf("扩容失败\n");
			return;
		}
		memcpy(newspace, array->ptr, sizeof(void*) * array->capacity);
		free(array->ptr);
		array->ptr = newspace;
		array->capacity = newcapacity;
		printf("扩容成功\n");
	}	
	//末尾插入
		if (pos > array->num)
		{
			pos = array->num;
		}
		//不是尾插就要移动
		for (int i = array->num - 1; i >= pos; i--)
		{
			array->ptr[i + 1] = array->ptr[i];
		}
		//指定位置插入
		memcpy(array->ptr + pos, &data, 4);
		array->num++;
	}
void userprint(void *p)//用户根据需要来自定义数据类型
{
	int* p1 = (int*)p;
	printf("%d ", *p1);
}
void foreach(D_array* array)//动态数组的遍历
{
	for (int i = 0; i < array->num; i++)
	{
		userprint(array->ptr[i]);
	}
	printf("\n");
}
//数据的删除
void del_data(D_array *array,int pos)
{
	if (pos >= array->num)
		return;
	for (int i = pos; i < array->num - 1; i++)
	{
		array->ptr[i] = array->ptr[i + 1];
	}
	array->num--;
}
void destroy(D_array* array)
{
	free(array->ptr);
	array->ptr = NULL;
	array->num = 0;
	array->capacity = 0;
}
int main()
{
	D_array* array= inti_data(start_capacity);
	//测试初始化数组:
	//printf("%d    %d",array->capacity,array->num);
	//测试扩容和插入
	int p1 = 3;
	int p2 = 5;
	int p3 = 4;
	insert_data(array, 4, &p1);
	insert_data(array, 0, &p2);
	insert_data(array, 2, &p3);
	foreach(array);
	printf("%d  %d\n", array->capacity, array->num);
	del_data(array, 1);
	foreach(array);
	printf("%d  %d\n", array->capacity, array->num);
	destroy(array);
	printf("%d  %d\n", array->capacity, array->num);
}

最后,如果这篇文章对你有所启发或者没什么启发那么就点点赞,收收藏,转转发,你们的支持就是我前进的动力(一个赞一道题),下次再见。

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
C语言结构体数组可以用来设计教务管理系统的增删改查功能。下面是一个简单的示例: 1. 定义结构体类型: ```c typedef struct { char name[20]; int studentID; float score; } Student; ``` 2. 声明结构体数组并初始化: ```c Student students[100]; // 假设最多有100个学生 int count = 0; // 记录当前学生数量 // 添加学生信息 strcpy(students[count].name, "张三"); students[count].studentID = 1001; students[count].score = 90.5; count++; strcpy(students[count].name, "李四"); students[count].studentID = 1002; students[count].score = 85.0; count++; ``` 3. 实现增加学生信息的功能: ```c void addStudent() { if (count >= 100) { printf("学生数量已达上限,无法添加新学生。\n"); return; } printf("请输入学生姓名:"); scanf("%s", students[count].name); printf("请输入学生学号:"); scanf("%d", &students[count].studentID); printf("请输入学生成绩:"); scanf("%f", &students[count].score); count++; printf("学生信息添加成功。\n"); } ``` 4. 实现删除学生信息的功能: ```c void deleteStudent() { int studentID; printf("请输入要删除的学生学号:"); scanf("%d", &studentID); int i; for (i = 0; i < count; i++) { if (students[i].studentID == studentID) { // 将最后一个学生信息覆盖到要删除的位置 students[i] = students[count - 1]; count--; printf("学生信息删除成功。\n"); return; } } printf("未找到该学生学号对应的学生信息。\n"); } ``` 5. 实现修改学生信息的功能: ```c void modifyStudent() { int studentID; printf("请输入要修改的学生学号:"); scanf("%d", &studentID); int i; for (i = 0; i < count; i++) { if (students[i].studentID == studentID) { printf("请输入新的学生姓名:"); scanf("%s", students[i].name); printf("请输入新的学生成绩:"); scanf("%f", &students[i].score); printf("学生信息修改成功。\n"); return; } } printf("未找到该学生学号对应的学生信息。\n"); } ``` 6. 实现查询学生信息的功能: ```c void queryStudent() { int studentID; printf("请输入要查询的学生学号:"); scanf("%d", &studentID); int i; for (i = 0; i < count; i++) { if (students[i].studentID == studentID) { printf("学生姓名:%s\n", students[i].name); printf("学生成绩:%.2f\n", students[i].score); return; } } printf("未找到该学生学号对应的学生信息。\n"); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值