为上述代码添加一些功能
时间: 2024-03-14 14:45:26 浏览: 16
好的,以下是对上述代码进行改进和添加一些功能的实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 链表节点定义
typedef struct ListNode {
int val;
struct ListNode* next;
} ListNode;
// 创建链表节点
ListNode* createListNode(int val) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->val = val;
node->next = NULL;
return node;
}
// 插入节点到链表头部
void insertListNode(ListNode** head, int val) {
ListNode* node = createListNode(val);
node->next = *head;
*head = node;
}
// 插入节点到链表尾部
void appendListNode(ListNode** head, int val) {
ListNode* node = createListNode(val);
if (*head == NULL) {
*head = node;
} else {
ListNode* curr = *head;
while (curr->next) {
curr = curr->next;
}
curr->next = node;
}
}
// 删除链表中指定值的节点
void deleteListNode(ListNode** head, int val) {
ListNode* dummy = createListNode(0);
dummy->next = *head;
ListNode* prev = dummy;
ListNode* curr = *head;
while (curr) {
if (curr->val == val) {
prev->next = curr->next;
free(curr);
curr = prev->next;
} else {
prev = curr;
curr = curr->next;
}
}
*head = dummy->next;
free(dummy);
}
// 反转链表
ListNode* reverseList(ListNode* head) {
ListNode* prev = NULL;
ListNode* curr = head;
while (curr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// 合并两个有序链表
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummy = createListNode(0);
ListNode* curr = dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
curr->next = l1;
l1 = l1->next;
} else {
curr->next = l2;
l2 = l2->next;
}
curr = curr->next;
}
if (l1) {
curr->next = l1;
} else {
curr->next = l2;
}
return dummy->next;
}
// 打印链表
void printList(ListNode* head) {
ListNode* curr = head;
while (curr) {
printf("%d ", curr->val);
curr = curr->next;
}
printf("\n");
}
// 主函数
int main() {
ListNode* head = NULL;
insertListNode(&head, 1);
insertListNode(&head, 2);
insertListNode(&head, 3);
insertListNode(&head, 4);
appendListNode(&head, 6);
appendListNode(&head, 7);
printList(head);
deleteListNode(&head, 3);
printList(head);
ListNode* reversedList = reverseList(head);
printList(reversedList);
ListNode* l1 = createListNode(1);
l1->next = createListNode(3);
l1->next->next = createListNode(5);
ListNode* l2 = createListNode(2);
l2->next = createListNode(4);
ListNode* mergedList = mergeTwoLists(l1, l2);
printList(mergedList);
return 0;
}
```
以上代码对原有的单链表实现了以下改进和添加:
1. 添加了插入节点到链表尾部的方法 `appendListNode`;
2. 添加了反转链表的方法 `reverseList`;
3. 添加了合并两个有序链表的方法 `mergeTwoLists`;
4. 在主函数中演示了新增方法的使用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)