循环链表 c

本文介绍了一个简单的循环链表实现过程,包括创建链表、插入节点及遍历显示链表等功能。通过C语言代码示例展示了如何操作循环链表。

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

1. 循环链表

### C语言实现循环链表的数据结构 以下是基于提供的引用内容以及专业知识编写的关于如何用C语言实现循环链表的完整示例代码。 #### 单循环链表的实现 单循环链表是一种特殊的线性数据结构,其中最后一个节点指向头节点形成闭环。下面是一个简单的单循环链表的创建、插入和遍历功能: ```c #include <stdio.h> #include <stdlib.h> typedef struct SingleCircularListNode { int data; struct SingleCircularListNode *next; } SingleCircularListNode; // 创建新节点 SingleCircularListNode* createNode(int value) { SingleCircularListNode* newNode = (SingleCircularListNode*)malloc(sizeof(SingleCircularListNode)); if (!newNode) { printf("Memory error\n"); exit(1); } newNode->data = value; newNode->next = NULL; return newNode; } // 初始化单循环链表 void initList(SingleCircularListNode **head, int value) { *head = createNode(value); (*head)->next = *head; // 自己指向自己形成环 } // 插入到单循环链表头部 void insertAtHead(SingleCircularListNode **head, int value) { SingleCircularListNode* newNode = createNode(value); if (*head == NULL) { initList(head, value); return; } newNode->next = (*head)->next; (*head)->next = newNode; // 更新头节点为新的节点 *head = newNode; } // 遍历并打印单循环链表 void traverseList(SingleCircularListNode *head) { if (head == NULL || head->next == NULL) { printf("The list is empty.\n"); return; } SingleCircularListNode *temp = head->next; do { printf("%d ", temp->data); temp = temp->next; } while (temp != head->next); printf("\n"); } ``` 上述代码实现了单循环链表的核心操作:初始化列表 `initList`[^1] 和在头部插入节点 `insertAtHead` 的逻辑。最后通过 `traverseList` 函数来展示整个链表的内容。 --- #### 双向循环链表的实现 双向循环链表不仅具有前后指针,还形成了一个闭合回路。下面是其基本的操作实现: ```c #include <stdio.h> #include <stdlib.h> typedef struct DoubleCircularListNode { int data; struct DoubleCircularListNode *prev; struct DoubleCircularListNode *next; } DoubleCircularListNode; // 创建新节点 DoubleCircularListNode* createDCLinkedListNode(int value) { DoubleCircularListNode* newNode = (DoubleCircularListNode*)malloc(sizeof(DoubleCircularListNode)); if (!newNode) { printf("Memory error\n"); exit(1); } newNode->data = value; newNode->prev = newNode->next = NULL; return newNode; } // 初始化双向循环链表 void initDCLinkedList(DoubleCircularListNode **head, int value) { *head = createDCLinkedListNode(value); (*head)->prev = *head; (*head)->next = *head; } // 在头部插入节点 void insertAtDCHead(DoubleCircularListNode **head, int value) { DoubleCircularListNode* newNode = createDCLinkedListNode(value); if (*head == NULL) { initDCLinkedList(head, value); return; } newNode->next = *head; newNode->prev = (*head)->prev; (*head)->prev->next = newNode; (*head)->prev = newNode; *head = newNode; } // 前向遍历 void forEachNext(DoubleCircularListNode *head) { if (head == NULL || head->next == head) { printf("The list is empty or has only one element.\n"); return; } DoubleCircularListNode *current = head->next; do { printf("%d ", current->data); current = current->next; } while (current != head->next); printf("\n"); } // 后向遍历 void forEachPrev(DoubleCircularListNode *head) { if (head == NULL || head->prev == head) { printf("The list is empty or has only one element.\n"); return; } DoubleCircularListNode *current = head->prev; do { printf("%d ", current->data); current = current->prev; } while (current != head->prev); printf("\n"); } ``` 此部分展示了双向循环链表的关键特性——支持从前向后和从后向前两种方式遍历链表的功能 `forEachNext` 和 `forEachPrev`[^4]。同时提供了在头部插入节点的方法 `insertAtDCHead`[^2]。 --- #### 总结 以上两段代码分别演示了单循环链表和双向循环链表的基础构建方法及其核心操作。这些基础模块可以进一步扩展以满足更复杂的需求,比如删除特定位置上的节点或者查找某个值是否存在等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值