Latest News

Palindromo con pilas

Clase Pila

public class pila {
    private int tope;
    private char A[];     
   
    public pila(int n){
        A=new char[n];  //inicializacion del arreglo
        this.tope=-1;       
    }
   
    public void push(char x){
        tope++;
        A[tope]=x;       
    }
   
    public char pop(){
        char aux=A[tope];
        tope--;
        return aux;
    }
   
    public char stacktop(){
        char aux=pop();
        push(aux);
        return aux;
    }
   
    public boolean empty(){
        if(tope==-1){
            return true;
        }
        return false;
    }
   
    public boolean full(){
        if(tope==A.length-1){
            return true;
        }
        return false;
    }
}

Clase MAIN


public class Palindromo {
  
    public static void main(String[] args) throws IOException {
        BufferedReader leer=new BufferedReader(new InputStreamReader(System.in));
        int op=0;       
        String C, Cinv="";
        System.out.println("Ingrese una palabra");
        C=leer.readLine();      
       
        pila ob=new pila(C.length());
        pila ob2=new pila(C.length());
        pila ob3=new pila(C.length());
        do{
            System.out.println("1.-Insertar palabra");
            System.out.println("2.-Comprobar si es palindromo");           
            System.out.println("Elige una opcion");
            try{
            op=Integer.parseInt(leer.readLine());
            }catch(NumberFormatException e){
                System.out.println("Error de escritura");
            }
            switch(op){
                case 1:
                    if(!ob.full() && !ob2.full()){
                        for(int i=0;i<C.length();i++){
                            ob.push(C.charAt(i));
                            ob2.push(C.charAt(i));                          
                        }
                        while(ob3.full()!=true){
                            char d=ob.pop();                           
                            ob3.push(d);                            
                        }
                        if(ob3.full()==true && ob2.full()==true){
                            System.out.println("datos ingresados correctamente");
                        }
                    }else{
                        System.out.println("la píla esta llena");
                    }
                break;
                   
                case 2: 
                    char a = 0, b=0;
                    boolean band=true;
                    if(ob2.empty()!=true && ob3.empty()!=true){                       
                        while(ob2.empty()!=true && ob3.empty()!=true && band==true){
                            a=ob2.pop();
                            b=ob3.pop();
                            if(a==b){
                            band=true;
                            }else{
                                band=false;
                            }
                        }                       
                        if(band){
                            System.out.println(" la palabra es un palindromo");
                        }else{
                            System.out.println("la palabra no es palindromo");
                        }
                       
                    }else{
                        System.out.println("la pila esta vacia");  
                    }                       
                    break;                                                                       
                       
                case 3:
                    System.exit(0);
                    break;              
            }
        }while(op!=4);   
       
    }

1 comentario:

Ibit-Up Designed by Templateism.com Copyright © 2014

Con tecnología de Blogger.