typedef int DataType_t
typedef struct circularlinkedlist {
DataType_t data
struct circularlinkedlist *next
} CircLList_t
// 1.创建单向循环链表
CircLList_t *CircLList_Create() {
CircLList_t *Head = (CircLList_t *)calloc(1, sizeof(CircLList_t))
if (Head == NULL) {
perror("申请头节点空间失败!")
return NULL
}
Head->next = Head
return Head
}
// 2.创建新节点
CircLList_t *CircLList_NewNode(DataType_t data) {
CircLList_t *New = (CircLList_t *)calloc(1, sizeof(CircLList_t))
if (New == NULL) {
perror("申请新节点内存空间失败!")
return NULL
}
New->next = NULL
New->data = data
return New
}
// 3.头插
bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data) {
CircLList_t *Phead = Head
CircLList_t *New = CircLList_NewNode(data)
if (New == NULL) {
printf("无法插入新节点!\n")
return false
}
if (Head->next == Head) {
Head->next = New
New->next = New
return true
}
// 找到尾节点
while (Phead->next != Head->next) {
Phead = Phead->next
}
Phead->next = New
New->next = Head->next
Head->next = New
return true
}
// 4.尾插
bool CircLList_TailInsert(CircLList_t *Head, DataType_t data) {
CircLList_t *Phead = Head
CircLList_t *New = CircLList_NewNode(data)
if (New == NULL) {
printf("无法插入新节点!\n")
return false
}
if (Head->next == Head) {
Head->next = New
New->next = New
return true
}
// 找到尾节点
while (Phead->next != Head->next) {
Phead = Phead->next
}
Phead->next = New
New->next = Head->next
return true
}
// 5.中间插(在指定值节点后插入)
bool CircLList_MidInsert(CircLList_t *Head, DataType_t destval, DataType_t data) {
CircLList_t *Phead = Head->next
CircLList_t *New = CircLList_NewNode(data)
if (New == NULL) {
printf("无法插入新节点!\n")
return false
}
if (Head->next == Head) {
Head->next = New
New->next = New
return true
}
// 查找目标节点
while (Phead != Head && Phead->data != destval) {
Phead = Phead->next
}
if (Phead == Head) {
printf("未找到值为%d的节点!\n", destval)
free(New)
return false
}
New->next = Phead->next
Phead->next = New
return true
}
// 6.遍历链表
bool CircLList_Print(CircLList_t *Head) {
if (Head->next == Head) {
printf("当前链表为空!\n")
return false
}
CircLList_t *Phead = Head->next
printf("链表内容: ")
do {
printf("%d ", Phead->data)
Phead = Phead->next
} while (Phead != Head->next)
printf("\n")
return true
}
// 7.头删
bool CircLList_HeadDel(CircLList_t *Head) {
if (Head->next == Head) {
printf("链表为空!\n")
return false
}
CircLList_t *Temp = Head->next
if (Temp->next == Temp) { // 只有一个节点的情况
Head->next = Head
} else {
// 找到尾节点
CircLList_t *Tail = Head->next
while (Tail->next != Head->next) {
Tail = Tail->next
}
Head->next = Temp->next
Tail->next = Head->next
}
free(Temp)
return true
}
// 8.尾删
bool CircLList_TailDel(CircLList_t *Head) {
if (Head->next == Head) {
printf("链表为空!\n")
return false
}
CircLList_t *Prev = Head
CircLList_t *Tail = Head->next
// 找到尾节点和前驱节点
while (Tail->next != Head->next) {
Prev = Tail
Tail = Tail->next
}
if (Tail == Head->next && Tail->next == Tail) { // 只有一个节点的情况
Head->next = Head
} else {
Prev->next = Head->next
}
free(Tail)
return true
}
// 9.中间删(删除指定值的节点)
bool CircLList_MidDel(CircLList_t *Head, DataType_t destval) {
if (Head->next == Head) {
printf("链表为空!\n")
return false
}
CircLList_t *Prev = Head
CircLList_t *Current = Head->next
// 查找目标节点
while (Current != Head && Current->data != destval) {
Prev = Current
Current = Current->next
}
if (Current == Head) {
printf("未找到值为%d的节点!\n", destval)
return false
}
if (Current == Head->next) { // 要删除的是首节点
if (Current->next == Current) { // 只有一个节点
Head->next = Head
} else {
// 找到尾节点
CircLList_t *Tail = Head->next
while (Tail->next != Head->next) {
Tail = Tail->next
}
Head->next = Current->next
Tail->next = Head->next
}
} else {
Prev->next = Current->next
}
free(Current)
return true
}
// 10.销毁链表
void CircLList_Destroy(CircLList_t *Head) {
while (Head->next != Head) {
CircLList_HeadDel(Head)
}
free(Head)
}
int main() {
// 创建链表
CircLList_t *list = CircLList_Create()
if (list == NULL) {
printf("链表创建失败!\n")
return -1
}
// 测试插入操作
printf("测试头插:\n")
CircLList_HeadInsert(list, 10)
CircLList_HeadInsert(list, 20)
CircLList_HeadInsert(list, 30)
CircLList_Print(list)
printf("\n测试尾插:\n")
CircLList_TailInsert(list, 40)
CircLList_TailInsert(list, 50)
CircLList_Print(list)
printf("\n测试中间插(在20后插入25):\n")
CircLList_MidInsert(list, 20, 25)
CircLList_Print(list)
// 测试删除操作
printf("\n测试头删:\n")
CircLList_HeadDel(list)
CircLList_Print(list)
printf("\n测试尾删:\n")
CircLList_TailDel(list)
CircLList_Print(list)
printf("\n测试中间删(删除10):\n")
CircLList_MidDel(list, 10)
CircLList_Print(list)
// 销毁链表
CircLList_Destroy(list)
printf("\n链表已销毁\n")
return 0
}
这是我自己写的 以上是完整的代码
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int DataType_t;
typedef struct circularlinkedlist
{
DataType_t data;
struct circularlinkedlist *next;
} CircLList_t;
CircLList_t *CircLList_Create()
{
CircLList_t *Head = (CircLList_t *)calloc(1, sizeof(CircLList_t));
if (Head == NULL)
{
perror("申请头节点空间失败!");
return NULL;
}
Head->next = Head;
return Head;
}
CircLList_t *CircLList_NewNode(DataType_t data)
{
CircLList_t *New = (CircLList_t *)calloc(1, sizeof(CircLList_t));
if (New == NULL)
{
perror("申请新节点内存空间失败!");
return NULL;
}
New->next = NULL;
New->data = data;
return New;
}
bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data)
{
CircLList_t *Phead = Head;
CircLList_t *New = CircLList_NewNode(data);
if (New == NULL)
{
printf("can not insetr new node!\n");
return false;
}
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
while (Phead->next)
{
Phead = Phead->next;
if (Phead->next == Head->next)
{
break;
}
}
Phead->next = New;
New->next = Head->next;
Head->next = New;
return true;
}
bool CircLList_TailInsert(CircLList_t *Head, DataType_t data)
{
CircLList_t *Phead = Head;
CircLList_t *New = CircLList_NewNode(data);
if (New == NULL)
{
printf("can not insetr new node!\n");
return false;
}
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
while (Phead->next)
{
Phead = Phead->next;
if (Phead->next == Head->next)
{
break;
}
}
Phead->next = New;
New->next = Head->next;
return true;
}
bool CircLList_TailInsert(CircLList_t *Head, DataType_t destval, DataType_t data)
{
CircLList_t *Phead = Head;
CircLList_t *New = CircLList_NewNode(data);
if (New == NULL)
{
printf("can not insetr new node!\n");
return false;
}
if (Head->next == Head)
{
Head->next = New;
New->next = New;
return true;
}
while (Phead != Head && Phead->data != destval)
{
Phead = Phead->next;
}
if (Phead == Head)
{
return false;
}
New->next = Phead->next;
Phead->next = New;
return true;
}
bool CircLList_Print(CircLList_t *Head)
{
CircLList_t *Phead = Head;
if (Head->next = Head)
{
printf("current linkedlist is empty!");
return false;
}
while (Phead->next)
{
Phead = Phead->next;
printf("data = %d", Phead->data);
if (Phead->next == Head->next)
{
break;
}
}
return true;
}
bool CircLList_Print(CircLList_t *Head)
{
CircLList_t *Phead = Head;
CircLList_t *Temp = Head->next;
if (Head->next == Head)
{
printf("LinkedList is Empty!\n");
return false;
}
if (Head->next = Head->next->next)
{
Temp->next = NULL;
Head->next = Head;
free(Temp);
return true;
}
while (Phead->next)
{
Phead = Phead->next;
if (Phead->next == Head->next)
{
break;
}
}
Phead->next = Head->next->next;
Head->next = Phead->next;
Temp->next = NULL;
free(Temp);
return true;
}