#1 2011-10-14 15:18:44

michal8m

Administrator

Zarejestrowany: 2011-02-03
Posty: 45
Punktów :   

Sortowanie zad 9

Uporzadkuj elementy na stosie według malejacych wartosci, korzystajac z jednego dodatkowego
stosu i kilku innych zmiennych lokalnych.









#include <iostream>
#include <cstdlib>

using namespace std;

struct elem {
    int dane;
    elem *nast;
};

void push(elem *&stos, int x) {
    elem* e = new elem;
    e->dane = x;
    e->nast = stos;
    stos = e;
}

int pop(elem *&stos) {
    int w = stos->dane;
    elem* d = stos;
    stos = stos->nast;
    delete d;
    return w;
}

int topEl(elem* stos) {
    return stos->dane;
}

int count(elem* stos) {
    int iloscEl = 0;
    while (stos != NULL) {
        iloscEl++;
        stos = stos->nast;
    }
    return iloscEl;
}

bool isEmpty(elem* stos) {
    bool w = true;
    if (count(stos) > 0) {
        w = false;
    }
    return w;
}

void usun(elem* &stos) {
    while (stos != NULL) {
        pop(stos);
    }
}

void stos_sort(elem* &stos) {
    elem* stospom = NULL;
    int liczbaelem = count(stos) - 1;
    bool ruch = true;
    while (ruch) {
        push(stospom, pop(stos));
        for (int i = 0; i < liczbaelem; i++) {
            if (topEl(stos) < topEl(stospom)) {
                push(stospom, pop(stos));
            } else {
                int tym = pop(stospom);
                push(stospom, pop(stos));
                push(stospom, tym);
            }
        }
        ruch = false;
        push(stos, pop(stospom));
        while (!isEmpty(stospom)) {
            if (topEl(stos) > topEl(stospom)) {
                ruch = true;
            };
            push(stos, pop(stospom));
        }
    }
}

int main() {
    elem* pierwszy = NULL;
    system("Pause");
    return 0;
}

Offline

 

Stopka forum

RSS
Powered by PunBB
© Copyright 2002–2008 PunBB
Polityka cookies - Wersja Lo-Fi


Darmowe Forum | Ciekawe Fora | Darmowe Fora
www.javaity.pun.pl taśmy led producent kompensatory mocy biernej