74.交替插入链表

给定两组输入,建立两个逆序链表,之后将两个链表依次交替插入到一个新的链表中,如果两个链表不等厂,则将长的链表中剩余的部分直接插入到新链表中

例如:

输入:
12 13 14 15 16 17 0
22 23 24 25 0

输出:12 22 13 23 14 24 15 25 16 17

    void createList(PNODE h){  
        PNODE p;int num;PNODE q;   
        p=(PNODE)malloc(sizeof(NODE));                 
        p->next=NULL;  
        h->next=p;  
        scanf("%d",&num);  
        p->data=num;  
        while(1){  
            scanf("%d",&num);  
            if(num==0){  
                break;  
            }else{  
                q=(PNODE)malloc(sizeof(NODE));  
                q->next=NULL;p->next=q;q->data=num;  
                p=q;  
            }  
        }  
    }   
    void deployList(PNODE h1,PNODE h2,PNODE h3){  
        int i=1,a1=0,a2=0;PNODE p3;PNODE p;  
        p3=h3;  
        p=h1;  
        while(p->next!=NULL){p=p->next;a1++;}  
        p=h2;                              
        while(p->next!=NULL){p=p->next;a2++;}                           
        if(a1>=a2){  
            for(i=1;i<=2*a2;i++){  
                if(i%2 == 1){  
                    p3->next=h1->next;h1->next=(h1->next)->next;p3=p3->next;  
                }else{  
                    p3->next=h2->next;h2->next=(h2->next)->next;p3=p3->next;  
                }  
            }  
            p3->next=h1->next;  
        }else{  
            for(i=1;i<=2*a1;i++){  
                if(i%2 == 1){  
                    p3->next=h1->next;h1->next=(h1->next)->next;p3=p3->next;  
                }else{  
                    p3->next=h2->next;h2->next=(h2->next)->next;p3=p3->next;  
                }  
            }  
            p3->next=h2->next;  
        }  
    }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值