Update README.md
This commit is contained in:
parent
fb1e08c1a0
commit
e275251e3a
118
README.md
118
README.md
@ -6,61 +6,111 @@
|
|||||||
## 示例
|
## 示例
|
||||||
`linked_list.h`:
|
`linked_list.h`:
|
||||||
```c
|
```c
|
||||||
#ifndef LINKED_LIST_H
|
#ifndef LINKED_LIST_H
|
||||||
#define LINKED_LIST_H
|
#define LINKED_LIST_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// 链表节点结构体
|
||||||
typedef struct Node {
|
typedef struct Node {
|
||||||
int data;
|
int data;
|
||||||
struct Node* next;
|
struct Node* next;
|
||||||
} Node;
|
} Node;
|
||||||
|
|
||||||
Node* createNode(int data);
|
// 链表操作函数声明
|
||||||
void insertNode(Node** head, int data);
|
Node* create_node(int data);
|
||||||
void deleteNode(Node** head, int data);
|
void append_node(Node** head, int data);
|
||||||
void printList(Node* head);
|
bool delete_node(Node** head, int data);
|
||||||
|
void print_list(const Node* head);
|
||||||
|
void free_list(Node** head);
|
||||||
|
|
||||||
|
#endif // LINKED_LIST_H
|
||||||
|
|
||||||
#endif
|
|
||||||
```
|
```
|
||||||
|
|
||||||
`linked_list.c`:
|
`linked_list.c`:
|
||||||
```c
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "linked_list.h"
|
#include "linked_list.h"
|
||||||
|
|
||||||
Node* createNode(int data) {
|
// 创建新节点
|
||||||
Node* newNode = (Node*)malloc(sizeof(Node));
|
Node* create_node(int data) {
|
||||||
newNode->data = data;
|
Node* new_node = (Node*)malloc(sizeof(Node));
|
||||||
newNode->next = NULL;
|
if (!new_node) {
|
||||||
return newNode;
|
fprintf(stderr, "内存分配失败\n");
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
|
|
||||||
void insertNode(Node** head, int data) {
|
|
||||||
Node* newNode = createNode(data);
|
|
||||||
newNode->next = *head;
|
|
||||||
*head = newNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
void deleteNode(Node** head, int data) {
|
|
||||||
Node* temp = *head, *prev = NULL;
|
|
||||||
while (temp != NULL && temp->data != data) {
|
|
||||||
prev = temp;
|
|
||||||
temp = temp->next;
|
|
||||||
}
|
}
|
||||||
if (temp == NULL) return;
|
new_node->data = data;
|
||||||
if (prev) prev->next = temp->next;
|
new_node->next = NULL;
|
||||||
else *head = temp->next;
|
return new_node;
|
||||||
free(temp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void printList(Node* head) {
|
// 在链表末尾添加节点
|
||||||
Node* temp = head;
|
void append_node(Node** head, int data) {
|
||||||
while (temp) {
|
Node* new_node = create_node(data);
|
||||||
printf("%d -> ", temp->data);
|
if (*head == NULL) {
|
||||||
temp = temp->next;
|
*head = new_node;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Node* current = *head;
|
||||||
|
while (current->next != NULL) {
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
current->next = new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除包含指定数据的节点
|
||||||
|
bool delete_node(Node** head, int data) {
|
||||||
|
if (*head == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Node* current = *head;
|
||||||
|
Node* previous = NULL;
|
||||||
|
|
||||||
|
// 查找要删除的节点
|
||||||
|
while (current != NULL && current->data != data) {
|
||||||
|
previous = current;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 未找到节点
|
||||||
|
if (current == NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除节点
|
||||||
|
if (previous == NULL) {
|
||||||
|
// 要删除的是头节点
|
||||||
|
*head = current->next;
|
||||||
|
} else {
|
||||||
|
previous->next = current->next;
|
||||||
|
}
|
||||||
|
free(current);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打印链表
|
||||||
|
void print_list(const Node* head) {
|
||||||
|
const Node* current = head;
|
||||||
|
while (current != NULL) {
|
||||||
|
printf("%d -> ", current->data);
|
||||||
|
current = current->next;
|
||||||
}
|
}
|
||||||
printf("NULL\n");
|
printf("NULL\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 释放链表内存
|
||||||
|
void free_list(Node** head) {
|
||||||
|
Node* current = *head;
|
||||||
|
while (current != NULL) {
|
||||||
|
Node* next = current->next;
|
||||||
|
free(current);
|
||||||
|
current = next;
|
||||||
|
}
|
||||||
|
*head = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
`main.c`:
|
`main.c`:
|
||||||
```c
|
```c
|
||||||
|
Loading…
x
Reference in New Issue
Block a user