使用C语言封装数组,动态实现增删改查
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 = 0; parr->sortstat = 0; } 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 = 1; parr->sortstat = 0; } 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 = 0; } else printf("initWithArr Error!\n"); } //显示 void show(struct array *parr) { if (parr == NULL || parr->pstart == NULL || parr->length == 0) { printf("没有数据,无法打印\n"); 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 (parr->pstart == NULL || parr->length == 0) initWithData(parr, data); else { parr->pstart = realloc(parr->pstart, (parr->length + 1) * sizeof(datatype));//拓展内存 parr->length += 1; //长度加一 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 == 0) 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 == 0) { printf("没有数据,无法查找\n"); return NULL; } else { int *pfind = NULL; for (int i = 0; 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 +1) * sizeof(datatype)); for (int i = parr->length - 1; i >= cur; i--) { parr->pstart[i + 1] = parr->pstart[i]; //往后移动 } parr->pstart[cur] = insertdata; //插入数据 parr->length += 1; //长度增加 } } 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 - 1; 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[10] = { 1,2,3,4,5,6,7,8,9,10 }; int b[5] = { 11,12,13,14,15 }; int c[3] = { 21,22,23 }; initWithArr(&mydata, a, 10); //初始化数组 show(&mydata); addobjects(&mydata, b, 5); //尾部插入数组b show(&mydata); addobjects(&mydata, c, 3); //尾部插入数组c show(&mydata); insertobject(&mydata, 21, 666); show(&mydata); insertobjects(&mydata, 6, c, 3); show(&mydata); system("pause"); }
文章写来不易,转载请标注。。。欢迎关注!