FacundoCruz

Pila

Oct 11th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. //
  2. //Creado por Julio Tentor <jtentor@fi.unju.edu.ar>
  3. //Modificado y Traducido al Español por Mario Ariel Fernando Cabana <42268639@fi.unju.edu.ar>
  4. //
  5.  
  6.  
  7. /*
  8. La clase Pila representa un objeto tipo pila last-in-first-out (LIFO).
  9.  
  10. Las usuales operaciones push and pop son proveidas, de igual modo el metodo
  11. peek para el item de la cima de la pila, un método dedicado a comprobar si
  12. la pila esta vacia.
  13.  
  14. Cuando una pila es creada, esta no contiene items.
  15.  
  16. from https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/Stack.html
  17. from https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Stack.html
  18. from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Stack.html
  19.  
  20. */
  21. public class Pila<T> {
  22. //---------------Constantes del objeto Genérico Pila----------------//
  23.  private final static Integer defaulDimension = 10;
  24.  
  25. //---------------Atributos del objeto Genérico Pila----------------//
  26.  private T [] dato;
  27.  private Integer contador;
  28.  
  29. //-------------Constructores del objeto Genérico Pila--------------//
  30.  public Pila() {
  31.      this(Pila.defaulDimension);
  32.  }
  33.  
  34.  public Pila(Integer dimension) {
  35.      if (dimension <= 0) {
  36.          throw new RuntimeException("La cantidad de elementos en la  pila debe ser positiva");
  37.      }
  38.      this.dato = (T []) new Object[dimension];
  39.      this.contador = 0;
  40.  }
  41.  
  42. //----------------Metodos del objeto Genérico Pila-----------------//
  43.  // Verifica si la Pila se encuentra vacía.
  44.  public boolean empty() {
  45.      return this.contador <= 0;
  46.  }
  47.  
  48.  // Devuelve el dato en la cima de la pila sin sacarlo de la misma.
  49.  public T peek() {
  50.      if (this.empty()) {
  51.          throw new RuntimeException("La pila se encuenta vacía");
  52.      }
  53.      return this.dato[this.contador - 1];
  54.  }
  55.  
  56.  // Remueve el dato situado en la cima y devuelve el valor removido.
  57.  public T pop() {
  58.      if (this.empty()) {
  59.          throw new RuntimeException("La pila está vacía...");
  60.      }
  61.      --this.contador;
  62.      return this.dato[this.contador];
  63.  }
  64.  
  65.  // Superpone un dato en la cima de la Pila.
  66.  public T push(T element) {
  67.      if (this.size() >= this.dato.length) {
  68.  
  69.          T [] temp = (T []) new Object[this.dato.length * 2];
  70.          for (int i = 0; i < this.dato.length; ++i) {
  71.              temp[i] = this.dato[i];
  72.          }
  73.          this.dato = temp;
  74.      }
  75.      this.dato[this.contador] = element;
  76.      ++this.contador;
  77.      return element;
  78.  }
  79.  
  80.  // Returns the 1-based position where an object is on this stack.
  81.  public int search(Object object) {
  82.      for (int pos = this.contador - 1; pos >= 0; --pos) {
  83.          if (this.dato[pos].equals(object)) {
  84.              return this.contador - pos;
  85.          }
  86.      }
  87.      return -1;
  88.  }
  89.  
  90.  // from https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Vector.html
  91.  // Retorna el número de componenetes en este vector.
  92.  public int size() {
  93.      return this.contador;
  94.  }
  95.  
  96. //-----------------Mostrar el objeto Genérico Pila-----------------//
  97.  @Override
  98.  public String toString() {
  99.  
  100.      if (this.size() <=0) {
  101.          return "";
  102.      }
  103.  
  104.      // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  105.      StringBuilder sb = new StringBuilder();
  106.      sb.append("[" + this.dato[0].toString());
  107.      for (int i = 1; i < this.size(); ++i) {
  108.          sb.append(", " + this.dato[i].toString());
  109.      }
  110.      sb.append("]");
  111.      return sb.toString();
  112.  }
  113. }
  114.  
Add Comment
Please, Sign In to add comment