lista.h

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
1.4 kB
2
Indexable
Never
#ifndef LISTA_H_INCLUDED
#define LISTA_H_INCLUDED

//Generikus
template <typename Adattipus>
class Lista
{
    struct ListaElem
    {
        Adattipus adat;
        ListaElem* kov;
        ListaElem(Adattipus a) :adat(a), kov(NULL) {}
    };

    ListaElem* eleje;
    size_t meret;

public:
    Lista() :eleje(NULL), meret(0) {};

    size_t getSize() {return meret;}
    bool isNull() {return eleje == NULL ? true : false;}

    void Add(Adattipus e)
    {
        ListaElem *uj = new ListaElem(e);

        if (eleje == NULL)
        {
            eleje = uj;
            meret++;
            //std::cout<<"Elem: " << eleje->adat <<std::endl;
        }
        else
        {
            ListaElem* mozgo = eleje;
            while(mozgo != NULL)
                mozgo = mozgo->kov;
            mozgo = uj;
            meret++;
            //std::cout<<"Elem" << eleje->adat <<std::endl;
        }
    }

    Adattipus& operator[](size_t idx)
    {
        if (idx >= meret)
            throw "Rossz index Lista[]!";
        ListaElem* mozgo = eleje;
        while(mozgo->kov != NULL)
            mozgo = mozgo->kov;
        return mozgo->adat;
    }

    void clear()
    {
        ListaElem* mozgo = eleje;
        ListaElem* torlos = eleje;
        while(mozgo != NULL)
        {
            mozgo = mozgo->kov;
            delete torlos;
            torlos = mozgo;
        }
    }
};
#endif // LISTA_H_INCLUDED
Leave a Comment