顺序线性表——通过存放地址,实现顺序存储--C语言简单实现

#define  _CRT_SECURE_NO_WARNINGS 
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include"seqlist.h"

typedef struct Teacher
{
	char name[32];
	int age;
}Teacher;

void main()
{
	int ret = 0;
	int i = 0;
	SeqList *list;//线性表头指针-------------------

	Teacher t1, t2, t3;
	t1.age = 11;
	t2.age = 22;
	t3.age = 33;

	list = SeqList_Create(10);
	//思考1:如何实现链表的api(链表的算法)和具体的数据分离
	//思考2:链表库(业务逻辑)测试程序的业务逻辑  结点的生命周期  归谁管

	ret = SeqList_Insert(list, (SeqListNode*)&t1, 0);//头插法
	ret = SeqList_Insert(list, (SeqListNode*)&t2, 0);//头插法
	ret = SeqList_Insert(list, (SeqListNode*)&t3, 0);//头插法

	//遍历链表
	for (i = 0;i < SeqList_Length(list);i++)
	{
		Teacher*tmp = (Teacher*)SeqList_Get(list, i);
		if (NULL == tmp)
		{
			printf("func SeqList_Get()  err:%d \n", ret);
			return;
		}
		printf("age:%d\n", tmp->age);
	}

	//销毁链表
	while (SeqList_Length(list) > 0)
	{
		Teacher *tmp = (Teacher*)SeqList_Delete(list, 0);
		if (NULL == tmp)
		{
			printf("func SeqList_Get() err:%d \n", ret);
			return;
		}
		printf("age:%d \n", tmp->age);
	}
	SeqList_Destroy(list);

	system("pause");
}

#ifndef _SEQLIST_H_
#define _SEQLIST_H_

typedef void SeqList;
typedef void SeqListNode;

typedef struct _tag_SeqList
{
	int capacity;
	int length;
	//int *node[100];
	int*  *node;//存放int*  的  指针----即  指向一个元素都是int*的数组
}TSeqList;
//链表  创建
SeqList* SeqList_Create(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_H_

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

//创建1
SeqList* SeqList_Create(unsigned int capacity)
{
	if (0 ==capacity)
	{
		return NULL;
	}
	int ret=0;
	TSeqList *tmp = NULL;
	tmp = (TSeqList*)malloc(sizeof(TSeqList));
	if (NULL == tmp)
	{
		ret = 1;
		printf("func SeqList_Create() err:%d \n", ret);
		return NULL;
	}
	memset(tmp, 0, sizeof(TSeqList));
	tmp->capacity = capacity;
	tmp->length = 0;


	tmp->node = (int **)malloc(sizeof(void *)*capacity);
	if (NULL == tmp->node)
	{
		ret = 2;
		printf("func SeqList_Create() err:%d \n", ret);
		return NULL;
	}
	memset(tmp->node, 0, sizeof(void*)*capacity);

	return tmp;

}
int SeqList_Create2(int capacity, SeqList** handle)
{
	if (NULL == handle)
	{
		return -1;
	}
	int ret = 0;
	TSeqList *tmp = NULL;
	tmp = (TSeqList*)malloc(sizeof(TSeqList));
	if (NULL == tmp)
	{
		ret = 1;
		printf("func SeqList_Create2() err;%d \n", ret);
		return ret;
	}
	memset(tmp, 0, sizeof(TSeqList));
	tmp->capacity = capacity;
	tmp->length = 0;
	tmp->node = (int**)malloc(sizeof(void*)*capacity);
	if (NULL == tmp->node)
	{
		ret = 2;
		printf("func SeqList_Create2() err;%d \n", ret);
		return ret;
	}
	*handle = tmp;
	return ret;
}

//销毁

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

//清空

void SeqList_Clear(SeqList* list)
{
	TSeqList *tmp = NULL;
	if (NULL == list)
	{
		return;
	}
	tmp=(TSeqList*)list;
	tmp->length = 0;
	memset(tmp->node, 0, tmp->capacity * sizeof(void*));
	return;
}

//链表  长度

int SeqList_Length(SeqList* list)
{
	TSeqList* tmp = NULL;
	if (NULL == list)
	{
		return -1;
	}
	tmp = (TSeqList*)list;
	return tmp->length;
}

//链表  容量

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

//链表  插入

int SeqList_Insert(SeqList *list, SeqListNode *node, int pos)
{
	TSeqList  *tList = NULL;
	int i = 0;
	if (NULL == list || NULL == node)
	{
		return -1;
	}
	tList = (TSeqList*)list;
	//如果满了
	if (tList->length >= tList->capacity)
	{
		return -2;
	}

	//pos位置的容错处理
	if (pos > tList->length)
	{
		pos = tList->length;
	}
	for (i = tList->length;i > pos;i--)
	{
		tList->node[i] = tList->node[i - 1];
	}
	tList->node[i] = (int*)node;
	tList->length++;
	return 0;
}

//获取某一个位置的链表节点
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
	TSeqList *tList = NULL;
	SeqListNode *tmp = NULL;
	tList = (TSeqList*)list;
	if (NULL == list || pos < 0 || pos >= tList->length)
	{
		return NULL;
	}
	tmp = tList->node[pos];
	return tmp;
}

//删除某一个位置的结点
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	int i = 0;
	TSeqList *tList = NULL;
	SeqListNode*tmp = NULL;
	tList = (TSeqList*)list;
	if (NULL == list || pos < 0 || pos >= tList->length)
	{
		return NULL;
	}
	tmp = tList->node[pos];
	for (i = pos + 1;i < tList->length;i++)
	{
		tList->node[i - 1] = tList->node[i];
	}
	tList->length--;
	return tmp;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值