链表实现增闪查改,并根据数据大小插入链表
时间: 2023-11-09 20:05:47 浏览: 50
下面是一个使用链表实现增(插入)、删(删除)、查(查询)和改(修改)操作的示例代码。该示例还根据数据大小将元素插入有序链表中。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("内存分配失败\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertNode(Node** head, int data) {
Node* newNode = createNode(data);
// 如果链表为空或新节点的数据小于头节点的数据
if (*head == NULL || data < (*head)->data) {
newNode->next = *head;
*head = newNode;
} else {
Node* curr = *head;
// 找到新节点应该插入的位置
while (curr->next != NULL && curr->next->data < data) {
curr = curr->next;
}
newNode->next = curr->next;
curr->next = newNode;
}
}
void deleteNode(Node** head, int data) {
if (*head == NULL) {
printf("链表为空,无法删除节点\n");
return;
}
Node* curr = *head;
Node* prev = NULL;
// 找到要删除的节点
while (curr != NULL && curr->data != data) {
prev = curr;
curr = curr->next;
}
// 如果找到了要删除的节点
if (curr != NULL) {
// 如果要删除的节点是头节点
if (prev == NULL) {
*head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
printf("成功删除节点 %d\n", data);
} else {
printf("未找到要删除的节点 %d\n", data);
}
}
Node* searchNode(Node* head, int data) {
Node* curr = head;
// 遍历链表查找节点
while (curr != NULL && curr->data != data) {
curr = curr->next;
}
if (curr != NULL) {
printf("找到节点 %d\n", curr->data);
} else {
printf("未找到节点 %d\n", data);
}
return curr;
}
void updateNode(Node* head, int targetData, int newData) {
Node* node = searchNode(head, targetData);
if (node != NULL) {
node->data = newData;
printf("成功将节点 %d 更新为 %d\n", targetData, newData);
}
}
void printList(Node* head) {
Node* curr = head;
printf("链表内容:");
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
void freeList(Node* head) {
Node* curr = head;
Node* temp;
while (curr != NULL) {
temp = curr;
curr = curr->next;
free(temp);
}
}
int main() {
Node* head = NULL;
insertNode(&head, 5);
insertNode(&head, 2);
insertNode(&head, 10);
insertNode(&head, 7);
insertNode(&head, 3);
printList(head);
deleteNode(&head, 10);
deleteNode(&head, 4);
printList(head);
searchNode(head, 7);
updateNode(head, 2, 8);
updateNode(head, 6, 9);
printList(head);
freeList(head);
return 0;
}
```
该示例中定义了一个链表节点结构体 `Node`,并使用 `insertNode` 函数将元素按升序插入有序链表中。`deleteNode` 函数用于删除指定的节点,`searchNode` 函数用于查找指定的节点,`updateNode` 函数用于更新指定节点的数据。最后,使用 `printList` 函数打印链表的内容,并使用 `freeList` 函数释放链表的内存。
您可以根据需要修改示例代码,并根据具体的业务逻辑进行扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)