Example #1
0
//------------------------------------------------------------------------------
//Функция объединения двух упорядоченных списков с сохранением порядка
List Concat(List list1,List list2)
{
    if (list1.root==NULL || list2.root==NULL)
    {
        cout <<"Concat2:error: one or more lists is empty\n";
    }
    TNode *temp1,*temp2;
    temp1=list1.last;
    temp2=list2.root;
    while (temp2!=NULL)
    {
        for (;;)
        {
            if ( (temp1->data < temp2->data) || (temp1->prev==NULL) ) break;
            temp1=temp1->prev;
        }
        if ( (temp1==list1.root) && (temp1->data > temp2->data) )
            list1.AddBefore(temp1,temp2->data);
        else
            list1.AddAfter(temp1,temp2->data);
        temp1=list1.last;
        if (temp2->next==NULL) break;
        temp2=temp2->next;
        list2.Delete(list2.root);
    }
    return list1;
}