myArray.h :

#pragma once    //包含的时候只包含一次
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define datatype int struct array
{
datatype *pstart; //数组首地址
int length; //长度
int sortstat; //有序或者无序,0无序,1有序
}; void init(struct array *parr); //初始化
void initWithData(struct array *parr, datatype data); //初始化并插入数据
void initWithArr(struct array *parr, datatype *pdata, int datalength); //初始化并插入数组 void show(struct array *parr); //显示 datatype *findfirst(struct array *parr, datatype data); //查找元素,找到返回地址,没找到返回NULL void addobject(struct array *parr, datatype data); //尾部插入一个数据
void addobjects(struct array *parr, datatype *pdata,int datalength); //尾部插入一个数组
void insertobject(struct array *parr, datatype data, datatype insertdata); //任意位置插入一个数据
void insertobjects(struct array *parr, datatype data, datatype *pdata, int datalength); //任意位置插入一个数组

myArray.c :

#include "myArray.h"

//初始化
void init(struct array *parr)
{
if (parr != NULL)
{
parr->pstart = NULL;
parr->length = ;
parr->sortstat = ;
}
else
printf("init Error!\n");
} //初始化并插入数据
void initWithData(struct array *parr, datatype data)
{
if (parr != NULL)
{
parr->pstart = malloc(sizeof(datatype));
*(parr->pstart) = data; //初始化
parr->length = ;
parr->sortstat = ;
}
else
printf("initWithData Error!\n");
} //初始化并插入数组
void initWithArr(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 = ;
}
else
printf("initWithArr Error!\n");
} //显示
void show(struct array *parr)
{
if (parr == NULL || parr->pstart == NULL || parr->length == )
{
printf("没有数据,无法打印\n");
return;
}
else
{
printf("\n数组此时状态:\n");
for (int i = ; i < parr->length; i++)
{
printf("%4d", parr->pstart[i]); //打印数据
}
}
} //尾部插入一个数据
void addobject(struct array *parr, datatype data)
{
if (parr != NULL)
{
if (parr->pstart == NULL || parr->length == )
initWithData(parr, data);
else
{
parr->pstart = realloc(parr->pstart, (parr->length + ) * sizeof(datatype));//拓展内存
parr->length += ; //长度加一
parr->pstart[parr->length] = data; //插入
}
}
else
printf("addobject Error!\n");
} //尾部插入一个数组
void addobjects(struct array *parr, datatype *pdata, int datalength)
{
if (parr != NULL)
{
if (parr->pstart == NULL || parr->length == )
initWithArr(parr, pdata, datalength);
else
{
parr->pstart = realloc(parr->pstart, (parr->length + datalength)*sizeof(datatype));//拓展内存 memcpy(parr->pstart + parr->length, pdata, datalength * sizeof(datatype)); parr->length += datalength; //插入 }
}
else
printf("addobjects Error!\n");
} //查找元素,找到返回地址,没找到返回NULL
datatype *findfirst(struct array *parr, datatype data)
{
if (parr == NULL || parr->pstart == NULL || parr->length == )
{
printf("没有数据,无法查找\n");
return NULL;
}
else
{
int *pfind = NULL;
for (int i = ; i < parr->length; i++)
{
if (parr->pstart[i] == data)
{
pfind = &parr->pstart[i];
break;
}
} return pfind;
}
} //任意位置插入一个数据
void insertobject(struct array *parr, datatype data, datatype insertdata)
{
if (parr != NULL)
{
datatype *pfind = findfirst(parr, data); if(pfind==NULL)
printf("can not insertobject Error!\n");
else
{
int cur = pfind - parr->pstart; //指针相减确定下标
parr->pstart = realloc(parr->pstart, (parr->length +) * sizeof(datatype)); for (int i = parr->length - ; i >= cur; i--)
{
parr->pstart[i + ] = parr->pstart[i]; //往后移动
} parr->pstart[cur] = insertdata; //插入数据
parr->length += ; //长度增加
} }
else
printf("insertobject Error!\n"); } //任意位置插入一个数组
void insertobjects(struct array *parr, datatype data, datatype *pdata, int datalength)
{
if (parr != NULL)
{
datatype *pfind = findfirst(parr, data); if (pfind == NULL)
printf("can not insertobject Error!\n");
else
{
int cur = pfind - parr->pstart; //指针相减确定下标
parr->pstart = realloc(parr->pstart, (parr->length + datalength) * sizeof(datatype)); for (int i = parr->length - ; i >= cur; i--)
{
parr->pstart[i + datalength] = parr->pstart[i]; //往后移动
} memcpy(parr->pstart + cur, pdata, datalength * sizeof(datatype));//插入数据 parr->length += datalength; //长度增加
} }
else
printf("insertobjects Error!\n");
}

main.c :

#include "myArray.h"

void main()
{
struct array mydata;
int a[] = { ,,,,,,,,, };
int b[] = { ,,,, };
int c[] = { ,, }; initWithArr(&mydata, a, ); //初始化数组
show(&mydata); addobjects(&mydata, b, ); //尾部插入数组b
show(&mydata); addobjects(&mydata, c, ); //尾部插入数组c
show(&mydata); insertobject(&mydata, , );
show(&mydata); insertobjects(&mydata, , c, );
show(&mydata); system("pause");
}
05-22 00:15