参考资料,编写目的是为了锻炼自己对于函数的封装以及基本数据结构的学习能力,记录之。
能力不足,希望以后继续改进!!!
头文件声明如下:
#pragma once #ifndef _SeqList_H_ #define _SeqList_H_ //准备数据类型的封装 typedef void SeqList; typedef void SeqListNode; //创建并且返回一个空的线性表(返回句柄或者内存空间) SeqList* LinkSeqList_Create(int SeqList_Capacity); //销毁一个线性表SeqList bool SeqList_Destroy(SeqList* SeqList); //将一个线性表SeqList中的所有元素清空, 线性表回到创建时的初始状态 bool SeqList_Clear(SeqList* SeqList); //返回一个线性表SeqList中的所有元素个数 int SeqList_Length(SeqList* SeqList); //向一个线性表SeqList的pos位置处插入新元素node int SeqList_Insert(SeqList* SeqList, SeqListNode* node, int pos); //获取一个线性表SeqList的pos位置处的元素 int SeqList_Get(SeqList* SeqList, int pos); //删除一个线性表SeqList的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 SeqListNode* SeqList_Delete(SeqList* SeqList, int pos); //获取线性表的容量 int SeqList_Capacity(SeqList* SeqList); #endif函数实现如下: #include <iostream> #include "SeqList.h" #define uint unsigned int typedef void SeqList; typedef void SeqListNode; //链表内部结构 typedef struct _SeqList { int capacity; int length; uint *node;//动态开辟数组内存 }TSeqList; //创建并且返回一个空的线性表 SeqList* LinkSeqList_Create_demo(int SeqList_Capacity) { /*SeqList*p_head = new SeqList [SeqList_Capacity]; if (p_head==NULL) { return NULL; } return p_head;*/ TSeqList *ret = NULL; if (SeqList_Capacity < 0) { return NULL; } ret = (TSeqList*)new TSeqList; if (ret == NULL) { return NULL; } memset(ret, 0, sizeof(TSeqList)); //ret->node = (uint *)new TSeqList[SeqList_Capacity * (uint)]; ret->node = (uint *)malloc(sizeof(uint)*SeqList_Capacity); if (ret->node == NULL) { return NULL; } memset(ret->node, 0, sizeof(uint)*SeqList_Capacity); ret->capacity = SeqList_Capacity; ret->length = 0; return ret; } SeqList* LinkSeqList_Create(int SeqList_Capacity) { TSeqList *ret = NULL; if (SeqList_Capacity < 0) { return NULL; } //ret = (TSeqList*)new TSeqList; //2.0:一次分配了两块内存 ret = (TSeqList*)malloc(sizeof(TSeqList)+sizeof(uint)*SeqList_Capacity); if (ret == NULL) { return NULL; } memset(ret, 0, sizeof(TSeqList)+sizeof(uint)*SeqList_Capacity); //ret向后跳SeqList的大小(指针的步长) ret->node = (uint*)(ret + 1); ret->capacity = SeqList_Capacity; ret->length = 0; return ret; } //销毁一个线性表SeqList bool SeqList_Destroy(SeqList* Seq_List) { if (Seq_List==NULL) { return false; } free(Seq_List); Seq_List = NULL; return true; } //将一个线性表SeqList中的所有元素清空, 线性表回到创建时的初始状态 bool SeqList_Clear(SeqList* Seq_List) { TSeqList *tList = NULL; if (Seq_List == NULL) { return false; } tList = (TSeqList *)Seq_List; tList->length = 0; return true; } //统计并返回一个线性表SeqList中的所有元素个数(长度) int SeqList_Length(SeqList* Seq_List) { TSeqList *q_tmp = NULL; if (Seq_List == NULL) { return -1; } q_tmp = (TSeqList *)Seq_List; return q_tmp->length; } //向一个线性表SeqList的pos位置处插入新元素node int SeqList_Insert(SeqList* SeqList, SeqListNode* node, int pos) { int i; TSeqList *q_tmp = NULL; q_tmp = (TSeqList *)SeqList; if (q_tmp == NULL) { return -1; } if (pos >=q_tmp->capacity||pos < 0) { return -2; } //优化的容错 if (pos >= q_tmp ->length) { pos = q_tmp->length; } //顺序插入 for (i = q_tmp->length; i > pos; i--) { q_tmp->node[i] = q_tmp->node[i-1]; } q_tmp->node[pos] = (uint)node; q_tmp->length++; return 0; } //获取一个线性表SeqList的pos位置处的元素 int SeqList_Get(SeqList* SeqList, int pos) { TSeqList *q_tmp = NULL; q_tmp = (TSeqList *)SeqList; if (q_tmp == NULL || pos<0||pos>=q_tmp->length) { return -1; } int ret = q_tmp->node[pos]; return ret; } //删除一个线性表SeqList的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 SeqListNode* SeqList_Delete(SeqList* SeqList, int pos) { int i=0; TSeqList *q_tmp = NULL; q_tmp = (TSeqList *)SeqList; if (q_tmp == NULL || pos<0 || pos >= q_tmp->length) { return NULL; } //提前缓存要删除的元素 SeqListNode* tmp = (SeqListNode*)q_tmp->node[pos]; for ( i = pos+1 ; i <q_tmp -> length; i++) { q_tmp->node[i - 1] = q_tmp->node[i]; } q_tmp->length--; return tmp; } //获取线性表的容量 int SeqList_Capacity(SeqList* Seq_List) { TSeqList *q_tmp = NULL; if (Seq_List == NULL) { return -1 ; } q_tmp = (TSeqList *)Seq_List; return q_tmp->capacity; }
