Advertisement
mamelui

PilaLinkedCounter

May 25th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. /**
  2.  * @file PilaLinkedCounter.java
  3.  * @brief Fuente de la clase genérica PilaLinkedCounter.
  4.  */
  5. package lab01;
  6.  
  7. /**
  8.  * Clase genérica de pilas.
  9.  *
  10.  * Esta clase implementa la pila a través de elementos enlazados.
  11.  * @param T Tipo de los elementos en la pila.
  12.  * @since 5/18/2015
  13.  */
  14. public class PilaLinkedCounter<T> implements InterfacePila<T> {
  15.  
  16.     /**
  17.      * Referencia al elemento al tope de la pila.
  18.      */
  19.     public  PilaLinkedCounter() {
  20.         tope=null;
  21.         counter = new Counter();
  22.        
  23.     }
  24.     public PilaLinkedCounter(Counter counter) {
  25.         tope= null;
  26.         this.counter=counter;
  27.  
  28.     }
  29.    
  30.     Elemento<T> tope;
  31.     Counter counter;
  32.     public Counter getCounter() {
  33.         return counter;
  34.     }
  35.  
  36.     public void setCounter(Counter counter) {
  37.         this.counter = counter;
  38.     }
  39.  
  40.     /**
  41.      * Crea una pila vacía.
  42.      */
  43.    
  44.    
  45.     /* (non-Javadoc)
  46.      * @see lab01.InterfacePila#push(java.lang.Object)
  47.      */
  48.     @Override
  49.     public InterfacePila<T> push(T t) throws IllegalArgumentException {
  50.         counter.inc();
  51.         if ( t == null )
  52.             throw new IllegalArgumentException("Null can't be inserted.");
  53.         Elemento<T> e= new Elemento<T>();
  54.         e.value= t;
  55.         e.below= tope;
  56.         tope= e;
  57.         counter.inc(5);
  58.         return this;
  59.     }
  60.  
  61.     /* (non-Javadoc)
  62.      * @see lab01.InterfacePila#peek()
  63.      */
  64.     @Override
  65.     public T peek() {
  66.         counter.inc();
  67.         if ( tope == null )
  68.             return null;
  69.         counter.inc();
  70.         return tope.value;
  71.     }
  72.  
  73.     /* (non-Javadoc)
  74.      * @see lab01.InterfacePila#pop()
  75.      */
  76.     @Override
  77.     public T pop() {
  78.         counter.inc();
  79.         if ( tope == null )
  80.             return null;
  81.         T t= tope.value;
  82.         tope= tope.below;
  83.         counter.inc(3);
  84.         return t;
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement