Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package clases;
- import java.util.ArrayList;
- import interfaces.Interfaz5_InterfazColeccion;
- public class Pila implements IColeccion {
- private ArrayList<Integer> pila;
- private int contadorObjetos;
- public Pila () {
- pila = new ArrayList<Integer>();
- contadorObjetos = 0;
- }
- @Override
- public boolean estaVacia() {
- boolean rta = false;
- if (contadorObjetos == 0) {
- rta = true;
- }
- return rta;
- }
- @Override
- public Object extraer() {
- Integer tope = null;
- if (!estaVacia()) {
- tope = pila.set(contadorObjetos - 1, null); // me pisa el ultimo valor y me retorna el "tope" o null si
- contadorObjetos--; // estaba vacĂa
- }
- return tope;
- }
- @Override
- public Object primero() {
- Integer tope = null;
- if (!estaVacia()) {
- tope = pila.get(contadorObjetos - 1);
- }
- return tope;
- }
- @Override
- public boolean agregar(Object obj) {
- boolean exito = false;
- if (obj != null) {
- if (obj instanceof Integer) {
- Integer aux = (Integer) obj;
- pila.add(aux);
- contadorObjetos++;
- exito = true;
- }
- }
- return exito;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment