【数据结构】线性表的顺序存储API及实现
数据类型及API声明
线性表的顺序存储是指各元素按顺序依次排列存储在一段连续内存的数据结构。
//线性表数据类型
typedef void LinearList;
//线性表结点数据类型
typedef void LinearListNode;
//线性表的表头数据类型
typedef struct _LinearList _LinearListHead;
//创建线性表并返回线性表句柄
LinearList* LinearList_Create(int length);
//销毁线性表(释放内存)
void LinearList_Destroy(LinearList* lList);
//清空线性表(不释放内存)
void LinearList_Clear(LinearList* lList);
//返回线性表长度(元素个数)
int LinearList_Length(LinearList* lList);
//插入元素
int LinearList_Insert(LinearList* lList, LinearListNode* node, int pos);
//返回pos处的元素
LinearListNode* LinearList_Get(LinearList* lList, int pos);
//删除一个结点并返回结点值
LinearListNode* LinearList_Delete(LinearList* lList, int pos);
线性表的API实现
定义一个表头结构体
- 表头含义:该表头存储了线性表的最大长度,当前长度(元素个数),元素内存起始地址。
struct _LinearList //这是一个线性表头
{
int length; //总长度
int cur_length; //当前长度
int* node; //线性表的元素存放在这个内存中
};
创建线性表并返回线性表句柄
- 函数功能:创建一个线性表,根据传入的长度分配内存,并返回线性表的句柄
- 函数参数:length 线性表长度
- 函数返回:创建好的线性表句柄
LinearList* LinearList_Create(int length)
{
_LinearListHead* head;
head = (_LinearListHead*)malloc(sizeof(_LinearListHead));
if (head == NULL)
{
printf("head = (_LinearListHead*)malloc(sizeof(_LinearListHead)) err\n");
return NULL;
}
memset(head, 0, sizeof(_LinearListHead));
head->length = length;
head->cur_length = 0;
head->node = (int*)malloc(sizeof(int) * length);
if (head->node == NULL)
{
printf("head->node = (int*)malloc(sizeof(int) * length) err\n");
return NULL;
}
return head;
}
销毁线性表
- 函数功能:销毁线性表并释放所有内存
- 函数参数:lList 线性表的句柄
- 函数返回:无
void LinearList_Destroy(LinearList* lList)
{
_LinearListHead* head = NULL;
if (lList == NULL)
{
printf("LinearList* lList = NULL\n");
return;
}
head = (_LinearListHead*)lList;
if (head->node != NULL)
{
free(head->node);
}
free(head);
}
清空线性表
- 函数功能:清空线性表仅仅把当前长度置为0,不会释放内存,相当于把线性表恢复到刚创建的状态
- 函数参数:lList 线性表句柄
- 函数返回:无
void LinearList_Clear(LinearList* lList)
{
_LinearListHead* head = NULL;
if (lList == NULL)
{
printf("LinearList* lList = NULL\n");
return;
}
head = (_LinearListHead*)lList;
memset(head->node, 0, sizeof(int) * head->length);
head->cur_length = 0;
}
返回线性表当前长度
- 函数功能:返回线性表中当前存储的元素个数
- 函数参数:lList 链表句柄
- 函数返回:链表元素个数
int LinearList_Length(LinearList* lList)
{
int ret = 0;
_LinearListHead* head = NULL;
if (lList == NULL)
{
ret = -1;
printf("LinearList* lList = NULL err: %d\n", ret);
return ret;
}
head = (_LinearListHead*)lList;
return head->cur_length;
}
插入元素
- 函数功能:在给定位置插入一个元素
- 函数参数:lList 线性表句柄 node 要插入的元素 pos插入位置
- 函数返回:插入成功返回0
int LinearList_Insert(LinearList* lList, LinearListNode* node, int pos)
{
int ret = 0, i = 0;
_LinearListHead* head = NULL;
if ((lList == NULL) || (node == NULL) || (pos < 0))
{
ret = -1;
printf("(lList == NULL) || (node == NULL) || (pos < 0) err: %d\n", ret);
return ret;
}
head = (_LinearListHead*)lList;
if (head->cur_length >= head->length)
{
ret = head->length;
printf("The linear table is full\n");
return ret;
}
if (pos >= head->cur_length)
{
head->node[head->cur_length] = (int)node;
}
for (i = head->cur_length; i > pos; i--)
{
head->node[i] = head->node[i - 1];
}
head->node[i] = *(int*)node;
head->cur_length++;
return 0;
}
获取一个元素
- 函数功能:返回给定位置处的元素
- 函数参数:lList 线性表句柄 pos 要返回第几个元素
- 函数返回:要返回的元素
LinearListNode* LinearList_Get(LinearList* lList, int pos)
{
_LinearListHead* head = NULL;
if ((lList == NULL) || (pos < 0))
{
printf("(lList == NULL) || (pos < 0) err\n");
return NULL;
}
head = (_LinearListHead*)lList;
return (LinearListNode*)head->node[pos];
}
删除元素
- 函数功能:删除一个元素并返回该元素
- 函数参数:lList 线性表句柄 pos 删除元素的位置
- 函数返回:被删除的元素
LinearListNode* LinearList_Delete(LinearList* lList, int pos)
{
int i = 0;
_LinearListHead* head = NULL;
LinearListNode* temp_node = NULL;
if ((lList == NULL) || (pos < 0))
{
printf("(lList == NULL) || (pos < 0) err\n");
return NULL;
}
head = (_LinearListHead*)lList;
temp_node = (LinearListNode*)head->node[pos];
for (i = pos + 1; i < head->cur_length; i++)
{
head->node[i - 1] = head->node[i];
}
head->cur_length--;
return temp_node;
}
测试函数
#include "LinearList.h"
int main()
{
int i = 0;
LinearList* mList = NULL;
LinearListNode* pNode = NULL;
mList = LinearList_Create(10);
for (int i = 0; i < 10; i++)
{
int ret = 0;
int temp = i + 1;
ret = LinearList_Insert(mList, (LinearListNode*)&temp, 0);
if (ret != 0)
{
printf("LinearList_Insert() err: %d\n", ret);
return ret;
}
}
for (i = 0; i < 10; i++)
{
printf("第%d个元素值:%d\n", i, (int)LinearList_Get(mList, i));
}
pNode = LinearList_Delete(mList, 3);
printf("被删除元素:%d\n", (int)pNode);
printf("当前线性表长度:%d\n", LinearList_Length(mList));
i = 12;
i = LinearList_Insert(mList, (LinearListNode*)&i, 3);
if (i != 0)
{
printf("LinearList_Insert() err\n");
}
for (i = 0; i < 10; i++)
{
printf("第%d个元素值:%d\n", i, (int)LinearList_Get(mList, i));
}
LinearList_Destroy(mList);
system("pause");
return 0;
}
具体的代码资源已经上传在这个链接
线性表的顺序存储API实现以及测试函数https://download.csdn.net/download/qq_43471489/85040668