#include"head.h"
struct Student* insert(struct Student*ahead, struct Student*bhead)
{
    struct Student *pa1, *pa2, *pb1, *pb2;
    pa1 = pa2 = ahead;
    pb1 = pb2 = bhead;
    if ((ahead != NULL)&&(bhead != NULL))    {
        do
        {
            while ((pb1->num > pa1->num) && (pa1->next != NULL)) //先找到要插入的位置
            {
                pa2 = pa1;
                pa1 = pa1->next;     //pa1在前,pa2在后
            }
            if (pb1->num <= pa1->num)
            {
                if (pa1 == ahead) ahead = pb1; //pb1往上要么把插入的链表链接到头指针
                else pa2->next = pb1;//pb1往上要么把插入的链表链接到pa2->nex
                pb1 = pb1->next; //先把pb1往下一个要插的节点移,释放pb1
                pb2->next = pa1;//往下把插入的节点连接到原列表pa1处
                pa2 = pb2;//把pa2指向刚插入的链表pb2,与pa1挨着,回归原始状态
                pb2 = pb1;//再把pb2移到pb1处,回归原始状态
            }
        } while ((pa1->next != NULL) || (pb1 != NULL&&pa1 == NULL));
//循环条件,还没有指向原链表最后一个元素或者pb还有元素且原列表不空
        if ((pb1 != NULL) && (pb1->num > pa1->num) && (pa1->next == NULL))
            pa1->next = pb1;  //原列表已经插到最后了
    }
    else if ((ahead == NULL) && (bhead != NULL)) ahead = bhead;           
    return ahead;
}

//分四种情况,{alist=null,blist=null},{ alist!=null,blist=null },{ //alist=null,blist=!null },
//{ alist=!null,blist=!null },第四种最复杂。

 

posted on 2016-12-23 22:20  新爱代  阅读(1229)  评论(0编辑  收藏  举报