设C = {a1,b1,a2,b2......,an,bn},采用头结点的hc单链表来存放, 设计一个就地算法,将其拆成 A = {a1,a2....a2} B = {bn,bn-1,...b1}


/*
设C = {a1,b1,a2,b2......,an,bn},采用头结点的hc单链表来存放,
设计一个就地算法,将其拆成
A = {a1,a2....a2}   B = {bn,bn-1,...b1}

很容易联想到头插法
*/
LinkList DisCreat_2(LinkList &A)
{
    LinkList B = (LinkList)malloc(sizeof(NODE));

    B->next = NULL;
    LinkList p = A->next,q;//p为工作指针
    LinkList ra = A;
    while(p)
    {
        ra->next = p;
        ra = p;
        p = p->next;

        q = p->next;//q是为了防止断链
        p->next = B->next;
        B->next = p;

        p = q;//还原
    }
    ra->next = NULL;

    return B;
}

 

你可能感兴趣的:(算法,数据结构)