#include "General.h"


BOOLEAN IntDataTypeIsBigger(Data_Type dt1, Data_Type dt2)
{
    return (dt1.int_ > dt2.int_);
}

void SwapDataType(Data_Type * dt1, Data_Type * dt2)
{
    Data_Type temp = *dt1;
    *dt1 = *dt2;
    *dt2 = temp;
}


DLLL * Partition(DLLL * low, DLLL * high, BOOLEAN IsBigger(Data_Type dt1, Data_Type dt2), void Swap(Data_Type * dt1, Data_Type * dt2)) 
{
    DLLL * temp_min = low->prev; 
    DLLL * temp_loop;
  
    for (temp_loop = low; temp_loop != high; temp_loop = temp_loop->next) 
    { 
        if (IsBigger(high->info, temp_loop->info))
        { 
            temp_min = (temp_min == NULL) ? low : temp_min->next; 
  
            Swap(&(temp_min->info), &(temp_loop->info)); 
        } 
    } 
    temp_min = (temp_min == NULL) ? low : temp_min->next;
    Swap(&(temp_min->info), &(high->info)); 
    return (temp_min); 
} 
void QuickSortDLLL(DLLL * low, DLLL * high) 
{ 
    if (high != NULL && low != high && low != high->next) 
    { 
        DLLL * p = Partition(low, high, IntDataTypeIsBigger, SwapDataType); 
        QuickSortDLLL(low, p->prev); 
        QuickSortDLLL(p->next, high); 
    } 
} 

DLLL * LastDLLL(DLLL * manager) 
{ 
    while (manager->next != NULL) 
        manager = manager->next; 
    return (manager); 
}


void main(void)
{
    int * seed;
    DLLL * manager;
    DLLL * last;
    InitDLLL(&manager);
    PushDLLL(&manager);
    PushDLLL(&manager);
    PushDLLL(&manager);
    PushDLLL(&manager);
    PushDLLL(&manager);
    PushDLLL(&manager);
    DLLL * temp = manager;
    while (temp != NULL)
    {
        temp->info.int_ = Random(&seed,0, 10);
        temp = temp->next;
    }
    last = LastDLLL(manager);
    PrintDLLL(manager, PrintIntDataType);
    QuickSortDLLL(manager, last);

    PrintDLLL(manager, PrintIntDataType);

}