数据结构 顺序表的数据封装

数据结构

顺序表

定义数据结构:

typedef void (darr_op_t)(const void *);
typedef int (darr_cmp_t)(const void *, const void *);
typedef void (darr_mod_t)(void *, const void *);

//抽象数据类型
typedef struct darr_t{
	//数据
	void *data;//存储数据空间
	int size;//表示数据类型
	int count;//表示数据个数
}DARR;

//函数声明
//==================insert=============
//添加数据到结尾
void darr_end_insert(DARR *handle, void *data);
//添加数据到开头
void darr_first_insert(DARR *handle, void *data);
//指定位置插入
void darr_index_insert(DARR *handle, void *data, int index);
//================del=================
void darr_first_del(DARR *handle, void *key, darr_cmp_t *cmp);
void darr_end_del(DARR *handle, void *key, darr_cmp_t *cmp);
void darr_all_del(DARR *handle, void *key, darr_cmp_t *cmp);
void darr_index_del(DARR *handle, int index);
//=============find=======================
void *darr_first_find(DARR *handle, void *key, darr_cmp_t *cmp);
void *darr_end_find(DARR *handle, void *key, darr_cmp_t *cmp);
DARR *darr_all_find(DARR *handle, void *key, darr_cmp_t *cmp);
void *darr_index_find(DARR *handle, int index);
//===============mod=====================
void darr_first_mod(DARR *handle, void *old_key, darr_cmp_t *cmp, void *new_key, darr_mod_t *mod);
void darr_end_mod(DARR *handle, void *old_key, darr_cmp_t *cmp, void *new_key, darr_mod_t *mod);
void darr_all_mod(DARR *handle, void *old_key, darr_cmp_t *cmp, void *new_key, darr_mod_t *mod);
void darr_index_mod(DARR *handle, int index, void *new_key, darr_mod_t *mod);

//============sort=================
void darr_sort(DARR *handle, darr_cmp_t *cmp);
//================fileio===============
void darr_store(DARR *handle, const char *path);
DARR *darr_load(const char *path);

int darr_num(DARR *);

DARR *darr_creat(int size);
void darr_travel(DARR *handle, darr_op_t *op);

//销毁函数
void darr_destroy(DARR **handle);

​ 函数实现:

*DARR darr_creat(int size)

DARR *darr_creat(int size)
{
	DARR *handle = NULL;

	handle = (DARR *)malloc(sizeof(DARR));
	ERRP(NULL == handle, malloc handle, goto ERR1);

	//存储数据空间
	handle->data = NULL;
	//数据类型
	handle->size = size;
	//数据个数
	handle->count = 0;

	return handle;
ERR1:
	return NULL;
}

**void darr_travel(DARR handle, darr_op_t op)

void darr_travel(DARR *handle, darr_op_t *op)
{
	int i;
	for (i = 0; i < handle->count; i++)
	{
		op(handle->data + i * handle->size);
	}
}

**void darr_destroy(DARR handle)

void darr_destroy(DARR **handle)
{
	free((*handle)->data);
	(*handle)->data = NULL;
	free(*handle);
	*handle = NULL;
}

// insert

void darr_end_insert(DARR *handle, void *data)
{
	void *new = NULL;

	//申请新的空间内存空间
	new = (void *)malloc(handle->size * (handle->count + 1));
	ERRP(NULL == new, malloc new, goto ERR1);

	//拷贝原来数据到新的空间中
	memmove(new, handle->data, handle->size * (handle->count));
	//释放原来数据空间地址
	free(handle->data);
	//拷贝最新接受数据到新的空间中
	memmove(new + handle->count * handle->size, data, handle->size);
	//保存最新的数据空间地址
	handle->data = new;
	//数据个数增加1个
	handle->count++;
	
	return ;
ERR1:
	return ;
}
void darr_first_insert(DARR *handle, void *data)
{
	void *new = NULL;

	//申请新的空间内存空间
	new = (void *)malloc(handle->size * (handle->count + 1));
	ERRP(NULL == new, malloc new, goto ERR1);

	memmove(new, data, handle->size);
	memmove(new + handle->size, handle->data, handle->size * (handle->count));
	free(handle->data);

	handle->data = new;
	handle->count++;
ERR1:
	return ;
}

void darr_index_insert(DARR *handle, void *data,int index)
{
	void *new = NULL;

	ERRP(index < -1 || index > handle->count , index ,goto ERR1);

	//申请新的空间内存空间
	new = (void *)malloc(handle->size * (handle->count + 1));
	ERRP(NULL == new, malloc new, goto ERR1);

	memmove(new , handle->data , handle->size*(index));
	memmove(new+handle->size*(index) , data , handle->size);
	memmove(new+handle->size*(index+1) , handle->data+handle->size*(index) , handle->size*(handle->count-index));
	
	free(handle->data);

	handle->data = new;
	handle->count++;
ERR1:
	return ;
}

//del

void darr_first_del(DARR *handle, void *key, darr_cmp_t *cmp)
{
	int i;
	void *new = NULL;

	for (i = 0; i < handle->count; i++)
	{
		if (cmp(handle->data + i * handle->size, key) == 0)
		{
			new = (void *)malloc(handle->size * (handle->count - 1));
			ERRP(NULL == new, malloc, goto ERR1);

			memcpy(new, handle->data, handle->size * i);
			memcpy(new + handle->size * i, handle->data + handle->size * (i + 1), handle->size * (handle->count - i - 1));
			free(handle->data);
			handle->data = new;
			handle->count--;
			return ;
		}
	}

	return ;
ERR1:
	return ;
}

void darr_end_del(DARR *handle, void *key, darr_cmp_t *cmp)
{
	int i;
	void *new = NULL;

	for(i=handle->count - 1 ; i>=0 ; i--)
	{
		if(cmp(handle->data + i * handle->size, key) == 0)
		{
			new = (void *)malloc(handle->size * (handle->count - 1));
			ERRP(NULL == new, malloc, goto ERR1);

			memcpy(new, handle->data, handle->size * i);
			memcpy(new + handle->size * i, handle->data + handle->size * (i + 1), handle->size * (handle->count - i - 1));
			free(handle->data);
			handle->data = new;
			handle->count--;
			return ;
		}
	}

	return ;
ERR1:
	return ;
}

void darr_all_del(DARR *handle, void *key, darr_cmp_t *cmp)
{
	int i;
	for (i = 0; i < handle->count; i++)
	{	
		void *new = NULL;
		if (cmp(handle->data + i * handle->size, key) == 0)
		{
			new = (void *)malloc(handle->size * (handle->count - 1));
			ERRP(NULL == new, malloc, goto ERR1);

			memcpy(new, handle->data, handle->size * i);
			memcpy(new + handle->size * i, handle->data + handle->size * (i + 1), handle->size * (handle->count - i - 1));
			free(handle->data);
			handle->data = new;
			handle->count--;
			i--;
		}
	}

	return ;
ERR1:
	return ;
}

void darr_index_del(DARR *handle, int index)
{
	int i;
	void *new = NULL;

	ERRP(index < -1 || index > handle->count , index ,goto ERR1);

	new = (void *)malloc(handle->size * (handle->count - 1));
	ERRP(NULL == new, malloc, goto ERR1);
	
	memmove(new , handle->data , handle->size * index);
	memmove(new + handle->size * index , handle->data + handle->size * (index+1) , handle->size * (handle->count - index-1));

	free(handle->data);
	handle->data = new;
	handle->count--;

	return ;
ERR1:
	return ;
}

//find

void *darr_first_find(DARR *handle, void *key, darr_cmp_t *cmp)
{
	int i;
	
	for (i = 0 ; i < handle->count ; i++)
	{
		if (cmp(handle->data + i * handle->size, key) == 0)
		{
			return handle->data + i * handle->size;
		}
	}

	return NULL;
}

void * darr_end_find(DARR *handle, void *key, darr_cmp_t *cmp)
{
	int i;

	for (i = handle->count - 1; i >= 0; i--)
	{
		if (cmp(handle->data + i * handle->size, key) == 0)
		{
			return handle->data + i * handle->size;
		}
	}

	return NULL;
}

DARR *darr_all_find(DARR *handle, void *key, darr_cmp_t *cmp)
{
	DARR *temp = darr_creat(handle->size);
	ERRP(NULL == temp , malloc , goto ERR1);

	int i=0;

	for(i=0 ; i<handle->count ; i++)
	{
		if(cmp(handle->data + i * handle->size , key) == 0)
		{
			darr_first_insert(temp, handle->data + i * handle->size);
		}
	}
	if(NULL != temp->data)
	{
		return temp;
	}
	return NULL;
ERR1:
	return NULL;
}

void *darr_index_find(DARR *handle, int index)
{
	ERRP(index < -1 || index > handle->count , index ,goto ERR1);

	if(NULL != handle->data + index * handle->size)
	{
		return handle->data + index * handle->size;
	}
	return NULL;

ERR1:
	return NULL;
}

//num

int darr_num(DARR *handle)
{
	return handle->count;
}

//sort

void darr_sort(DARR *handle, darr_cmp_t *cmp)
{
	int i,j;
	void *temp = (void *)malloc(handle->size);
	ERRP(NULL == temp , malloc , goto ERR1);

	for(i = 0 ; i < handle->count - 1; i++)
	{
		for(j = 0 ; j<handle->count-i-1 ; j++)
		{
			if(cmp(handle->data + handle->size * j  , handle->data + handle->size * (j+1)) == 1)
			{
				memmove(temp , handle->data + handle->size * j , handle->size);
				memmove(handle->data + handle->size * j , handle->data + handle->size * (j+1) , handle->size);
				memmove( handle->data + handle->size * (j+1) , temp , handle->size);
			}
		}
	}
	free(temp);
ERR1: 
	return ;
}

//fileio

void darr_store(DARR *handle, const char *path)
{
	FILE *fp = NULL;

	fp = fopen(path,"wb");
	ERRP(fp == NULL, fopen, goto ERR1);

	if(fwrite(handle , sizeof(*handle) , 1 , fp) != 1)
	{
		perror("fwrite");
		goto ERR2;
	}

	if(fwrite(handle->data , handle->size , handle->count , fp) != handle->count)
	{
		perror("fwrite");
		goto ERR2;
	}

	fclose(fp);
	return ; 
ERR2:
	fclose(fp);
ERR1:
	return ;
}

DARR *darr_load(const char *path)
{
	FILE *fp = NULL;
	DARR *handle = (DARR *)malloc(sizeof(DARR));

	fp = fopen(path,"rb");
	ERRP(fp == NULL, fopen, goto ERR2);

	if(fread(handle , sizeof(*handle) , 1 , fp) != 1)
	{
		perror("fread");
		goto ERR3;
	}

	handle->data = (void *)malloc(handle->size * handle->count);
	ERRP(handle->data == NULL, malloc, goto ERR3);

	if(fread(handle->data , handle->size , handle->count , fp) != handle->count)
	{
		perror("fread");
		free(handle->data);
		goto ERR3;
	}

	fclose(fp);
	return handle;
	
ERR3:	
	fclose(fp);
ERR2:
	free(handle);
ERR1:
	return NULL;
}

//mod

void darr_first_mod(DARR *handle, void *old_key, darr_cmp_t *cmp,     void *new_key, darr_mod_t *mod)
{
	int i;

	for (i = 0; i < handle->count; i++)
	{
		if (cmp(handle->data + i * handle->size, old_key) == 0)
		{
			mod(handle->data + i * handle->size, new_key);
			return ;
		}
	}
}

void darr_end_mod(DARR *handle, void *old_key, darr_cmp_t *cmp, void *new_key, darr_mod_t *mod)
{
	int i;

	for (i = handle->count -1 ; i >= 0; i--)
	{
		if (cmp(handle->data + i * handle->size, old_key) == 0)
		{
			mod(handle->data + i * handle->size, new_key);
			return ;
		}
	}
}

void darr_all_mod(DARR *handle, void *old_key, darr_cmp_t *cmp, void *new_key, darr_mod_t *mod)
{
	int i;

	for(i = 0 ; i < handle->count ; i++)
	{
		if (cmp(handle->data + i * handle->size, old_key) == 0)
		{
			mod(handle->data + i * handle->size, new_key);
		}
	}
	return ;
}

void darr_index_mod(DARR *handle, int index, void *new_key, darr_mod_t *mod)
{
	ERRP(index < 0 || index > handle->count , index , goto ERR1);

	mod(handle->data + index * handle->size, new_key);

ERR1: 
	return ;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值