Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------------
- #ifndef UTablaH
- #define UTablaH
- //---------------------------------------------------------------------------
- const MAX_TABLA = 100;
- template <class T>
- class Tabla
- {
- private:
- int numElem;
- T* tabla;
- public:
- Tabla();
- ~Tabla();
- int dameNumElem() const;
- bool buscaPos(const T&, int&);
- void push(const T&);
- bool pop(T&);
- bool dameElem(int, T&) const;
- } ;
- //----------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- template <class T> //constructora
- Tabla<T>::Tabla()
- {
- numElem = 0;
- tabla = new T[MAX_TABLA];
- }
- //------------------------------------------------------------------------------
- template <class T> //destructora
- Tabla<T>::~Tabla()
- {
- // delete[] tabla;
- // tabla = NULL;
- delete [] tabla;
- numElem = 0;
- }
- //------------------------------------------------------------------------------
- template <class T> //devuelve el llenas
- int Tabla<T>::dameNumElem() const
- {
- return numElem;
- }
- //------------------------------------------------------------------------------
- template <class T>
- bool Tabla<T>::buscaPos(const T& e, int& i) // T& engloba todos los tipos metidos en T*(int, char, bytecodes...)
- {
- for (int j=0;j<numElem;j++)
- {
- if (e == tabla[j])
- {
- i = j; //PORQUE LO PONE EN EL ENUNCIADO
- return true;
- }
- }
- i = numElem;
- return false;
- }
- //------------------------------------------------------------------------------
- template <class T>
- void Tabla<T>::push(const T& d) //AΓADE AQUI EN CONCRETO AL FINAL DEL ARRAY, ADIFERECNCIA DEL RESTO Q ERA AL PRINCIPIO. POR ENUNCIADO
- {
- int i=0;
- if ( numElem > MAX_TABLA ) //SI LAS LLENAS ES MAYOR QUE EL TOPE...
- {
- /* int MAX_TABLA2 = MAX_TABLA*2;
- T* tabla2 = new T[MAX_TABLA2];
- for (int j=0;j<=MAX_TABLA;j++)
- {
- tabla2[j]=tabla1[j];
- }
- tabla2[MAX_TABLA+1]=d;
- numElem++;
- MAX_TABLA = MAX_TABLA2;
- tabla = tabla2;
- delete tabla2;
- tabla2 = NULL;
- */ }
- else
- {
- tabla[numElem] = d;
- numElem++;
- }
- }
- //------------------------------------------------------------------------------
- template <class T>
- bool Tabla<T>::pop(T& d)
- {
- numElem--;
- tabla[numElem] = NULL;
- }
- //------------------------------------------------------------------------------
- template <class T>
- bool Tabla<T>::dameElem(int i, T& v) const
- {
- if ((i<0)||(i>=numElem))
- {
- return false;
- }
- else
- {
- v = tabla[i];
- return true;
- }
- }
- //------------------------------------------------------------------------------
- #endif
Add Comment
Please, Sign In to add comment