Guest User

Untitled

a guest
May 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #ifndef UTablaH
  4. #define UTablaH
  5.  
  6. //---------------------------------------------------------------------------
  7. const MAX_TABLA = 100;
  8. template <class T>
  9.  
  10. class Tabla
  11. {
  12. private:
  13. int numElem;
  14. T* tabla;
  15.  
  16. public:
  17. Tabla();
  18. ~Tabla();
  19. int dameNumElem() const;
  20. bool buscaPos(const T&, int&);
  21. void push(const T&);
  22. bool pop(T&);
  23. bool dameElem(int, T&) const;
  24. } ;
  25.  
  26. //----------------------------------------------------------------------------
  27.  
  28.  
  29. //------------------------------------------------------------------------------
  30.  
  31. template <class T> //constructora
  32. Tabla<T>::Tabla()
  33. {
  34. numElem = 0;
  35. tabla = new T[MAX_TABLA];
  36. }
  37. //------------------------------------------------------------------------------
  38. template <class T> //destructora
  39. Tabla<T>::~Tabla()
  40. {
  41. // delete[] tabla;
  42. // tabla = NULL;
  43.  
  44. delete [] tabla;
  45. numElem = 0;
  46.  
  47. }
  48. //------------------------------------------------------------------------------
  49. template <class T> //devuelve el llenas
  50. int Tabla<T>::dameNumElem() const
  51. {
  52. return numElem;
  53. }
  54. //------------------------------------------------------------------------------
  55. template <class T>
  56. bool Tabla<T>::buscaPos(const T& e, int& i) // T& engloba todos los tipos metidos en T*(int, char, bytecodes...)
  57. {
  58. for (int j=0;j<numElem;j++)
  59. {
  60. if (e == tabla[j])
  61. {
  62. i = j; //PORQUE LO PONE EN EL ENUNCIADO
  63. return true;
  64. }
  65. }
  66. i = numElem;
  67. return false;
  68. }
  69. //------------------------------------------------------------------------------
  70. template <class T>
  71. 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
  72. {
  73. int i=0;
  74.  
  75. if ( numElem > MAX_TABLA ) //SI LAS LLENAS ES MAYOR QUE EL TOPE...
  76. {
  77. /* int MAX_TABLA2 = MAX_TABLA*2;
  78. T* tabla2 = new T[MAX_TABLA2];
  79. for (int j=0;j<=MAX_TABLA;j++)
  80. {
  81. tabla2[j]=tabla1[j];
  82. }
  83. tabla2[MAX_TABLA+1]=d;
  84. numElem++;
  85. MAX_TABLA = MAX_TABLA2;
  86. tabla = tabla2;
  87. delete tabla2;
  88. tabla2 = NULL;
  89. */ }
  90. else
  91. {
  92. tabla[numElem] = d;
  93. numElem++;
  94. }
  95. }
  96. //------------------------------------------------------------------------------
  97. template <class T>
  98. bool Tabla<T>::pop(T& d)
  99. {
  100. numElem--;
  101. tabla[numElem] = NULL;
  102. }
  103. //------------------------------------------------------------------------------
  104. template <class T>
  105. bool Tabla<T>::dameElem(int i, T& v) const
  106. {
  107. if ((i<0)||(i>=numElem))
  108. {
  109. return false;
  110. }
  111. else
  112. {
  113. v = tabla[i];
  114. return true;
  115. }
  116. }
  117. //------------------------------------------------------------------------------
  118.  
  119. #endif
Add Comment
Please, Sign In to add comment