
TAD PILA
Snippet options
Download: Download snippet as tad-pila.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!
import java.util.ArrayList; public class Pila<T> { ArrayList<T> pila; Pila(){//método constructor pila = new ArrayList<T>(); } Boolean vacia(){ //return pila.isEmpty(); esto lo hice yo return pila.size()==0; } void agregar(T i){//Agrega a la cima pila.add(i); } T quitar(){ //Quita de la cima el último elemento T elem = this.cima();// da el elemnto de la posicion que le digo(antes puso esto :pila.get(pila.size() - 1); pila.remove(elem);// antes hicimos : pila.remove(pila.size() - 1) pero no expresa bien el concepto de pila return elem; } T cima(){// Devuelve la cima sin quitarla (consulta el último elemento) return pila.get(pila.size() - 1); } ////////////////////////////////// b) T minimo(){ //obtiene y quita el mìnimo de la pila Pila aux = new Pila(); T elem = this.cima(); while(!this.vacia()){ // O(1) if(this.cima().compareTo(elem) == -1) elem = this.cima(); // orden de complejidad del while O(n) aux.agregar(this.quitar()); } while(!aux.vacia()){ if(!elem.equals(aux.cima())) this.agregar((T)aux.quitar()); // hicimos un casteo } return elem; } void ordenar(){//para calcular la complejidad miro la pila (dado n cantidad elementos de la pila //mirar la complejidad de los mètodos que llama (la complejidad de una pila no es siempre la misma depende // de la estructura que estemos utilizando). Pila aux = new Pila(); T elem; while(!this.vacia()){ //O(1) elem = this.minimo(); // O(n2) (lo trae de el procedimienot minimo) aux.agregar(elem); // O(1) } while(!aux.vacio()){ this.agregar(aux.quitar()); } } public static void main(String[] args) { // TODO Auto-generated method stub } }
Create a free my code stock.com account now.
my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!
Find out more and register nowYou can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.