Замена порядка узлов в списке обратным.

Модераторы: Hawk, Romeo, Absurd, DeeJayC, WinMain

Аватара пользователя
TDUTY
Сообщения: 34
Зарегистрирован: 30 янв 2009, 10:20
Контактная информация:

вот мое решение данной задачи... с реверсом и сортировкой...
если конешно все понял правильно:

Код: Выделить всё

#pragma argsused
#include <iostream.h>
#include <conio.h>
//---------------------------------------------------------------------------
class element
{
    public:
        element() : value(0) {};
        element(int value) : value(value) {};
        element(int value, element *next) : value(value), next(next) {};

        int value;
        element *next;
        bool EOL() { return (next == NULL); };

        void addNextElement(int Value)
        {
            element *new_el = new element(Value, next);
            next = new_el;
        };
};
//---------------------------------------------------------------------------
void printElement(element *first)
{
    element *current = first;
    while(!current->EOL())
    {
        cout << current->value << ",";
        current = current->next;
    };
    cout << current->value << ";";
    return;
};
//---------------------------------------------------------------------------
element *reverse(element *first)
{
    element *current = first;

    element *previous = current;
    if(current->next == NULL) return first;
    current = current->next;

    while(!current->EOL())
    {
        element *buf = current->next;
        current->next = previous;
        previous = current;
        current = buf;
    };
    current->next = previous;
    first->next = NULL;
    return current;
};
//---------------------------------------------------------------------------
bool isSorted(element *first)
{
    element *current = first;
    while(!current->EOL())
    {
        if(current->value > current->next->value) return false;
        current = current->next;
    };
    return true;
};
//---------------------------------------------------------------------------
void sort(element *first)
{
    int sch = 0;
    while(!isSorted(first))
    {
        element *current = first;
        while(!current->EOL())
        {
            if(current->value > current->next->value)
            {
                int zn = current->value;
                current->value = current->next->value;
                current->next->value = zn;
            };
            current = current->next;
        };
        cout << "step = " << sch << ":\n\t";
        sch++;
        printElement(first);
        cout << "\n";
    };
};


//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
    //создаем первый эллемент списка
    element *first = new element(0,NULL);

    //заполняем список
    element *current = first;
    for(int i = 1; i <= 10; i++)
    {
        current->addNextElement(i);
        current = current->next;
    };

    //выводим список до реверса
    cout << "before: ";
    printElement(first);
    cout << "\n";

    //реверс
    first = reverse(first);

    //выводим список после реверса
    cout << "after: ";
    printElement(first);
    cout << "\n\n";

    //производим сортировку
    cout << "sort: \n\n";
    sort(first);
    cout << "sort end\n";

    cout << "Press anykey...";
    getch();
    return 0;
}
//---------------------------------------------------------------------------
Ответить