顺序链表API

参考资料,编写目的是为了锻炼自己对于函数的封装以及基本数据结构的学习能力,记录之。

能力不足,希望以后继续改进!!!


头文件声明如下:

#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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值