从键盘输入两个升序排列的整数序列inka和link b,每个序
列以输入0为结束标记。
·将链表link a和link b归并为link c,link c仍然为升序排列。归
并完成后,link a和link b为空表。输出link c。
·对link c进行处理,保持升序不变,删除其中重复的整数,
对重复的整数只保留一个,输出删除重复整数后的链表。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct List {
int num;
struct List* next;
}list;
int main() {
list* head1 = (list*)malloc(sizeof(list));
list* s1 = head1;
scanf("%d", &(s1->num));
while (s1->num != 0) {
list* s = (list*)malloc(sizeof(list));
s1->next = s;
s1 = s1->next;
scanf("%d", &(s1->num));
}
s1->next = NULL;
list* head2 = (list*)malloc(sizeof(list));
list* s2 = head2;
scanf("%d", &(s2->num));
while (s2->num != 0) {
list* s = (list*)malloc(sizeof(list));
s2->next = s;
s2 = s2->next;
scanf("%d", &(s2->num));
}
s2->next = NULL;
list* head3 = (list*)malloc(sizeof(list));
head3->num;
list* s3 = head3;
if (head1->num >= head2->num) {
s3->num = head1->num;
list* p1 = head1->next;
list* p2 = head2;
while (p1 && p2) {
if (p1 >= p2) {
s3->next = p1;
s3 = s3->next;
p1 = p1->next;
}
else {
s3->next = p1;
s3 = s3->next;
p2 = p2->next;
}
s3->next = p1 ? p1 : p2;
s3->next = NULL;
}
}
else{
s3->num = head2->num;
list* p1 = head1;
list* p2 = head2->next;
while (p1 && p2) {
if (p1 >= p2) {
s3->next = p1;
s3 = s3->next;
p1 = p1->next;
}
else {
s3->next = p2;
s3 = s3->next;
p2 = p2->next;
}
s3->next = p1 ? p1 : p2;
s3->next = NULL;
}
}
s3 = head3;
while(s3->next!=NULL)
{
if (s3->num == s3->next->num) {
s3->next = s3->next->next;
continue;
}
s3 = s3->next;
}
s3 = head3;
while (s3->num!=0) {
printf("%d ",s3->num);
s3 = s3->next;
}
return 0;
}