You are on page 1of 6

Pilas en 

Java
with 9 comments

Bueno, voy a poner el código de un programa en Java que hace uso de pilas. En este
programa se le ingresa una palabra (hasta las groserías funcionan…), y separa las letras
y las imprime de una en una de abaja hacia arriba:

Ejemplo: Si se escribe la palabra JAVA.


La impresión sera:
A
V
A
J

Bueno, aquí esta el código:

view source

print?

01 import java.io.*;
02 import java.util.Scanner;
03 public class PilaCadena{
04        public static void main( String args[] ){
05        Scanner leer = new Scanner(System.in);
06        operapilaschar Obj = new operapilaschar();
07   
08        String cadena;
09        char ch;
10   
11        System.out.println( "Escribe texto:" );
12        cadena = leer.next();

13   
14        for( int i=0; i<cadena.length(); i++ ){
15         ch = cadena.charAt(i);
16         Obj.push(ch);
17         }
18   
19         for( int i=0; i<cadena.length(); i++ ){
20             Obj.pop();
21             System.out.println( Obj.dret );
22             }
23        }
24 }

Hace uso de los métodos de la siguiente clase:

view source

print?

001 class operapilaschar


002   
003   {
004   
005     public static char dret;
006   
007     public static int max;
008   
009     public static char pila[];
010   
011     public static int tope = -1;
012   
013     public operapilaschar()
014   
015       {
016   
017     max=20;
018   
019     pila=new char [max];
020   
021       }
022   
023     public operapilaschar(int n)
024   
025       { 
026   
027      max=n-1;
028   
029      pila = new char [max];
030   
031       }         
032   
033  public static boolean pila_Llena(int tope,int max)
034   
035   {
036   
037      boolean llena;
038   
039      if (tope==max)
040   
041           llena=true;
042   
043         else
044   
045           llena=false;
046   
047      return llena;
048   
049   }         
050   
051  public static boolean pila_Vacia(int tope)
052   
053   {
054   
055      boolean vacia;
056   
057      if (tope == -1)
058   
059         vacia=true;
060   
061        else
062   
063         vacia=false;
064   
065      return vacia;
066   
067   }
068   
069  public static void push(char dato)
070   
071  {
072   
073     if(pila_Llena(tope,max))
074   
075       System.out.println("!Cuidado!, Desbordamiento!!!!!");
076   
077        else
078   
079          {
080   
081             tope++;
082   
083             pila[tope]=dato;// pone el nuevo dato en la pila
084   
085          }
086   
087  }          
088   
089 public static void pop()
090   
091  {
092   
093     if (pila_Vacia(tope))
094   
095        System.out.println("!Cuidado!, Subdesbordamiento!!!!!");
096   
097     else {
098   
099           dret=pila[tope];
100   
101           tope--;
102   
103           }// actualiza tope y se elimina elemento en el tope
104   
105  }
106   
107 public static boolean compara(int dret,int ch)
108   
109  {
110   
    if (dret=='(' && ch==')' || dret=='{' && ch=='}'
111
|| dret=='[' && ch==']')
112   
113       return true;
114   

115       else 

116   
117       return false;
118   
119  }
120   
121  public static void estado()
122   
123   {
124   
125     int i;
126   
127     System.out.println(" El estado de la pila es : ");
128   
129     System.out.println(" --------------------------");
130   
131     for(i=0;i<=tope;i++)
132   
133      {
134   
135         System.out.println("pila["+i+"] : "+pila[i]);
136   
137      }
138   
139   }
140 }

You might also like