修改了一下链表合并处的代码,供参考。ID和name我合起来传了,忽略
#include <stdio.h>
#include <stdlib.h>
struct circle{
int ID;
char name;
struct circle* next;
};
int main() {
struct circle* head = (struct circle*) malloc(sizeof(struct circle));
head->next=NULL;
struct circle* p;
p = head;
int n;
printf("请输入表长:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
struct circle* s = (struct circle*) malloc(sizeof(struct circle));
p->next = s;
printf("请输入ID和name: ");
scanf("%d %c", &s->ID, &s->name);
s->next = NULL;
p = s;
}
p = head;
struct circle* head1 = (struct circle*) malloc(sizeof(struct circle));
head1->next=NULL;
struct circle* p1;
p1 = head1;
int k;
printf("请输入表长:");
scanf("%d", &k);
for (int i = 1; i <= k; i++) {
struct circle* s = (struct circle*) malloc(sizeof(struct circle));
p1->next = s;
printf("请输入ID和name: ");
scanf("%d %c", &s->ID, &s->name);
s->next = NULL;
p1 = s;
}
p1 = head1;
struct circle* head2 = (struct circle*) malloc(sizeof(struct circle));
head2->next=NULL;
struct circle* p2;
p2 = head2;
p = p->next;
p1 = p1->next;
while(p != NULL || p1 != NULL) {
if (p1 == NULL || (p != NULL && p->ID > p1->ID)) {
p2->next = p;
p2 = p;
p = p->next;
} else {
p2->next = p1;
p2 = p1;
p1 = p1->next;
}
}
p2 = head2;
printf("输出:");
for (int i = 0; i < n+k; i++) {
p2 = p2->next;
printf("%d %c ", p2->ID, p2->name);
}
}