(2) c语言设计一个循环单链表:实现循环单链表的初始化、求元素个数、插入、删 除、取元素操作;设计一个测试主函数验证设计的循环单链表的正确性
时间: 2024-10-19 11:08:11 浏览: 46
在C语言中,我们可以设计一个简单的循环单链表结构,包括节点结构体、链表头结点以及对应的初始化、操作等函数。这里是一个基础的实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构
typedef struct Node {
int data;
struct Node* next;
} Node;
// 初始化链表
Node* createList() {
Node* head = (Node*)malloc(sizeof(Node));
if (!head) {
printf("Memory allocation failed.\n");
return NULL;
}
head->data = 0; // 首节点通常设为空
head->next = head; // 创建循环链表
return head;
}
// 求元素个数
int getLength(Node* head) {
int count = 0;
Node* temp = head;
while (temp != head->next) { // 循环直到遇到第一个节点
count++;
temp = temp->next;
}
return count + 1; // 包含头节点
}
// 插入元素
void insert(Node** head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed.\n");
return;
}
newNode->data = value;
newNode->next = (*head)->next;
(*head)->next = newNode;
// 如果已经是循环链表,则连接新节点到头节点
if ((*head)->next == *head)
(*head)->next = newNode;
}
// 删除元素
void remove(Node** head, int value) {
if (*head == NULL) return;
Node* current = *head;
Node* prev = NULL;
while (current != NULL && current->data != value) {
prev = current;
current = current->next;
}
if (current == NULL) return; // 没找到要删除的节点
if (prev == NULL) { // 如果要删除的是头节点
*head = current->next;
} else {
prev->next = current->next;
}
free(current); // 释放内存
}
// 取元素
int getElement(Node* head, int index) {
if (index < 0 || index >= getLength(head)) {
printf("Invalid index.\n");
return -1;
}
Node* temp = head;
for (int i = 0; i < index; i++) {
temp = temp->next;
}
return temp->data;
}
// 测试主函数
int main() {
Node* list = createList();
if (list == NULL) {
return 1;
}
// ... 进行一些操作,比如插入、删除和取值,然后打印链表长度
printf("List length: %d\n", getLength(list));
// 添加更多测试案例
insert(&list, 5);
printf("Inserted element: %d\n", getElement(list, 1)); // 应该返回5
remove(&list, 5);
printf("After removing 5, length is %d\n", getLength(list));
free(list);
return 0;
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)