//头文件
//纯手打,未验证
#ifndef _ZCH_SEQLIST_H_
#define _ZCH_SEQLIST_H_
typedef void SeqList; // 数据封装
typedef void SeqListNode;
//创建一个顺序存储的线性表
SeqList* Create1(int capacity);
int Create2(SeqList** handle, int capacity);
//向顺序存储的线性表的pos位置插入一个新元素node
int Insert(SeqList* list, SeqListNode* node, int pos);
//获取顺序存储的线性表的元素个数
int Length(SeqList* list);
//获取pos位置的元素
SeqListNode* GetEle(SeqList* list, int pos);
//删除pos位置的元素,成功返回该元素的地址,失败返回NULL
SeqListNode* DeleteEle(SeqList* list, int pos);
//销毁一个顺序存储的线性表
void Destory(SeqList* list);
//清空顺序存储的线性表
void Clear(SeqList* list);
//查看顺序存储的线性表的容量
int Capacity(SeqList* list);
//实现
#include "zch_seqlist.h"
//线性表顺序存储的抽象
typedef struct _tag_SeqList
{
int capacity; // 容量
int length; //长度
unsigned int *node; //相当维护了一个unsigned int array[capacity]数组
}TSeqList;
SeqList* Create1(int capacity)
{
TSeqList* ret = NULL;
if(capacity < 0)
{
return NULL;
}
//note:一步到位
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(unsigned int) * capacity);
if(ret == NULL)
{
return NULL;
}
//为自己的内存负责
memset(ret, 0, sizeof(TSeqList) + sizeof(unsigned int) * capacity);
//指针的灵活运用
ret->node = (unsigned int *)(ret + 1);
ret->capacity = capacity;
ret->length = 0;
return ret;
}
//二级指针的运用
int Create2(SeqList** handle, int capacity)
{
TSeqList* ret = NULL;
if(capacity < 0)
{
return -1;
}
//note:一步到位
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(unsigned int) * capacity);
if(ret == NULL)
{
return -1;
}
//为自己的内存负责
memset(ret, 0, sizeof(TSeqList) + sizeof(unsigned int) * capacity);
//指针的灵活运用
ret->node = (unsigned int *)(ret + 1);
ret->capacity = capacity;
ret->length = 0;
*handle = ret;
return 0;
}
void Destory(SeqList* list)
{
if(list == NULL)
{
return;
}
//一步到位的分配,就可以一下子都free掉,很方便吧
free(list);
return;
}
void Clear(SeqList* list)
{
TSeqList* tList = (TSeqList*)list;
if(tList == NULL)
{
return;
}
//线性表清零就是将你底层维护的长度清零
tList->length = 0;
return ;
}
int Length(SeqList* list)
{
TSeqList* tList = (TSeqList*)list;
if(tList == NULL)
{
return -1;
}
return tList->length;
}
int Capacity(SeqList* list)
{
TSeqList* tList = (TSeqList)list;
if(tList == NULL)
{
return -1;
}
return tList->capacity;
}
int Insert(SeqList* list, SeqListNode* node, int pos)
{
int i = 0;
TSeqList* tList = (TSeqList*)list;
if(tList == NULL || node == NULL)
{
return -1;
}
//满了
if(tList->length >= tList->capacity)
{
return -1;
}
if(pos < 0 || pos >= tList->capacity)
{
return -1;
}
if(pos >= tList->length)
{
pos = tList->length;
}
//循环后移元素
for(i = tList->length; i > pos; i--)
{
tList->node[i] = tList->node[i-1];
}
//charu新元素的地址
tList->node[pos] = (unsigned int)node;
tList->length++;
return 0;
}
SeqListNode* GetEle(SeqList* list, int pos)
{
TSeqList* tList = (TSeqList*)list;
if(tList == NULL || pos < 0 || pos >= tList->length)
{
return NULL;
}
return (SeqListNode*)(tList->node[pos]);
}
SeqListNode* Delete(SeqList* list, int pos)
{
int i = 0;
TSeqList* tList = (TSeqList*)list;
SeqListNode* ele = NULL;
if(tList == NULL || pos < 0 || pos >= tList->length)
{
return NULL;
}
//先将被删元素的地址保存一份
ele = (SeqListNode*)(tList->node[pos]);
for(i = pos + 1; i < tList->length; i++)
{
tList->node[i-1] = tList->node[i];
}
tList->length--;
return ele;
}