#include"SeqList.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma region 定义线性表的结构
typedef unsigned int TSeqListNode;
typedef struct _Tag_SeqList
{
int length;
int capacity;
TSeqListNode* node;
}TSeqList;
#pragma endregion
#pragma region 创建线性表
SeqList *SeqList_Create(int Capacity)
{
TSeqList* TList = NULL;
TList = (TSeqList*)malloc(sizeof(TSeqList)+sizeof(TSeqListNode)*Capacity);
if (TList == NULL)/*线性表内存分配不成功*/
{
printf("func selistcreate() err");
}
else
{
TList->capacity = Capacity;
TList->length = 0;
TList->node = (TSeqListNode*)(TList + 1);/*表示线性表的header指向下一个指针*/
}
return TList;
}
#pragma endregion
#pragma region 销毁线性表
void SeqList_Destory(SeqList *TList)
{
free(TList);
}
#pragma endregion
#pragma region 清空线性表
void SeqList_Clear(SeqList* TList)
{
TSeqList* tmpList = (TSeqList*)TList;
if (tmpList == NULL)
{
printf("线性表为空,无需清空");
}
tmpList->length = 0;
/*注意此时:清空元素并不等于释放node指向的内存,只是将元素进行清空,放入别的元素*/
return;
}
#pragma endregion
#pragma region 获取线性表的长度
int SeqList_Length(SeqList* TList)
{
TSeqList* tmpList = NULL;
tmpList = (TSeqList*)TList;
if (tmpList == NULL)
{
printf("线性表为空");
}
return tmpList->length;
}
#pragma endregion
#pragma region 获取线性表的容量
int SeqList_Capacity(SeqList* TList)
{
TSeqList* tmpList = NULL;
tmpList = (TSeqList*)TList;
if (tmpList == NULL)
{
printf("线性表为空");
}
return tmpList->capacity;
}
#pragma endregion
#pragma region 向线性表中的指定位置插入元素
int SeqList_Insert(SeqList* TList, SeqListNode *item, int Pos)
{
TSeqList* tmpList = NULL;
tmpList = (TSeqList*)TList;
if (tmpList == NULL || item == NULL || Pos<0)
{
printf("不能完成向指定位置插入元素的操作");
}
int result = (tmpList != NULL) && ((Pos >= 0) && (Pos <= tmpList->capacity)) && (item!= NULL);
result = result && ((tmpList->length + 1) <= (tmpList->capacity));/*这句话保证了加入的元素*/
if (Pos>tmpList->length)
{
Pos = tmpList->length;
}
for (int i = tmpList->length; i>Pos; i)
{
*(tmpList->node + i) = *(tmpList->node);//每一个node地址存放item的指针非数据
}
*(tmpList->node + Pos) = (TSeqListNode*)item;//每一个node地址存放item的指针非数据
tmpList->length++;
return result;
}
#pragma endregion
#pragma region 获取线性表指定位置的元素
SeqListNode* SeqList_Get(SeqList* TList, int Pos)
{
TSeqList* tmpList = NULL;
tmpList = (TSeqList*)(TList);
if (tmpList == NULL)
{
printf("链表为空,无法返回指定元素");
}
TSeqListNode* tmpNode = NULL;
if ((Pos<=tmpList->length)&&(Pos>=0))
{
tmpNode = (TSeqListNode*)(tmpList + Pos);
}
return tmpNode;
}
#pragma endregion
#pragma region 删除指定位置的元素
SeqListNode* SeqList_Delete(SeqList* TList, int Pos)
{
TSeqList* tmpList = NULL;
tmpList = (TSeqList*)TList;
int result = (tmpList != NULL) && (Pos >= 0) && (Pos <= tmpList->length);
TSeqListNode* tmpNode = NULL;
if (result)
{
tmpNode = (TSeqListNode*)SeqList_Get((SeqList*)tmpList,Pos);//指向指定位置的元素 将指定位置的元素转为TeqListNode类型
int i = 0;
for ( i = Pos; i < tmpList->length; i++)
{
*(tmpList->node+i-1) =*(tmpList->node+i);/*注意这个是替换node中的元素而非node
所以如果写成*(tmpList+i)等价于将node元素替换,这是不对的
替换的是node中的item元素
因此*(tmpList+i-1) =*(tmpList+i)等价于node之间赋值,
想都想不明白肯定走不通*/
}
*(TSeqListNode*)(tmpList + i) = (TSeqListNode)NULL;//这句话说明将TSeqListNode*这个指针所指向的值转换为null的TSeqListNode类型
tmpList->length--;
}
return tmpNode;
}
#pragma endregion