#include <stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct ListNode{
int val;
struct ListNode *next;
};
int minList(int a,int b,int c){
if(a<b){
if(a<c){
return a;
}
else{
return c;
}
}
else{
if(b<c){
return b;
}
else{
return c;
}
}
}
struct ListNode* inputList(struct ListNode* head){
int n=0;
printf("Please enter how many number do you want to:\n");
scanf("%d",&n);
for(int i=1;i<=n-1;i++){
struct ListNode *p2;
p2=(struct ListNode*)malloc(sizeof(struct ListNode));
head->next=p2;
scanf("%d ",&head->next->val);
head=head->next;
}
scanf("%d",&head->val);
head->next=NULL;
return 0;
}
struct ListNode* outputList(struct ListNode* head){
head = head->next;
while(1){
if(head->next==NULL)break;
printf("%d->",head->val);
head=head->next;
}
printf("%d->NULL\n",head->val);
return 0;
}
struct ListNode* mergeList(struct ListNode* A,struct ListNode* B,struct ListNode* C){
struct ListNode *pA, *pB, *pC,*p;
pA = A;
pB = B;
pC = C;
int min;
while(1){
if(pA->next&&pB->next&&pC->next){
min = minList(pA->next->val, pB->next->val, pC->next->val);
}
else{
if (pA->next&&pB->next){
min = pA->next->val < pB->next->val ? pA->next->val: pB->next->val;
}
if(pA->next&&pC->next){
min = pA->next->val < pC->next->val ? pA->next->val: pC->next->val;
}
if(pB->next&&pC->next){
min = pB->next->val < pB->next->val ? pB->next->val: pC->next->val;
}
if(pA->next)
min = pA->next->val;
if(pB->next)
min = pB->next->val;
if(pC->next)
min = pC->next->val;
}
if(pA->next&&min==pA->next->val){
pA = pA->next;
}
else{
p=(struct ListNode*)malloc(sizeof(struct ListNode));
p->next = pA->next;
pA->next = p;
p->val = min;
pA = pA->next;
}
if(pB->next&&min==pB->next->val){
pB = pB->next;
}
if(pC->next&&min==pC->next->val){
pC = pC->next;
}
if(pA->next==NULL&&pB->next==NULL&&pC->next==NULL){
break;
}
}
}
int main() {
struct ListNode HA, HB, HC;
HA.val=HB.val=HC.val=-1;
HA.next=HB.next=HC.next=NULL;
inputList(&HA);inputList(&HB);inputList(&HC);
printf("\n\nList A = ");outputList(&HA);
printf("List B = ");outputList(&HB);
printf("List C = ");outputList(&HC);
mergeList(&HA, &HB, &HC);
printf("\n\nList A = ");outputList(&HA);
system("pause");
}