Advertisement
libardorengifo

Pila dinamica en java

Dec 1st, 2020
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. /*
  2.  * Ejercicio que realiza las operaciones básicas de una Pila.
  3.  * Utiliza tipos de datos Genericos.
  4.  * Cumple con los principios básicos de la Poo.
  5.  * ESTE ARCHIVO FUE CREADO POR EL PROFESOR NO MODIFICAR <--------------
  6.  */
  7. package pkg8.reinas.Stack;
  8.  
  9. public class piladinamica<T> {
  10.  
  11.     private Nodo<T> top;
  12.     private int tamanio;
  13.  
  14.     public piladinamica() {
  15.         top = null;
  16.         this.tamanio = 0;
  17.  
  18.     }
  19.  
  20.     public boolean IsEmpty() {
  21.         return top == null;
  22.     }
  23.  
  24.     public int size() {
  25.         return this.tamanio;
  26.     }
  27.  
  28.     public T top() {
  29.         if (IsEmpty()) {
  30.             return null;
  31.         } else {
  32.             T elemento = top.getElemento();
  33.             return elemento;
  34.         }
  35.     }
  36.  
  37.     public T pop() {
  38.         if (IsEmpty()) {
  39.             return null;
  40.         } else {
  41.             T elemento = top.getElemento();
  42.             Nodo<T> aux = top.getSiguiente();
  43.             top = null; // liberar memoria
  44.             top = aux;
  45.             this.tamanio--;
  46.             return elemento;
  47.         }
  48.     }
  49.  
  50.     public void push(T elemento) {
  51.         Nodo<T> aux = new Nodo<>(elemento, top);
  52.         top = aux;
  53.         this.tamanio++;
  54.     }
  55.  
  56.     @Override
  57.     public String toString() {
  58.         if (IsEmpty()) {
  59.             return "La pila esta Vacia";
  60.         } else {
  61.             String resultado = "";
  62.             Nodo<T> aux = top;
  63.             while (aux != null) {
  64.                 resultado += aux.toString();
  65.                 aux = aux.getSiguiente();
  66.  
  67.             }
  68.             return resultado;
  69.         }
  70.     }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement