顺序链表的实现

写了一下顺序链表,为了防止自己忘记,于是乎写在博客里方便查看。

程序中的容错处理做的不好,是我懒了,就简单处理了一下

下面才给出这儿一套API函数

头文件:

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef void SeqList;
typedef void SeqListNode;

SeqList * SeqList_Create(int capacity);//创建链表 Capacity 为链表的容量

void SeqList_Destroy(SeqList* list);  //析构链表 FREE内存

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

然后是头文件中函数的实现:

#include "seqlist.h"


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


SeqList * SeqList_Create(int capacity)
{
	if (capacity == 0)
	{
		return NULL;
	}
	TSeqList *list = NULL;
	list = (TSeqList *)malloc(sizeof(TSeqList));
	if (list == NULL)
	{
		return NULL;
	}
	memset(list, 0, sizeof(TSeqList));


	list->capacity = capacity;
	list->length = 0;

	list->node = (unsigned int *)malloc(sizeof(unsigned int )*capacity);
	return (SeqList *)list;

}

void SeqList_Destroy(SeqList* list)
{
	if (list == NULL)
	{
		return;
	}

	TSeqList * tmp = NULL; 
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return;
	}
	if (tmp->node != NULL)
	{
		free(tmp->node);
	}
	free(list);
	return;
}

void SeqList_Clear(SeqList* list)
{
	if (list == NULL)
	{
		return;
	}

	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return;
	}
	tmp->length = 0;
	return;

}

int SeqList_Length(SeqList* list)
{
	if (list == NULL)
	{
		return 0;
	}

	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return 0;
	}
	int ret = 0;
	ret = tmp->length;
	return ret;
}

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

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
	int ret = 0;
	if (list == NULL || node == NULL  || pos<0)
	{
		ret = -1;
		return ret;
	}

	
	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		ret = -2;
		return ret;
	}

	if (tmp->length >= tmp->capacity)//检查是否已满
	{
		ret = -3;
		return ret;
	}

	if (pos >= tmp->length)//位置修正
	{
		pos = tmp->length;
	}

	for (int i = tmp->length; i > pos; i--)//数组中元素后移
	{
		tmp->node[i] = tmp->node[i - 1];
	}
	tmp->node[pos] = (unsigned int)node;
	tmp->length++;

	return ret;
}

SeqListNode* SeqList_Get(SeqList* list, int pos)
{
	if (list == NULL  || pos<0)
	{
		return NULL;
	}


	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return NULL;
	}


	return (SeqListNode*)(tmp->node[pos]);
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	if (list == NULL || pos<0)
	{
		return NULL;
	}
	
	TSeqList * tmp = NULL;
	tmp = (TSeqList *)list;
	if (tmp == NULL)
	{
		return NULL;
	}

	SeqListNode * ret = (SeqListNode *)tmp->node[pos];//缓存删除的节点
	for (int i = pos+1; i < tmp->length; i++)
	{
		tmp->node[i-1] = tmp->node[i];
	}
	(tmp->length)--;
	return ret;
}

下面是简单的测试程序:

#include "seqlist.h"
typedef struct Teacher
{
	int age;
	char name[64];
}Teacher;
void main()
{
	int ret = 0;
	SeqList *List = NULL;
	Teacher t1, t2, t3;
	t1.age = 10;
	t2.age = 11;
	t3.age = 12;
	List = SeqList_Create(10);
	if (List == NULL)
	{
		printf("ERROR!!!");
	}

	int length = SeqList_Length(List);
	printf("%d\n", length);

	ret = SeqList_Insert(List, (SeqListNode*) &t1, 0);
	if (ret != 0)
	{
		printf("ERROR!!!%d",ret);
	}
	ret = SeqList_Insert(List, (SeqListNode*)&t2, 0);
	if (ret != 0)
	{
		printf("ERROR!!!%d", ret);
	}

	ret = SeqList_Insert(List, (SeqListNode*)&t3, 0);
	if (ret != 0)
	{
		printf("ERROR!!!%d", ret);
	}

	length = SeqList_Length(List);
	printf("%d\n", length);

	int capacity = SeqList_Capacity(List);
	printf("%d\n", capacity);

	for (int i = 0; i < length; i++)
	{
		Teacher *tmp = (Teacher *)SeqList_Get(List, i);
		if (tmp == NULL)
		{
			printf("ERROR!!!");
		}
		else
		{
			printf("%d\n", tmp->age);
		}
	}

	Teacher *t4 =(Teacher *) SeqList_Delete(List, 0);
	printf("%d", t4->age);

	SeqList_Destroy(List);

	system("pause");
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值