线性表的顺序存储


译者:Mrzhu007
日期:2018年04月13日
博客地址:金色世界


头文件seqlist.h

#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedf void SeqList;
typedf void SeqListNode;

SeqList* SeqList_Create(int capacity);
int SeqList_Create01(SeqList **handle, int capacity);
void SeqList_Destroy(SeqList* list);
void SeqList_Clear(SeqList* list);
int SeqList_Length(SeqList* list);
int SeqList_Capacity(SeqList* list);
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
SeqListNode* SeqList_Get(SeqList* list, int pos);
SeqListNode* SeqList_Delete(SeqList* list, int pos);
#endif

实现文件seqlist.cpp

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "seqlist.h"

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

SeqList* SeqList_Create_01(int capacity)
{
	TSeqList *ret = NULL;
	if(capacity < 0)
	{
		return NULL;
	}
	ret = (TSeqList *)malloc(sizeof(TSeqList));
	if(ret == NULL)
	{
		return NULL;
	}
	memset(ret, 0, sizeof(TSeqList));
	ret->node = (unsigned int *)malloc(sizeof(unsigned int)*capacity);
	if(ret->node == NULL)
	{
		return NULL;
	}
	memset(ret->node, 0, sizeof(unsigned int)*capacity);
	ret->capacity = capacity;
	ret->length = 0;
	return ret;
}

SeqList* SeqList_Create(int capacity)
{
	TSeqList *ret = NULL;
	if(capacity < 0)
	{
		return NULL;
	}
	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;
}
void SeqList_Destroy(SeqList* list)
{
	if(list == NULL)
	{
		return;
	}
	free(list);
	return;
}
void SeqList_Clear(SeqList* list)
{
	TSeqList *tList = NULL;
	if(list == NULL)
	{
		return;
	}
	tList = (TSeqList *)list;
	tList->length = 0;
	return;
}
int SeqList_Length(SeqList* list)
{
	TSeqList *tList = NULL;
	tList = (TSeqList *)list;
	if(list == NULL)
	{
		return -1;
	}
	return tList->length;
}

int SeqList_Capacity(SeqList* list)
{
	TSeqList *tList = NULL;
	tList = (TSeqList *)list;
	if(list == NULL)
	{
		return -1;
	}
	return tList->capacity;
}

itn SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
	int i = 0;
	TSeqList *tList = NULL;
	tList = (TSeqList *)list;

	if(list == NULL || node == NULL)
	{
		return -1;
	}
	if(tList->length >= tList->capactiy)
	{
		return -2;
	}
	if(pos < 0 || pos >= tList->capacity)
	{
		return -3;
	}
	if(pos >= tList->length)
	{
		pos = tList->length;
	}
	for(i = tList->length; i > pos; i--)
	{
		tList->node[i] = tList->node[i-1];
	}
	tList->node[pos] = (unsigned int)node;
	tList->length++;
	return 0;
}
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
	SeqListNode *ret = NULL;
	TSeqList *tList = NULL;
	tList = (TSeqList *)list;
	if(list == NULL || pos < 0 || pos >= tList->length)
	{
		return NULL;
	}
	ret = (SeqListNode*)tList->node[pos];
	return ret;
}
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	int i;
	TSeqList *tList = NULL;
	SeqListNode *ret = NULL;
	tLsit = (TseqList *)list;
	if(list == NULL || pos < 0 || pos >= tList->length)
	{
		return NULL;
	}
	ret = (SeqListNode *)tList->node[pos];
	for(i = pos + 1; i < tList->length; i++)
	{
		tList->node[i-1] = tList->node[i];
	}
	tList->length--;
	return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值