数据结构——单向不带头循环链表的C语言代码实现

系列文章目录

数据结构——顺序表的C语言代码实现
数据结构——八种链表的C语言代码实现
数据结构——栈的C语言代码实现


前言

单向不带头循环链表的C语言代码。


一、List.h

代码如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef int SLLDataType;
typedef struct SLListNode
{
	SLLDataType data;
	struct SLListNode* next;
}SLL;
//创建节点
SLL* SLLCreateNode(SLLDataType n);

//尾插
void SLLPushBack(SLL** pphead, SLLDataType n);

//尾删
void SLLPopBack(SLL** pphead);

//头插
void SLLPushFront(SLL** pphead, SLLDataType n);

//头删
void SLLPopFront(SLL** pphead);

//查找
SLL* SLLFind(SLL* phead, SLLDataType n);

//在指定节点后面插入
void SLLInsertBack(SLL* phead, SLLDataType n, SLLDataType insert);

//在指定节点前面插入
void SLLInsertFront(SLL** pphead, SLLDataType n, SLLDataType insert);

//指定删除某节点
void SLLDelete(SLL** pphead, SLLDataType n);

//更改节点
void SLLChange(SLL* phead, SLLDataType n, SLLDataType change);

//打印链表
void SLLPrint(SLL* phead);

//删除链表
void FreeSLL(SLL* phead);

二、List.c

代码如下:

#include"List.h"
SLL* tail = NULL;
SLL* SLLCreateNode(SLLDataType n)
{
	SLL* p = (SLL*)malloc(sizeof(SLL));
	p->data = n;
	p->next = NULL;
	return p;
}
//尾插法
void SLLPushBack(SLL** pphead, SLLDataType n)
{
	SLL* tem = SLLCreateNode(n);
	if (*pphead == NULL)
	{
		*pphead = tem;
		tail = tem;
		tail->next = *pphead;
	}
	else
	{
		tail->next = tem;
		tail = tem;
		tail->next = *pphead;
	}
}
//尾删法
void SLLPopBack(SLL** pphead)
{
	if ((*pphead)->next == *pphead)
	{
		printf("链表为空\n");
		return;
	}
	if ((*pphead)->next->next == *pphead)
	{
		free(tail);
		(*pphead)->next = *pphead;
		tail = *pphead;
	}
	else
	{
		SLL* tem = *pphead;
		while (tem->next != tail)
		{
			tem = tem->next;
		}
		tem->next = *pphead;
		free(tail);
		tail = tem;
	}
}
//头插法
void SLLPushFront(SLL * *pphead, SLLDataType n)
{
	SLL* insert = SLLCreateNode(n);
	insert->next = *pphead;
	*pphead = insert;
	tail->next = *pphead;
}
//头删法
void SLLPopFront(SLL** pphead)
{
	if ((*pphead)->next == *pphead)
	{
		printf("链表为空\n");
		return;
	}
	SLL* tem = (*pphead)->next;
	free(*pphead);
	*pphead = tem;
	tail->next = *pphead;
}
//查找节点
SLL* SLLFind(SLL* phead, SLLDataType n)
{
	if (phead->next == phead)
	{
		printf("链表为空\n");
		exit(-1);
	}
	SLL* tem = phead;
	if (tem->data == n)
	{
		return tem;
	}
	else
	{
		tem = phead->next;
		while (tem != phead)
		{
			if (tem->data == n)
				return tem;
			tem = tem->next;
		}
		return NULL;
	}
}
//在指定节点后面插入
void SLLInsertBack(SLL* phead, SLLDataType n, SLLDataType insert)
{
	SLL* tem = SLLFind(phead, n);
	if (tem == NULL)
	{
		printf("链表中没有%d\n",n);
		return;
	}
	if (tem == tail)
	{
		SLLPushBack(&phead, insert);
	}
	else
	{
		SLL* Insert = SLLCreateNode(insert);
		Insert->next = tem->next;
		tem->next = Insert;
	}
}
//在指定节点前面插入
void SLLInsertFront(SLL** pphead, SLLDataType n,SLLDataType x)
{
	SLL* tem = SLLFind(*pphead, n);
	if (tem == NULL)
	{
		printf("链表中没有%d\n", n);
		return;
	}
	if (tem == *pphead)
	{
		SLLPushFront(pphead, x);
	}
	else
	{
		SLL* preinsert = *pphead;
		while (preinsert->next != tem)
		{
			preinsert = preinsert->next;
		}
		SLL* Insert = SLLCreateNode(x);
		Insert->next = tem;
		preinsert->next = Insert;
	}
}
//指定删除某节点
void SLLDeleteNode(SLL** pphead, SLLDataType n)
{
	SLL* tem = SLLFind(*pphead, n);
	if (tem == NULL)
	{
		printf("链表中没有%d\n", n);
		return;
	}
	if (tem == *pphead)
	{
		SLLPopFront(pphead);
	}
	else if (tem == *pphead)
	{
		SLLPopBack(pphead);
	}
	else
	{
		SLL* pretem = *pphead;
		while (pretem->next != tem)
		{
			pretem = pretem->next;
		}
		pretem->next = tem->next;
		free(tem);
	}
}
//更改节点
void SLLChange(SLL* phead, SLLDataType n, SLLDataType change)
{
	SLL* tem = SLLFind(phead, n);
	if (tem == NULL)
	{
		printf("链表中没有%d\n", n);
		return;
	}
	tem->data = change;
}
//删除链表
void FreeSLL(SLL* phead)
{
	SLL* tem = phead;
	SLL* next = tem->next;
	free(tem);
	tem = phead->next;
	while (tem != phead)
	{
		next = tem->next;
		free(tem);
		tem = next;
	}
}
//打印链表
void SLLPrint(SLL* phead)
{
	if (phead->next == phead)
	{
		printf("链表为空\n");
		return 0;
	}
	printf("%d ", phead->data);
	SLL* tem = phead->next;
	while (tem != phead)
	{
		printf("%d ", tem->data);
		tem = tem->next;
	}
	printf("\n");
}

三、test.c

代码如下:

#include"List.h"
int main()
{
	SLL* sl = NULL;
	SLLPushBack(&sl, 1);
	SLLPushBack(&sl, 2);
	SLLPushBack(&sl, 3);
	SLLPrint(sl);
	SLLPopBack(&sl);
	SLLPrint(sl);
	SLLPushFront(&sl, 4);
	SLLPrint(sl);
	SLLInsertFront(&sl, 4, 5);
	SLLInsertFront(&sl, 4, 5);
	SLLInsertFront(&sl, 4, 9);

	SLLPrint(sl);
	SLLInsertBack(sl, 2, 7);
	SLLPrint(sl);
	SLLPushBack(&sl, 3);
	SLLPrint(sl);
	SLLInsertBack(sl, 20, 7);
	SLLDeleteNode(&sl, 5);
	SLLPrint(sl);
	SLLDeleteNode(&sl, 3);
	SLLPrint(sl);
	SLLDeleteNode(&sl, 1);
	SLLPrint(sl);
	SLLChange(sl, 5, 8);
	SLLPrint(sl);
	FreeSLL(sl);

	return 0;
}

总结

该形式链表并不具有特殊价值,仅用于锻炼链表能力!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值