连续存储----数组增、删、改操作API

以下代码是自己在学习数据结构------连续存储的总结,包括对数组的增/删/插入/排序/倒置/空、满判断。在vc++ 6.0中的调试成功,输出结果是:

---------------- --------------------------------华丽的分割线-------------------------------------------------     

/*delete 100 success
the array num is :
45      85      120     456
the array num is :
456     120     85      45*/      
                                                 

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

struct Array
{
	int *pBase;//the point of first number of a array;
	int cnt;//the count of a  array's numbers;
	int len;//the length of a array;
};//it is the information about a array

//following  is the operater of array functions;

void init_array(struct Array * pAry, int length);
void show_array(struct Array * pAry);
void append_array(struct Array * pAry, int val);
void insert_array(struct Array * pAry, int pos, int val);
void short_array(struct Array * pAry);
void inversion_array(struct Array * pAry);
bool delete_val(struct Array * pAry, int pos, int *pVal);
bool is_empty(struct Array * pAry);
bool is_full(struct Array * pAry);

int main()
{
	int val;
	struct Array arr;
	init_array(&arr,6);
	append_array(&arr,45);
	append_array(&arr,85);
	append_array(&arr,100);
	append_array(&arr,120);
	insert_array(&arr,3,456);
	short_array(&arr);
	//delete_val(&arr,3,&val);
	if(delete_val(&arr,3,&val))
	{
		printf("delete %d success\n",val);//from the function we can know that sub_fuction can modify main vale.
	}
	show_array(&arr);
	inversion_array(&arr);
	printf("\n");
	show_array(&arr);
	return 0;
}

void init_array(struct Array * pAry,int length)
{
	pAry->pBase = (int  *)malloc(sizeof(int)*length);//create the memory of a array;
	
	if(NULL == pAry->pBase)
	{
		printf("the memory is create fail\n");
		exit(-1);// break out the program.
	}
	else
	{
		pAry->len = length;
		pAry->cnt = 0;
	}
	
	return ;
}

bool is_empty(struct Array * pAry)
{
	if(0 == pAry->cnt)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void show_array(struct Array * pAry)
{
	if(is_empty(pAry))
	{
		printf("the array is empty\n");
	}
	else
	{
		printf("the array num is :\n");
		for(int i=0; i<pAry->cnt; ++i)
		{
			
			printf("%d\t", *((pAry->pBase)+i));
		}
	}
	return;
}

bool is_full(struct Array * pAry)
{
	if(pAry->cnt == pAry->len)
		return true;
	else
		return false;
}

void append_array(struct Array * pAry, int val)
{
	if(is_full(pAry))
	{
		printf("the array is become to max can't to append");
	}
	else
	{
		*((pAry->pBase)+pAry->cnt) = val;
		(pAry->cnt)++;
	}
	return ;
}

void insert_array(struct Array * pAry, int pos, int val)//pos is counted from 1;
{
	if(pos<1 || pos>pAry->cnt +1)
	{
		printf("the postion that you want to enter is abort!\n");
		exit(-1);
	}
	if(is_full(pAry))
	{	
		printf("the array is become to max can't to insert\n");
		exit(-1);
	}
	else
	{
		for(int i=pAry->cnt-1 ; i>=pos-1; --i)
		{
			*((pAry->pBase)+i+1) = *((pAry->pBase)+i);
		}
		*((pAry->pBase)+pos-1) = val;
		(pAry->cnt)++;
	}
	return ;
}

bool delete_val(struct Array * pAry, int pos, int *pVal)
{
	if(pos<1 || pos>pAry->cnt)
	{
		printf("The postion that you want to delete is wrong!\n");
		return false;
		exit(-1);
	}
	*pVal = *((pAry->pBase)+pos-1);
	if(is_empty(pAry))
	{
		printf("There is no value to deleted\n");
		return false;
		exit(-1);
	}
	else
	{
		for(int i=pos; i<pAry->cnt; ++i)
		{
			*((pAry->pBase)+i-1) = *((pAry->pBase)+i);
		}
		(pAry->cnt)--;
	}
	return true;
}

void inversion_array(struct Array * pAry)
{
	int i= 0;
	int j= pAry->cnt-1;
	int temp;
	
	while(i<j)
	{
		temp = *((pAry->pBase)+i);

		*((pAry->pBase)+i) = *((pAry->pBase)+j);
		*((pAry->pBase)+j) = temp;
		++i;
		--j;
	}
	return ;
}
void short_array(struct Array * pAry)
{
	int temp;
	for(int i=0; i<pAry->cnt; ++i)
	{
		for(int j= i+1; j<pAry->cnt; ++j)
		{
			if(*((pAry->pBase)+i) > *((pAry->pBase)+j))
			{
				temp = 	*((pAry->pBase)+i);
				*((pAry->pBase)+i) = *((pAry->pBase)+j);
				*((pAry->pBase)+j) =  temp;
			}
		}
	}
	return ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值