已知两个集合A和B,现要求A并B,如:A=(7,5,3,11),B=(2,6,3)合并后 A=(7,5,3,11,2,6) 用C语言链表编程实现
时间: 2023-10-02 07:05:34 浏览: 98
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node* next;
}Node;
Node* create(int data){
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
Node* merge(Node* a, Node* b){
if(a == NULL) return b;
if(b == NULL) return a;
Node* p = a;
while(p->next != NULL){
p = p->next;
}
p->next = b;
return a;
}
void printList(Node* head){
Node* p = head;
while(p != NULL){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main(){
Node* A = create(7);
A->next = create(5);
A->next->next = create(3);
A->next->next->next = create(11);
Node* B = create(2);
B->next = create(6);
B->next->next = create(3);
Node* C = merge(A, B);
printList(C);
return 0;
}
```
阅读全文