编辑代码

#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;
        }
    }
}
//minList函数结束

//作用:输入链表
//输入形式:
//4
//1 2 3 4 \n
//输出:无
//返回:无
//作用在全局变量
struct ListNode* inputList(struct ListNode* head){
    //需要输入n个数字
    int n=0;
    printf("Please enter how many number do you want to:\n");
    scanf("%d",&n);
    //循环n-1次
    for(int i=1;i<=n-1;i++){
        //分配新空间给后指针
        struct ListNode *p2;
        p2=(struct ListNode*)malloc(sizeof(struct ListNode));
        //让现指针指向后指针
        head->next=p2;//printf("->");
        //输入现指针的数值
        scanf("%d ",&head->next->val);//printf("->");
        //让现指针指向的指针变成现指针
        head=head->next;
        //printf("->");
    }
    //最后一次输入数字
    scanf("%d",&head->val);
    head->next=NULL;
    return 0;
}
//inputList函数结束

//作用:输出链表
//输入:表头
//输出形式:1->2->3->4->Null
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;
}
//outputList函数结束

//作用:使操作后的表A中仅留下3个表中均包含的数据元素的结点,且没有值相同的结点,并释放所有无用的结点
//输入三个表头
//输出:无
struct ListNode* mergeList(struct ListNode* A,struct ListNode* B,struct ListNode* C){
    struct ListNode *pA, *pB, *pC,*p;//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;
        }
        

        //三个节点都是NULL, 退出函数
        if(pA->next==NULL&&pB->next==NULL&&pC->next==NULL){
            break;
        }
        
    }
}
//mergeList函数结束

int main() {

    struct ListNode HA, HB, HC;//H~是头指针
    HA.val=HB.val=HC.val=-1;
    HA.next=HB.next=HC.next=NULL;
    //构建完成

    //HA=1->2->4->8->9->10->Null  (6)
    //HB=3->9->10->20->Null   (4)
    //HC=8->9->10->11->15->16->17->19->Null  (8)
    //测试数据(粘贴复制):6 1 2 4 8 9 10 4 3 9 10 20 8 8 9 10 11 15 16 17 19
    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);




    //输出结果
    //HA=1->2->3->4->8->9->10->11->15->16->17->19->20->Null
    printf("\n\nList A = ");outputList(&HA);
    //输出完成

    system("pause");
}