声明部分 myarray.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define datatype int
struct array
{
datatype *pstart;//数组首地址
int length;//长度
int sortstat;//有序或者无序 ,0无序,1有序
};
struct Res
{
datatype ** ppstart;//创建指针数组
int n;
};
//初始化
void init(struct array *parr);
//初始化时插入一个数据
void initwithdata(struct array *parr, datatype data);
//初始化时插入一个数组
void initwitharray(struct array *parr, datatype *pdata,int datalength);
//显示数组
void show(struct array *parr);
//尾部添加一个数据
void addobject(struct array *parr, datatype data);
//尾部添加多个数据(数组)
void addobjects(struct array *parr, datatype *pdata, int datalength);
//查找数组中第一个出现的数据
datatype * findfirst(struct array *parr, datatype data);
struct Res findall(struct array *parr, datatype data);
//插入数据
void insertobject(struct array *parr, datatype data,datatype insertdata);
void insertobjects(struct array *parr, datatype data, datatype *pdata, datatype datalength);
//删除数据
void deletefirstobject(struct array *parr, datatype data);//找到第一个就删除掉
void deleteallobject(struct array *parr, datatype data);//删除所有指定的数据
//修改数据
void changefirstobject(struct array *parr, datatype data, datatype newdata);
void changeallobject(struct array *parr, datatype data, datatype newdata);
------------------------------------------------------------------
定义部分 myarray.c
<pre name="code" class="cpp">#include "myarray.h"
void init(struct array *parr)
{
if (parr!=NULL)
{
parr->pstart = NULL;
parr->length = 0;
parr->sortstat = 0;
}
else
{
printf("初始化失败!");
}
}
void initwithdata(struct array *parr, datatype data)
{
if (parr != NULL)
{
parr->pstart = malloc(sizeof(datatype));
*(parr->pstart) = data;//初始化
parr->length = 1;
parr->sortstat = 0;
}
else
{
printf("添加数据失败!");
}
}
void initwitharray(struct array *parr, datatype *pdata, int datalength)
{
if (parr != NULL)
{
parr->pstart = malloc(sizeof(datatype) *datalength);//分配数组长度
memcpy(parr->pstart, pdata, sizeof(datatype) *datalength);//内存拷贝
parr->length = datalength;
parr->sortstat = 0;
}
else
{
printf("添加数组失败!");
}
}
void show(struct array *parr)
{
if (NULL==parr || NULL==parr->pstart || parr->length == 0)
{
printf("数组为空,无法打印!");
return;
}
else
{
printf("\n数组此时状态\n");
for (int i = 0; i < parr->length;i++)
{
printf("%4d", parr->pstart[i]);//打印数据
}
}
}
void addobject(struct array *parr, datatype data)
{
if (parr!=NULL)
{
if (NULL==parr->pstart || parr->length==0)
{
initwithdata(parr, data);
}
else
{
//5 a[4]
parr->pstart = realloc(parr->pstart, (parr->length +1)*sizeof(datatype));//拓展内存
parr->pstart[parr->length] = data;//插入
parr->length += 1;//长度自增
}
}
else
{
printf("添加数据出错!");
}
}
void addobjects(struct array *parr, datatype *pdata, int datalength)
{
if (parr != NULL)
{
if ( NULL==parr->pstart || parr->length == 0)
{
initwitharray(parr, pdata,datalength);//调用初始化
}
else
{
//12345 a[4] &a[5]
parr->pstart = realloc(parr->pstart, (parr->length + datalength) *sizeof(datatype));//拓展内存
memcpy(parr->pstart + parr->length, pdata, datalength *sizeof(datatype));
parr->length += datalength;//长度自增
}
}
else
{
printf("添加数组出错!");
}
}
datatype * findfirst(struct array *parr, datatype data)
{
if (NULL==parr ||NULL== parr->pstart || parr->length == 0)
{
printf("数组为空,无法查找!");
return NULL;
}
else
{
datatype *pfind = NULL;
for (int i = 0; i < parr->length; i++)
{
if (data==parr->pstart[i] )
{
pfind = &parr->pstart[i]; //parr->pstart+i
break;
}
}
return pfind;
}
}
void insertobject(struct array *parr, datatype data, datatype insertdata)
{
if (parr != NULL)
{
datatype *pfind = findfirst(parr, data);
if (NULL==pfind)
{
printf("不存在此数据,无法插入!");
}
else
{
int curr = pfind - parr->pstart;
printf("\n curr=%d", curr);
parr->pstart = realloc(parr->pstart,( parr->length +1)* sizeof(datatype));
for (int i = parr->length - 1; i >= curr;i--)
{
parr->pstart[i + 1] = parr->pstart[i];//往后移动
}
parr->pstart[curr] = insertdata;//插入数据
parr->length += 1;
}
}
else
{
printf("插入数据失败!");
}
}
void insertobjects(struct array *parr, datatype data, datatype *pdata, int datalength)
{
if (parr != NULL)
{
datatype *pfind = findfirst(parr, data);
if (NULL==pfind )
{
printf("无法插入数组,失败!");
}
else
{
int curr = pfind - parr->pstart;
printf("\n curr=%d", curr);
parr->pstart = realloc(parr->pstart, (parr->length + datalength)* sizeof(datatype));
for (int i = parr->length - 1; i >= curr; i--)
{
parr->pstart[i + datalength] = parr->pstart[i];//往后移动
}
//parr->pstart[curr] = insertdata;//插入数据
memcpy(parr->pstart + curr, pdata, datalength* sizeof(datatype));//拷贝数组
parr->length += datalength;//长度自增
}
}
else
{
printf("插入数组失败!");
}
}
void deletefirstobject(struct array *parr, datatype data)
{
if (parr != NULL)
{
datatype *pfind = findfirst(parr, data);
if (NULL==pfind )
{
printf("不存在该数据 无法删除数据!");
}
else
{
int curr = pfind - parr->pstart;//指针相减确定下标,当前数据的下标
for (int i = curr; i < parr->length - 1;i++)
{
parr->pstart[i] = parr->pstart[i + 1];//删除,从后向前移动
}
parr->length -= 1;//长度自增
parr->pstart = realloc(parr->pstart, (parr->length )* sizeof(datatype));//压缩内存
}
}
else
{
printf("删除失败!");
}
}
void deleteallobject(struct array *parr, datatype data)
{
if (parr != NULL)
{
for (int*pcurr = findfirst(parr, data); pcurr != NULL; pcurr = findfirst(parr, data))
{
deletefirstobject(parr, data);
}
}
else
{
printf("删除失败!");
}
}
void changefirstobject(struct array *parr, datatype data, datatype newdata)
{
if (parr != NULL)
{
datatype *pfind = findfirst(parr, data);
if ( NULL ==pfind)
{
printf("\t不存在要修改的数据!");
}
else
{
*pfind = newdata;
}
}
else
{
printf("删除失败!");
}
}
void changeallobject(struct array *parr, datatype data, datatype newdata)
{
if (parr != NULL)
{
for (int*pcurr = findfirst(parr, data); pcurr != NULL; pcurr = findfirst(parr, data))
{
changefirstobject(parr, data,newdata);
}
}
else
{
printf("删除失败!");
}
}
struct Res findall(struct array *parr, datatype data)
{
struct Res ResA;
ResA.n = 0;
for (int i = 0; i < parr->length;i++)
{
if (data==parr->pstart[i])//基本完成
{
ResA.n++;
}
}
ResA.ppstart = malloc(sizeof(datatype *)*ResA.n);//分配内存
int j = 0;//代表下标
for (int i = 0; i < parr->length; i++)
{
if (data == parr->pstart[i])//基本完成
{
ResA.ppstart[j++] = parr->pstart + i;//保存地址
}
}
return ResA;
}
------------------------------------------------------------
主函数测试部分 myarray.c
<pre name="code" class="cpp">#include "myarray.h"
//测试建立动态数组,并实现数组的数据增加(增加一个数组)
void main1()
{
printf("初始化数组:");
struct array mydata;
int a[10] = { 1, 2, 6, 4, 5, 6, 7, 8, 9, 6 };
initwitharray(&mydata, a, 10);
show(&mydata);
printf("\n插入数组:");
int b[5] = { 11, 12, 13, 14 };
addobjects(&mydata, b, 5);
show(&mydata);
int c[4] = { 21, 22, 23, 24 };
addobjects(&mydata, c, 4);
show(&mydata);
system("pause");
}
<img src="https://img-blog.csdn.net/20150802234514380" alt="" />
//测试插入数据(数组)
void main2()
{
printf("初始化数组:");
struct array myarray;
int a[6] = { 33, 45, 22, 67, 72, 25 };
initwitharray(&myarray, a, 6);
show(&myarray);
printf("\n插入数据:");
insertobject(&myarray, 1, 999);
show(&myarray);
insertobject(&myarray, 33, 999);
show(&myarray);
printf("\n插入数组:");
int bArray[5] = { 43, 44, 34, 5, 6 };
insertobjects(&myarray, 25, bArray, 5);
show(&myarray);
system("pause");
}
<img src="https://img-blog.csdn.net/20150802234505894" alt="" />
//测试删除指定的一个数据 以及 所有的数据 改变数据
void main3()
{
struct array myarray;
int a[10] = { 21, 24, 45, 578, 35, 45, 78, 32, 45,578 };
initwitharray(&myarray, a, 10);
show(&myarray);
printf("\n 删除数据:6\n");
deletefirstobject(&myarray, 6);
show(&myarray);
printf("\n 删除数据:21");
deletefirstobject(&myarray,21);
show(&myarray);
printf("\n 删除指定所有数据:45");
deleteallobject(&myarray, 45);
show(&myarray);
system("pause");
}
<img src="https://img-blog.csdn.net/20150802234441792" alt="" />
void main4()
{
struct array myarray;
int a[15] = { 21, 24, 45, 578, 35, 45, 78, 32, 45, 21, 578, 578, 0, 0, 2 };
initwitharray(&myarray, a, 15);
printf("\n 要改变数据:1");
changefirstobject(&myarray, 1, 950);
show(&myarray);
printf("\n 要改变数据:0");
changefirstobject(&myarray, 0, 950);
show(&myarray);
printf("\n 要改变指定所有数据:578");
changeallobject(&myarray, 578, 660);
show(&myarray);
system("pause");
}
<img src="https://img-blog.csdn.net/20150802234432713" alt="" />
//查找在数组中所有相同的数据
void main()
{
struct array myarray;
int a[10] = { 1, 2, 3, 89, 87, 98, 2, 1, 2, 3 };
initwitharray(&myarray, a, 10);
show(&myarray);
struct Res res = findall(&myarray, 3);
for (int i = 0; i < res.n; i++)
{
printf("\n%p %d", res.ppstart[i], *res.ppstart[i]);
}
show(&myarray);
system("pause");
}