线性表的顺序存储

#include <stdio.h>
#include <malloc.h>
#include "SeqList.h" 


typedef unsigned int TSeqListNode;


typedef struct _tag_SeqList
{
int capacity;
int length;
TSeqListNode* node;
} TSeqList;


SeqList* SeqList_Create(int capacity)
{
TSeqList* ret = NULL;

if( capacity >= 0 )
{
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
}

if( ret != NULL )
{
ret->capacity = capacity;
ret->length = 0;
ret->node = (TSeqListNode*)(ret + 1);
}

return ret;
}


void SeqList_Destroy(SeqList* list)
{
free(list);
}


void SeqList_Clear(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;

if( sList != NULL)
{
sList->length = 0;
}
}


int SeqList_Length(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;

if( sList != NULL)
{
ret = sList->length;
}

return ret;
}


int SeqList_Capacity(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;

if( sList != NULL)
{
ret = sList->capacity;
}

return ret;
}


SeqListNode* SeqList_Delete(SeqList* list,int pos)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = SeqList_Get(list,pos);
int i=0;

if( ret )
{
for(i=pos+1;i < sList->length;i++)
{
sList->node[i-1] = sList->node[i];
}
sList->length--;
}

return ret; 
}


int SeqList_Insert(SeqList* list,SeqListNode* node,int pos)
{
TSeqList* sList = (TSeqList*)list;
int ret = (sList != NULL);
int i = 0;

ret = ret && (sList->length + 1 <= sList->capacity);
ret = ret && (0 <= pos );

if( ret )
{
if(pos > sList->length)
{
pos = sList->length;
}
for(i=sList->length;i>pos;i--)
{
sList->node[i] = sList->node[i-1];
}
sList->node[i] = (TSeqListNode)node;

sList->length++;
}

return ret;



SeqListNode* SeqList_Get(SeqList* list,int pos)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;

if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
{
ret = (SeqListNode*)(sList->node[pos]); 
}

return ret;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值