循环链表的增删改查以及测试操作

#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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ac果

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值