void initList(Node *&list) {
list = NULL;
}
void insert(Node*& list ,int x) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = x;
newNode->next = NULL;
if (list == NULL) {
list = newNode;
list->next = list;
} else {
Node *current = list;
while (current->next != list) {
current = current->next;
}
current->next = newNode;
newNode->next = list;
}
}
void mergeLists(Node *lista, Node *listb, Node *&listc) {
if (lista == NULL) {
listc = listb;
return;
}
if (listb == NULL) {
listc = lista;
return;
}
Node *pa = lista;
Node *pb = listb;
Node *pc = listc = lista;
while (pa->next != lista && pb->next != listb) {
if (pa->data <= pb->data) {
pc->next = pa;
pc = pa;
pa = pa->next;
} else {
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
pc->next = (pa->next != lista) ? pa : pb;
pc = listc;
while (pc->next != lista) {
pc = pc->next;
}
pc->next = listc;
}
int main() {
int a, b, c, d, e, a1, b1, c1, d1, e1;
Node *lista, *listb, *listc;
initList(lista);
initList(listb);
printf("输入要插入到第一个循环链表中的数据:\n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
insert(lista, a);
insert(lista, b);
insert(lista, c);
insert(lista, d);
insert(lista, e);
printf("输入要插入到第二个循环链表中的数据:\n");
scanf("%d %d %d %d %d", &a1, &b1, &c1, &d1, &e1);
insert(listb, a1);
insert(listb, b1);
insert(listb, c1);
insert(listb, d1);
insert(listb, e1);
mergeLists(lista, listb, listc);
printf("合并后的链表数据为:\n");
Node* current = listc->next;
while (current != listc) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");