c++实现单链表的交并
时间: 2023-05-17 18:07:52 浏览: 57
非常感谢您的提问!以下是 C 语言实现单链表的交并操作的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createList(int arr[], int n) {
Node* head = NULL;
Node* tail = NULL;
for (int i = 0; i < n; i++) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = arr[i];
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
void printList(Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
Node* intersect(Node* list1, Node* list2) {
Node* head = NULL;
Node* tail = NULL;
while (list1 != NULL && list2 != NULL) {
if (list1->data < list2->data) {
list1 = list1->next;
} else if (list1->data > list2->data) {
list2 = list2->next;
} else {
Node* node = (Node*)malloc(sizeof(Node));
node->data = list1->data;
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
list1 = list1->next;
list2 = list2->next;
}
}
return head;
}
Node* merge(Node* list1, Node* list2) {
Node* head = NULL;
Node* tail = NULL;
while (list1 != NULL && list2 != NULL) {
if (list1->data < list2->data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = list1->data;
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
list1 = list1->next;
} else {
Node* node = (Node*)malloc(sizeof(Node));
node->data = list2->data;
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
list2 = list2->next;
}
}
while (list1 != NULL) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = list1->data;
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
list1 = list1->next;
}
while (list2 != NULL) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = list2->data;
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
list2 = list2->next;
}
return head;
}
int main() {
int arr1[] = {1, 3, 5, 7, 9};
int arr2[] = {2, 3, 4, 7, 8};
Node* list1 = createList(arr1, 5);
Node* list2 = createList(arr2, 5);
printf("List1: ");
printList(list1);
printf("List2: ");
printList(list2);
Node* inter = intersect(list1, list2);
printf("Intersection: ");
printList(inter);
Node* mergeList = merge(list1, list2);
printf("Merge: ");
printList(mergeList);
return 0;
}
```
希望这个代码能够帮助到您!
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)