动态数组(c语言版)

动态数组(c语言版)

数组是C语言中的很重要的一种构造类型,最初我们学习的都是静态数组,但是,静态数组有着自己难以改变的缺点——数组长度固定
静态数组在使用时我们不知道需要存放开辟的空间是否刚刚好,使用动态数组可以更好的节约空间,我们需要多少内存空间就可以根据自己的需求来开辟,也可以更好的管理好内存,因此动态数组在使用中体现着重要的地位。在平时的练习中也更应该学会使用动态数组。

动态数组测试

/*********************下面是常用的一些方式使用********************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

struct Array
{
	unsigned int *Phead;//首地址
	unsigned int Capacity;//容量
	unsigned int Num; //数量
};


void Init_Array(struct Array *P);  //初始化数组
void Array_Element_Detection(struct Array *p); //数组合法性检测
void Add_Element(struct Array *p,int data);//增加数组元素
void Insert_Element(struct Array *P,int Data,int Subscript);//插入元素
void Delete_Tail_Element(struct Array *P); //从尾部删除元素
void Delete_All_Element(struct Array *P); //删除全部元素
void Array_Free(struct Array *P);//释放整个数组
void Delete_Middle_Element(struct Array *P ,int Subscript);//删除数组中间元素
void Modi_Array_Element(struct Array *P ,int Subscript,int NewData);//修改数组元素的值
void Find_Array_Element(struct Array *P ,int data);//查找数组元素
void Output(struct Array *p);//输出数组元素


//初始化数组
void Init_Array(struct Array *P)
{
	P->Phead = (unsigned int *)malloc(sizeof(P->Capacity));
	P->Capacity = 5;
	P->Num = 0;
}


//数组合法性检测
void Array_Element_Detection(struct Array *p)
{
	if(p->Capacity == p->Num)// 数量已满
	{
		//容量变大
		p->Capacity = (p->Capacity + 10);
		//重新申请空间
		unsigned int *temp = (unsigned int *)malloc(sizeof(p->Capacity));
		memset(temp ,'\0',sizeof(p->Capacity));
		//将元数据复制进新空间
		for(unsigned int i = 0 ; i < p->Capacity ;i++)
		{
			temp[i] = p->Phead[i];
		}

		//将原空间释放
		free(p->Phead);
		//将数组头指针指向新空间
		p->Phead = temp;
	}
}


//增加数据
void Add_Element(struct Array *p,int data)
{
	if(p == NULL)
	{
		printf("param error\n");
		return ;
	}
	Array_Element_Detection(p);
	//数据装进去
	p->Phead[p->Num] = data;
	p->Num++;
}


void Insert_Element(struct Array *P,int Data,int Subscript)
{
	if(P== NULL)
	{
		printf("param error\n");
		return ;
	}
	//参数合法性检测
	Array_Element_Detection(P);
	//向后移动
	for(unsigned int i = P->Num ; i >= Subscript ;i-- )
	{
		P->Phead[i] = P->Phead[i-1];
	}
	//数据复制到指定位置
	P->Phead[Subscript] = Data;
	//数量加1
	P->Num++;
}


//元素删除(尾部)
void Delete_Tail_Element(struct Array *P)
{

	if(P == NULL)
	{
		printf("param error\n");
		return ;
	}
	P->Num--;

}

//元素全部删除
void Delete_All_Element(struct Array *P)
{
	if(P == NULL)
	{
		printf("param error\n");
		return ;
	}
	P->Num = 0;
}

//删除中间元素
void Delete_Middle_Element(struct Array *P ,int Subscript)
{
	if(P == NULL)
	{
		printf("param error\n");
		return ;
	}

	if(Subscript > P->Num)
	{
		printf("超出范围,不合法!\n");
		return ;
	}
	//后边向前面移
	for(unsigned int i = Subscript -1 ; i < P->Num -1 ;i++)
	{
		P->Phead[i] = P->Phead[i+1];
	}
	//数量减一
	P->Num--;

}


//修改数组中元素值
void Modi_Array_Element(struct Array *P ,int Subscript,int NewData)
{
	if(P == NULL) //合法性判断
	{
		printf("param error\n");
		return ;
	}
	//判断修改元素是否在该数组范围内
	if(Subscript > P->Num)
	{
		printf(" %d not exit in array !\n");
		return;
	}
	P->Phead[Subscript] = NewData;
}

//查找数组中元素
void Find_Array_Element(struct Array *P,int data)
{
	if(P == NULL) //合法性判断
	{
		printf("param error\n");
		return ;
	}
	for(unsigned int i = 0 ; i < P->Num ;i++)
	{
		if(P->Phead[i] == data)
		{
			printf("%u exit in this array Subscript is %u\n",P->Phead[i],i);
			return ;
		}
		else
		{
		printf("Not Find!\n");
		return ;
		}
	}
}


void Array_Free(struct Array *P)
{
	if(P == NULL)
	{
		printf("param error\n");
		return ;
	}
	P->Num = 0;
	P->Capacity = 0;
	free(P->Phead);
	P->Phead = NULL;
}

//输出
void Output(struct Array *p)
{
	if(p == NULL)
	{
		printf("param error\n");
		return ;
	}
	printf("数组容量: %u\n数组大小:%u\n",p->Capacity,p->Num);
	for(unsigned int i = 0 ; i < p->Num ; i++)
	{
		printf(" %d ",p->Phead[i]);
	}
	printf("\n");
}



int main(void)
{
	struct Array head;
	Init_Array(&head);
	Add_Element(&head,1);
	Add_Element(&head,7);
	Add_Element(&head,9);
	Add_Element(&head,0);
	
	Insert_Element(&head,3,2);
	Output(&head);
	Delete_Tail_Element(&head);
	Output(&head);
	Delete_Middle_Element(&head,3);
	Output(&head);
	Find_Array_Element(&head,10);
	Modi_Array_Element(&head,2,8);
	Output(&head);
	free(head.Phead);
	return 0;
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小殷学长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值