"La generación de código, como beber alcohol, es bueno si se hace con moderación"
-- Alex Lowe
hola amigos aqui les dejo otro proyecto de pilas son muy sencillo pero a final de cuentas cumple con el objetivo de la manera en como funciona las pilas,....
AQUI el link de descarga para el codigo fuente
package
pila1;
public class
Pila1 { // se crea la clase
public static void main(String[] args) {
/*SE CREA Y SE INSTANCIA EL OBJETO
DEL TIPO DE LA CLASE: Pilando*/
pilando
objPilando = new pilando();
/*SE LLAMAN A LOS MÉTODOS DE LA CLASE:
Pilando*/
objPilando.ingresar();//
permite ingresar datos a la pila
objPilando.show1();//
datos ingresados
objPilando.show2();
objPilando.show3();
objPilando.sacar();//
saca elementos de la píla
}
}
package pila1;
import java.util.Stack;
public class
pilando { // se crea la subclase
/*CREANDO LA PILA*/
Stack pila =
new Stack();
/*INGRESANDO
ELEMENTOS A LA PILA En este caso se
agregaran los numeros 1, 2 y 3*/
public void ingresar()
{
for (int x=1;x<=3;x++)
{
pila.push(Integer.toString(x));
}
}
/*VACIA LA PILA, EN CASO DE ENCONTRARSE
VACIA, MUESTRA EL MENSAJE "La pila está vacía!!!"*/
public void sacar()
{
while (this.empty() == 1)
{
System.out.println("Sacando el
elemento "+pila.pop());
}
System.out.println("La pila esta
vacía!!!");
}
/*
*
MUESTRA EL PRIMER ELEMENTO QUE FUE AGREGADO A LA PILA
*
*/
public void
show1()
{
System.out.println("El
primer elemento agregado es ---> "+pila.firstElement().toString());
}
/*
*
MUESTRA EL ULTIMO ELEMENTO QUE FUE AGREGADO A LA PILA
*
*/
public void
show2()
{
/*
*
MUESTRA EL ULTIMO ELEMENTO QUE FUE AGREGADO A LA PILA
*
*/
System.out.println("El
ultimo elemento agregado es ---> "+pila.lastElement().toString());
}
/*
*
MUESTRA EL TAMAÑO DE LA PILA -LA CANTIDAD DE ELEMENTOS DE LA PILA-
*
*/
public void
show3()
{
System.out.println("La
cantidad de elementos es de ---> "+pila.size());
}
/*
* DE VUELVE 0 SI LA PILA ESTA VACIA
* */
public int empty()
{
int valid = 1;
if(!pila.empty())
{
valid = 1;
}
else
if(pila.empty())
{
valid = 0;
}
return
valid;
}
}
No hay comentarios:
Publicar un comentario