Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file PilaLinkedCounter.java
- * @brief Fuente de la clase genérica PilaLinkedCounter.
- */
- package lab01;
- /**
- * Clase genérica de pilas.
- *
- * Esta clase implementa la pila a través de elementos enlazados.
- * @param T Tipo de los elementos en la pila.
- * @since 5/18/2015
- */
- public class PilaLinkedCounter<T> implements InterfacePila<T> {
- /**
- * Referencia al elemento al tope de la pila.
- */
- public PilaLinkedCounter() {
- tope=null;
- counter = new Counter();
- }
- public PilaLinkedCounter(Counter counter) {
- tope= null;
- this.counter=counter;
- }
- Elemento<T> tope;
- Counter counter;
- public Counter getCounter() {
- return counter;
- }
- public void setCounter(Counter counter) {
- this.counter = counter;
- }
- /**
- * Crea una pila vacía.
- */
- /* (non-Javadoc)
- * @see lab01.InterfacePila#push(java.lang.Object)
- */
- @Override
- public InterfacePila<T> push(T t) throws IllegalArgumentException {
- counter.inc();
- if ( t == null )
- throw new IllegalArgumentException("Null can't be inserted.");
- Elemento<T> e= new Elemento<T>();
- e.value= t;
- e.below= tope;
- tope= e;
- counter.inc(5);
- return this;
- }
- /* (non-Javadoc)
- * @see lab01.InterfacePila#peek()
- */
- @Override
- public T peek() {
- counter.inc();
- if ( tope == null )
- return null;
- counter.inc();
- return tope.value;
- }
- /* (non-Javadoc)
- * @see lab01.InterfacePila#pop()
- */
- @Override
- public T pop() {
- counter.inc();
- if ( tope == null )
- return null;
- T t= tope.value;
- tope= tope.below;
- counter.inc(3);
- return t;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement