链表学习笔记 -- 顺序链表

本文介绍了一种使用整型数组保存数据指针的顺序链表实现方法,并提供了完整的C语言代码示例。通过这种方式,可以有效地将链表结构独立于具体的数据类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    最近重新学习数据结构 看到了 一段 比之以往 要好的两段代码,原因是能在不用模版的情况下将链表独立出来,不用和数据粘在一起。

顺序链表的主要思想就是使用一个整型数组来保存数据的指针。
在创建内存的时候如图, 用后面的数组来保存指针。



#ifndef  __MY_SEQLIST_H__ 
#define __MY_SEQLIST_H__


//大量使用 给void重新起个名字 封装使用的数据j 让其对使用者是透明的
typedef void SeqList;
typedef void SeqListNode;

SeqList* SeqList_Create(int capacity);

int SeqList_Create2(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  //__MY_SEQLIST_H__

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

typedef struct _tag_SeqList
{

	int capacity;
	int length;
	unsigned int *node; //unsigned int array[capacity]用来装数据类型

}TSeqList;
int SeqList_Create2(SeqList **handle, int capacity)
{
	int iRet = 0;
	TSeqList *ret = NULL;
	if (handle == NULL || capacity < 0)
		return -1;

	
	ret = (TSeqList *)malloc(sizeof(TSeqList)+sizeof(unsigned int)*capacity);// 后面是指针部分

	if (ret == NULL)
	{
		return -1;
	}

	memset(ret, 0, sizeof(TSeqList)+sizeof(unsigned int)*capacity);

	ret->node = (unsigned int *)(ret + 1); //ret向后跳sizeof(TSeqList)是node指向 指针部分

	ret->capacity = capacity;
	ret->length = 0;

	*handle = ret;

	return iRet;
}

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向后跳sizeof(TSeqList)是node指向 指针部分

	ret->capacity = capacity;
	ret->length = 0;
	return ret;


}

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

void SeqList_Clear(SeqList* list)
{
	TSeqList *tList = NULL;
	if (list == NULL)
		return;
	tList = (SeqList *)list;
	tList->length = 0;
	return;
}

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

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

}

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


	if (tList->capacity == tList->length)
		return -1;


	//参数错误判断
	if (list == NULL || pos < 0 || node == NULL || pos > tList->capacity)
	{
		return -1;
	}
	//
	if (pos >= tList->length)
	{
		pos = tList->length;
	}

	
	int i = 0;

	for (i = tList->length; i>pos; i--)
	{
		//数组元素后移
		tList->node[i] = tList->node[i - 1];
	}
	//结束循环的时候,pos正好是要插入的位置
	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 (pos < 0 || list == NULL || pos > tList->length)
		return NULL;

	ret = (SeqListNode *)tList->node[pos];
	return ret;
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	SeqListNode			*ret = NULL;
	TSeqList			*tList = NULL;
	int					i = 0;

	tList = (TSeqList*)list;

	if (list == NULL || pos<0 || pos > tList->length)
		return NULL;
	//保存删除的内容
	ret = (SeqListNode*)tList->node[pos];//没什么用啊

	//通过内存覆盖来删除
	for (i = pos + i; i < tList->length; i++)
	{
		tList->node[i - 1] = tList->node[i];
	}
	tList->length--;

	return ret;
}

#include<stdio.h>
#include<stdlib.h>
#include"seqlist.h"






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

int main()
{
	SeqList *list = NULL;

	//list = SeqList_Create(10);
	SeqList_Create2(&list, 10);
	Teacher t1, t2, t3;
	t1.age = 1;
	t2.age = 2;
	t3.age = 3;


	SeqList_Insert(list, (SeqListNode*)&t1, 0);
	SeqList_Insert(list, (SeqListNode*)&t2, 0);
	SeqList_Insert(list, (SeqListNode*)&t3, 0);

	for (int i = 0; i < SeqList_Length(list); i++)
	{
		Teacher *temp = (Teacher *)SeqList_Get(list, i);
		if (temp != NULL)
		{
			printf("the teacher age is %d \n", temp->age);
		}
	}

	for (int i = 0; i < SeqList_Length(list); i++)
	{
		SeqList_Delete(list, i);
	}

	SeqList_Destroy(list);

	

	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值