package Pila;
public class PilaS
{
private int Tope = -1;
private int Max;
private int Pila[];
public int DatoPila;
public PilaS(int CantMax)
{
Pila = new int[CantMax];
Max = CantMax;
}
private boolean PilaVacia()
{
boolean resp = false;
if(Tope == -1)
resp = true;
return resp;
}
private boolean PilaLlena()
{
boolean resp = false;
if((Tope-1) == Max)
resp = true;
return resp;
}
public boolean InsertarPila(int Dato)
{
boolean resp = false;
if(!PilaLlena())
{
Tope++;
Pila[Tope] = Dato;
resp = true;
}
return resp;
}
public boolean RemoverPila()
{
boolean resp = false;
if(!PilaVacia())
{
DatoPila = Pila[Tope];
Tope--;
resp = true;
}
return resp;
}
public boolean VerPila()
{
boolean resp = false;
if(!PilaVacia())
{
DatoPila = Pila[Tope];
resp = true;
}
return resp;
}
}
Clase ProyectosPilas:
Este contiene el main y demas funciones.
package Pila;
public class ProyectosPilas
{
public static void main(String[] args)
{
//Pila Normal Mostrar Elementos inverso a como fueron insertados
Ej0();
//Pila Insertamos y mostramos en otra Pila de forma que fueron insertados
Ej1();
//Pila Insertamos y mostramos en la Pila Principal o 0 de forma que fueron insertados
Ej2();
}
private static void Ej0()
{
int num = read.dataInt("Total de Elementos en la Pila: ");
PilaS P = new PilaS(num);
for(int x = 0; x < num; x++)
{
P.InsertarPila(read.dataInt("Dato a Insertar[" + x + "]:"));
}
while(P.RemoverPila())
SOP(P.DatoPila);
}
private static void Ej1()
{
int num = read.dataInt("Total de Elementos en la Pila: ");
PilaS P = new PilaS(num);
PilaS P0 = new PilaS(num);
for(int x = 0; x < num; x++)
{
P.InsertarPila(read.dataInt("Dato a Insertar[" + x + "]:"));
}
while(P.RemoverPila())
P0.InsertarPila(P.DatoPila);
while(P0.RemoverPila())
SOP(P0.DatoPila);
}
private static void Ej2()
{
int num = read.dataInt("Total de Elementos en la Pila: ");
PilaS P = new PilaS(num);
PilaS P0 = new PilaS(num);
for(int x = 0; x < num; x++)
{
P.InsertarPila(read.dataInt("Dato a Insertar[" + x + "]:"));
}
while(P.RemoverPila())
P0.InsertarPila(P.DatoPila);
//P = P0;
PilaS P1 = new PilaS(num);
while(P0.RemoverPila())
P1.InsertarPila(P0.DatoPila);
while(P1.RemoverPila())
P.InsertarPila(P1.DatoPila);
while(P.RemoverPila())
SOP(P.DatoPila);
}
private static void SOP(int msj)
{
System.out.println(msj);
}
}
Clase read:
Esta clase no la cree yo sino un amigo Jackz(cryptt3r) con la que siempre utilizo para leer jeje
package Pila;
import java.io.*;
/**
* @author cryptter
*/
public class read
{
public static boolean error;
public static String data(String message) {
String sdato = "";
System.out.print(message);
try {
// Definir un flujo de caracteres de entrada: flujoE
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader flujoE = new BufferedReader(isr);
// Leer. La entrada finaliza al pulsar la tecla Entrar
sdato = flujoE.readLine();
}
catch(IOException e) {
System.err.println("Error: " + e.getMessage());
}
return sdato; // devolver el dato tecleado
}
public static int dataInt(String message){
try {
error=false;
int val=Integer.parseInt(data(message));
if (val<-32768 data-blogger-escaped-val="">32767)
error=true;
return val;
}
catch(NumberFormatException e) {
return Integer.MIN_VALUE; // valor más pequeño
}
}
public static short datoShort(String message){
try {
return Short.parseShort(data(message));
}
catch (NumberFormatException e){
return Short.MIN_VALUE;
}
}
public static long dataLong(String message){
try {
return Long.parseLong(data(message));
}
catch (NumberFormatException e){
return Long.MIN_VALUE;
}
}
public static float dataFloat(String message){
try {
Float f = new Float(data(message));
return f.floatValue();
}
catch (NumberFormatException e){
return Float.NaN;
}
}
public static double dataDouble(String message){
try {
Double d = new Double(data(message));
return d.doubleValue();
}
catch (NumberFormatException e){
return Double.NaN;
}
}
/*public static char dataChar(String message){
try {
Char d = new Char(data(message));
return d.charValue();
}
catch (NumberFormatException e){
return Char.NaN;
}
}*/
}
0 comentarios:
Publicar un comentario