74.交替插入链表

74.交替插入链表,第1张

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;  
        }  
    }  

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5699101.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存