/* 顺序线性表头文件 */ #ifndef _vzhangseqlist #define _vzhangseqlist typedef void SeqList; typedef void SeqListNode; //创建线性表 _declspec(dllexport)//如果在动态库中定义头文件 这句话不可以缺少 不然会报错 函数重定义 SeqList* SeqList_Create(int capacity); //销毁线性表 _declspec(dllexport) int SeqList_Destroy(SeqList** list); //清空线性表 _declspec(dllexport) int SeqList_Clear(SeqList* list); //获取线性表长度 _declspec(dllexport) int SeqList_Length(SeqList* list); //获取线性表的容量 _declspec(dllexport) int SeqList_Capacity(SeqList* list); //线性表指定位置插入元素 _declspec(dllexport) int SeqList_Insert(SeqList* list, SeqListNode* node, int pos); //获取线性表指定位置的元素 _declspec(dllexport) SeqListNode* SeqList_Get(SeqList* list, int pos); //删除线性表指定位置的元素 _declspec(dllexport) SeqListNode* SeqList_Delete(SeqList* list, int pos); #endif
//动态库代码实现 #include<stdio.h> #include<stdlib.h> #include<string.h> #include"SeqList.h" typedef struct _TSeqList { //指针数组---内存模型① unsigned int * Nodes; //数组元素个数 int length; //数组最大容量 int capacity; }TSeqList; //创建线性表 _declspec(dllexport)//文件导出关键字 SeqList* SeqList_Create(int capacity) { TSeqList * tlist = (TSeqList *)malloc(sizeof(TSeqList)); if (tlist==NULL) { printf("分配内存失败!\n"); return NULL; } //分配指针域数组 tlist->Nodes = (unsigned int *)malloc(sizeof(int)*capacity); if (tlist->Nodes==NULL) { printf("分配内存失败!\n"); return NULL; } tlist->capacity = capacity; tlist->length = 0; return (SeqList*)tlist; } //销毁线性表 _declspec(dllexport) int SeqList_Destroy(SeqList** list) { int ERRO_MSG = 0; if (list==NULL) { ERRO_MSG = -1; printf("list==NULL 传入参数不可以为空! erro msg:%d\n", ERRO_MSG); return ERRO_MSG; } TSeqList * tlist = (TSeqList *)*list; //释放指针域数组内存 if (tlist->Nodes!=NULL) { free(tlist->Nodes); tlist->Nodes = NULL; } //释放线性表对象 if (tlist!=NULL) { free(tlist); tlist = NULL; *list = NULL; } return ERRO_MSG; } //清空线性表 _declspec(dllexport) int SeqList_Clear(SeqList* list){ int ERRO_MSG = 0; if (list==NULL) { ERRO_MSG = -1; printf("list==NULL 传入参数不可以为空! erro msg:%d\n", ERRO_MSG); return ERRO_MSG; } TSeqList *tlist = (TSeqList *)list; memset(tlist->Nodes, 0, sizeof(unsigned int)*tlist->capacity); tlist->length = 0; return ERRO_MSG; } //获取线性表长度 _declspec(dllexport) int SeqList_Length(SeqList* list) { int ERRO_MSG = 0; if (list == NULL) { ERRO_MSG = -1; printf("list==NULL 传入参数不可以为空! erro msg:%d\n", ERRO_MSG); return ERRO_MSG; } TSeqList *tlist = (TSeqList *)list; return tlist->length; } //获取线性表的容量 _declspec(dllexport) int SeqList_Capacity(SeqList* list){ int ERRO_MSG = 0; if (list == NULL) { ERRO_MSG = -1; printf("list==NULL 传入参数不可以为空! erro msg:%d\n", ERRO_MSG); return ERRO_MSG; } TSeqList *tlist = (TSeqList *)list; return tlist->capacity; } //线性表指定位置插入元素 _declspec(dllexport) int SeqList_Insert(SeqList* list, SeqListNode* node, int pos){ int ERRO_MSG = 0, i = 0; if (list == NULL) { ERRO_MSG = -1; printf("list==NULL 传入参数不可以为空! erro msg:%d\n", ERRO_MSG); return ERRO_MSG; } TSeqList *tlist = (TSeqList *)list; /* 顺序线性表指定位置插入元素 后面的元素必须后移 */ //判断数组元素是不是满了 if (tlist->length >= tlist->capacity) { ERRO_MSG = -2; printf("数组元素已满! erro msg:%d\n", ERRO_MSG); return ERRO_MSG; } //判断插入位置--线性表必须一个个的插入 不能在容量位置向前插入 这样数组中间有些位置会空闲 if (pos<0 || pos>tlist->length) { //注意:这里的长度是数组已有元素的个数 //进行容错处理--从尾部插入 pos = tlist->length; } for (i = tlist->length; i >pos; i--) { /* 刚好将第pos个元素的位置空出来 */ tlist->Nodes[i] = tlist->Nodes[i - 1]; } //将指针强转为unsigned int类型 tlist->Nodes[pos] = (unsigned int) node; //数组元素个数加1 tlist->length++; return ERRO_MSG; } //获取线性表指定位置的元素 _declspec(dllexport) SeqListNode* SeqList_Get(SeqList* list, int pos){ int ERRO_MSG = 0, i = 0; if (list == NULL) { ERRO_MSG = -1; printf("list==NULL 传入参数不可以为空! erro msg:%d\n", ERRO_MSG); return NULL; } TSeqList *tlist = (TSeqList *)list; //判断获取位置 if (pos<0 || pos>tlist->length) { ERRO_MSG = -2; printf("没有该位置的元素! erro msg:%d\n", ERRO_MSG); return NULL; } return (SeqListNode*)tlist->Nodes[pos]; } //删除线性表指定位置的元素 _declspec(dllexport) SeqListNode* SeqList_Delete(SeqList* list, int pos){ int ERRO_MSG = 0, i = 0; unsigned int ret = 0; if (list == NULL) { ERRO_MSG = -1; printf("list==NULL 传入参数不可以为空! erro msg:%d\n", ERRO_MSG); return NULL; } TSeqList *tlist = (TSeqList *)list; //判断获取位置 if (pos<0 || pos>tlist->length) { ERRO_MSG = -2; printf("没有该位置的元素! erro msg:%d\n", ERRO_MSG); return NULL; } ret = tlist->Nodes[pos]; for (i = pos; i < tlist->length-1; i++) { tlist->Nodes[i] = tlist->Nodes[i+1]; } //最后元素置零--这一步也可以不要 memset(tlist->Nodes + tlist->length - 1, 0, sizeof(unsigned int)); //元素个数减1 tlist->length--; return (SeqListNode*)ret; }
//顺序线性表测试代码 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include"SeqList.h" typedef struct _Student{ char name[30]; int age; }Student; void Test(){ Student s1, s2, s3,s4,s5; int numx = 0, i = 0,ret=0; strcpy(s1.name, "小米"); s1.age = 11; strcpy(s2.name, "小刚"); s2.age = 12; strcpy(s3.name, "小红"); s3.age = 10; strcpy(s4.name, "啸天"); s4.age = 13; strcpy(s5.name, "莲华"); s5.age = 12; //线性表指针 SeqList *list = NULL; //创建线性表 list = SeqList_Create(10); //获取线性表容量 numx = SeqList_Capacity(list); printf("线性表容量是%d\n", numx); //插入元素 SeqList_Insert(list, (SeqListNode *)&s1, 0); SeqList_Insert(list, (SeqListNode *)&s2, 0); SeqList_Insert(list, (SeqListNode *)&s3, 0); SeqList_Insert(list, (SeqListNode *)&s4, 0); SeqList_Insert(list, (SeqListNode *)&s5, 0); //获取元素个数 numx = SeqList_Length(list); //打印所有元素 for (i = 0; i < numx; i++) { Student *temp = (Student *)SeqList_Get(list, i); printf("我的名字是%s;我的年龄是%d\n", temp->name, temp->age); } //删除线性表中的数据 Student *delem = (Student *)SeqList_Delete(list, 1); printf("我被删除了,我的名字是%s;我的年龄是%d\n", delem->name, delem->age); //销毁线性表 ret=SeqList_Destroy(&list); if (ret==0)printf("线性表销毁成功!\n"); } void main(){ Test(); system("pause"); }