#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct ListNode {
int data;
struct ListNode* next;
} ListNode, *LinkedList;
// 创建循环链表
LinkedList createCircularLinkedList() {
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
head->data = 0;
head->next = head;
return head;
}
// 在循环链表末尾插入元素
void circularListInsert(LinkedList list, int data) {
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = data;
newNode->next = NULL;
ListNode* p = list;
while (p->next != list) {
p = p->next;
}
newNode->next = list;
p->next = newNode;
}
// 在循环链表指定位置插入元素
bool circularListInsertAt(LinkedList list, int index, int data) {
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = data;
newNode->next = NULL;
ListNode* p = list;
int count = 0;
while (p->next != list && count < index) {
p = p->next;
count++;
}
if (count < index) {
free(newNode);
return false;
}
newNode->next = p->next;
p->next = newNode;
return true;
}
// 从循环链表中删除指定位置的节点
bool circularListDeleteAt(LinkedList list, int index) {
ListNode* p = list;
int count = 0;
while (p->next != list && count < index) {
p = p->next;
count++;
}
if (count < index) {
return false;
}
ListNode* q = p->next;
p->next = q->next;
free(q);
return true;
}
// 修改循环链表指定位置的节点值
bool circularListUpdate(LinkedList list, int index, int newData) {
ListNode* p = list->next;
int count = 0;
while (p != list && count < index) {
p = p->next;
count++;
}
if (count < index) {
return false;
}
p->data = newData;
return true;
}
// 在循环链表中查找指定位置的节点值
int circularListGet(LinkedList list, int index) {
ListNode* p = list->next;
int count = 0;
while (p != list && count < index) {
p = p->next;
count++;
}
if (count < index) {
return -1;
}
return p->data;
}
// 遍历循环链表并输出节点值
void circularListVisit(LinkedList list) {
ListNode* p = list->next;
while (p != list) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 销毁循环链表,释放内存
void destroyCircularLinkedList(LinkedList list) {
ListNode* p = list->next;
while (p != list) {
ListNode* temp = p;
p = p->next;
free(temp);
}
free(list);
}
int main() {
LinkedList list = createCircularLinkedList();
// 在循环链表末尾插入元素
circularListInsert(list, 10);
circularListInsert(list, 20);
circularListInsert(list, 30);
circularListInsert(list, 40);
// 输出循环链表内容
printf("循环链表内容:");
circularListVisit(list);
// 在循环链表指定位置插入元素
bool success = circularListInsertAt(list, 2, 25);
if (success) {
printf("插入元素后的循环链表内容:");
circularListVisit(list);
} else {
printf("插入元素失败\n");
}
// 删除循环链表指定位置的节点
success = circularListDeleteAt(list, 1);
if (success) {
printf("删除元素后的循环链表内容:");
circularListVisit(list);
} else {
printf("删除元素失败\n");
}
// 修改循环链表指定位置的节点值
success = circularListUpdate(list, 2, 35);
if (success) {
printf("修改元素后的循环链表内容:");
circularListVisit(list);
} else {
printf("修改元素失败\n");
}
// 查找循环链表指定位置的节点值
int data = circularListGet(list, 1);
if (data != -1) {
printf("索引1的节点值:%d\n", data);
} else {
printf("查找元素失败\n");
}
destroyCircularLinkedList(list);
return 0;
}
11-10
737
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-01
3247
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-17
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交