链表学习笔记 --- 循环链表

本文介绍了一个简单的循环链表实现,包括创建、销毁、插入、删除等基本操作,并通过具体示例展示了如何使用这些函数。

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

h头文件

cpp实现文件

主函数文件


这次代码写的是循环链表,用的是节点连接。

循环链表相比与一般链表要注意的地方就是 头部插入和尾部插入,需要保持好链表的环状结构即可



#ifndef _CRICLELIST_H_
#define _CRICLELIST_H_

typedef void CircleList;
typedef struct _tag_CircleListNode{
	struct _tag_CircleListNode *next;
} CircleListNode;

CircleList* CircleList_Create();

void CircleList_Destroy(CircleList* list);

int CircleList_Clear(CircleList* list);

int CircleList_Length(CircleList* list);

CircleListNode* CircleList_Get(CircleList* list, int pos);

int CircleList_Insert(CircleList* list, CircleListNode *node, int pos);

CircleListNode* CircleList_Delete(CircleList *list, int pos);

#endif


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

#include"CircleList.h"

typedef struct _tag_CircleList
{

	CircleListNode header;
	CircleListNode *slider;
	int length;

} TCircleList;


CircleList* CircleList_Create()
{
	TCircleList *ret = (TCircleList*)malloc(sizeof(TCircleList));
	if (ret == NULL)
	{
		return NULL;
	}

	memset(ret, 0, sizeof(TCircleList));
	ret->header.next = NULL;
	ret->length = 0;
	ret->slider = NULL;
	return ret;
}

void CircleList_Destroy(CircleList* list)
{
	if (list == NULL)
		return;
	free(list);
	return;
}

int CircleList_Clear(CircleList* list)
{
	TCircleList *tList = NULL;
	tList = (TCircleList*)list;
	if (tList == NULL)
	{
		return -1;
	}
	tList->length = 0;
	return 0;
}

int CircleList_Length(CircleList* list)
{
	TCircleList *tList = NULL;
	tList = (TCircleList*)list;
	if (tList == NULL)
	{
		return -1;
	}
	 return tList->length;
}

int CircleList_Insert(CircleList* list, CircleListNode *node, int pos)
{
	TCircleList *tList = NULL;
	int ret = 0, i = 0;
	if (list == NULL || pos < 0)
	{
		ret = -1;
		return ret;
	}
	tList = (TCircleList*)list;
	if (pos > tList->length)
	{
		pos = tList->length;
	}
	//移动到要插入的位置
	CircleListNode *current = (CircleListNode*)&(tList->header);
	for (i = 0; i < pos && current->next != NULL; i++)
	{
		current = current->next;
	}
	//插入节点
	node->next = current->next;
	current->next = node;
	tList->length++;

	if (tList->length == 1)
	{
		tList->slider = node;
	}
	
	//如果是头部插入 需要找到最后一个元素, 让最后一个元素的next 指向头节点
	if (current == (CircleListNode*)&(tList->header))
	{
		CircleListNode* tail = NULL;
		tail = CircleList_Get(tList, tList->length - 1 );
		tail->next = current;
	}
	return ret;
}

CircleListNode* CircleList_Get(CircleList* list, int pos)
{
	TCircleList *tList = NULL;
	int i = 0;
	tList = (TCircleList*)list;
	if (tList == NULL || pos < 0)
	{
		return NULL;
	}

	CircleListNode *current = (CircleListNode*)&(tList->header);
	for (i = 0; i <= pos; i++)
	{
		current = current->next;
	}
	return current;
}

CircleListNode* CircleList_Delete(CircleList *list, int pos)
{
	TCircleList *tList = NULL;
	CircleListNode *ret = NULL;
	int i = 0;
	if (list == NULL || pos < 0 || tList->length == 0)
	{
		
		return ret;
	}
	tList = (TCircleList*)list;
	if (pos > tList->length)
	{
		pos = tList->length;
	}

	CircleListNode *current = (CircleListNode*)&(tList->header);
	for (i = 0; i < pos; i++)
	{
		current = current->next;
	}

	
	ret = current->next;
	current->next = ret->next;
	

	////如果删除了第一个元素
	//if (current == (CircleListNode*)&(tList->header))
	//{
	//	current = ret->next;
	//	CircleListNode* tail = NULL;
	//	tail = CircleList_Get(tList, tList->length-1);
	//	tail->next = current;
	//}

	tList->length--;
	ret->next = NULL;
	
	return ret;
}

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

typedef struct _tag_Teacher
{
	CircleListNode node;
	char name[64];
	int age;

}Teacher;

int main()
{
	CircleList *list = NULL;
	Teacher *temp = NULL;
	Teacher t1, t2, t3;
	int i = 0;


	t1.age = 1;
	t2.age = 2;
	t3.age = 3;

	list = CircleList_Create();

	CircleList_Insert(list, (CircleListNode*)&t1, 0);
	CircleList_Insert(list, (CircleListNode*)&t2, 0);
	CircleList_Insert(list, (CircleListNode*)&t3, 0);

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

	}
	int size = CircleList_Length(list);

	for(i = 0; i < size; i++)
	{
		temp = (Teacher*)CircleList_Delete(list, 0);
		printf("the age of teacher is %d \n", temp->age);
	}
	

	CircleList_Destroy(list);
	system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值