用c语言建立一个单链表
时间: 2023-11-07 20:05:02 浏览: 40
以下是使用C语言实现单链表的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义一个链表节点的结构体
typedef struct Node {
int data; // 数据
struct Node* next; // 指向下一个节点的指针
} Node;
// 定义一个函数,用于在链表的末尾添加一个节点
void addNode(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node)); // 创建一个新的节点
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode; // 如果链表为空,将新节点作为链表的头节点
} else {
Node* currentNode = *head;
while (currentNode->next != NULL) {
currentNode = currentNode->next; // 找到链表的末尾节点
}
currentNode->next = newNode; // 将新节点添加到末尾
}
}
// 定义一个函数,用于在链表中查找指定数据的节点
Node* findNode(Node* head, int data) {
Node* currentNode = head;
while (currentNode != NULL) {
if (currentNode->data == data) {
return currentNode;
}
currentNode = currentNode->next;
}
return NULL;
}
// 定义一个函数,用于删除链表中指定数据的节点
void deleteNode(Node** head, int data) {
Node* currentNode = *head;
Node* prevNode = NULL;
while (currentNode != NULL) {
if (currentNode->data == data) {
if (prevNode == NULL) {
*head = currentNode->next; // 如果要删除的节点是头节点,更新头节点
} else {
prevNode->next = currentNode->next; // 将前一个节点的next指针指向下一个节点
}
free(currentNode); // 释放要删除的节点的内存空间
return;
}
prevNode = currentNode;
currentNode = currentNode->next;
}
}
// 定义一个函数,用于打印链表中所有节点的数据
void printList(Node* head) {
Node* currentNode = head;
while (currentNode != NULL) {
printf("%d ", currentNode->data);
currentNode = currentNode->next;
}
printf("\n");
}
int main() {
Node* head = NULL; // 定义一个空链表
// 在链表末尾添加节点
addNode(&head, 1);
addNode(&head, 2);
addNode(&head, 3);
addNode(&head, 4);
addNode(&head, 5);
// 打印链表中所有节点的数据
printList(head); // 输出:1 2 3 4 5
// 查找链表中指定数据的节点
Node* node3 = findNode(head, 3); // 查找数据为3的节点
if (node3) {
printf("Found node with data %d\n", node3->data); // 输出:Found node with data 3
} else {
printf("Node not found\n");
}
// 删除链表中指定数据的节点
deleteNode(&head, 2); // 删除数据为2的节点
printList(head); // 输出:1 3 4 5
// 释放链表中所有节点的内存空间
Node* currentNode = head;
while (currentNode != NULL) {
Node* temp = currentNode;
currentNode = currentNode->next;
free(temp);
}
return 0;
}
```
在该示例代码中,我们使用了一个结构体来定义链表节点,并使用指针来指向下一个节点。我们还定义了几个函数来操作链表,包括添加节点、查找节点、删除节点和打印链表中所有节点的数据。在 `main()` 函数中,我们创建了一个空链表,并在其中添加了一些数据。然后,我们查找链表中的一个节点,删除另一个节点,并最终释放链表中所有节点的内存空间。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)