Java - Pila en Java

 
Vista:

Pila en Java

Publicado por Jane (1 intervención) el 19/10/2018 04:22:28
Hola, quiero hacer una pila en java, que ingrese datos y que se puedan eliminar.
este es mi código, pero a la hora de eliminar datos, se eliminan todos los que están en la pila uno por uno los va mostrando, quiero que solo se elimine un dato y que regrese al menú. Ojala alguien pueda orientarme.
Muchas gracias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package pilas;
 
import javax.swing.JOptionPane;
 
public class Pilas {
 
    public static void main(String[] args) {
        int op=0;
        int [] pila=new int[0];
        do{
            op=Integer.parseInt(JOptionPane.showInputDialog(null,"Menú\n 1.-tamaño\n 2.-Agregar\n 3.- Atender \n 10.- Salir"));
            switch(op){
                case 1:
                    JOptionPane.showMessageDialog(null,"El tamaño es: "+pila.length);break;
                case 2:
                    pila=agregar(pila);break;
                case 3:
                    pila=atender(pila);break;
            }
        }while(op!=10);
    }
     private static int[] agregar(int []col){
         int tamanio=col.length;
         int [] colonTemp=new int[tamanio+1];
         for (int i=0; i<=tamanio; i++){
         if(i==tamanio){
            colonTemp[i]=Integer.parseInt(JOptionPane.showInputDialog("Ingrese el dato a agregar"));
         }
         else{
            colonTemp[i]=col[i];
         }
         }
         return colonTemp;
    }
     private static int[] atender(int []col){
         int tamanio=col.length;
         if(tamanio==0){
            JOptionPane.showMessageDialog(null,"La cola esta vacía");
         }else{
           int [] colonTemp=new int[tamanio-1];
            for (int i=0; i<tamanio; i++){
            tamanio--;
           JOptionPane.showMessageDialog(null,"El número atendido es "+col[tamanio]);
         }
         col=colonTemp;
              }
          return col;
     }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Pila en Java

Publicado por Billy Joel (876 intervenciones) el 23/10/2018 22:01:30
Lo he resuelto de otra forma, pero antes de empezar hay que tener en cuenta lo siguiente
En un array, lista, pila o cola no sabemos cuantos elementos va llegar a tener.
Java nos ofrece estructuras de datos como java.util.List o como en este ejemplo he usado java.util.Stack que sirve para representar a una LIFO (last-in-first-out)

Bueno lo resolví así:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
 
public class Pilas {
 
    public static final int OPCION_SALIR = 0;
    public static final int OPCION_MOSTRAR = 1;
    public static final int OPCION_MOSTRAR_TOP = 2;
    public static final int OPCION_AGREGAR_ELEMENTO = 3;
    public static final int OPCION_ELIMINAR_ELEMENTO = 4;
 
    public static void main(String[] args) {
        int opcion = -1;
        Stack<Integer> pila = new Stack();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (opcion != 0) {
            System.out.println("Introduzca 0 para salir");
            System.out.println("Introduzca 1 para mostrar la pila");
            System.out.println("Introduzca 2 para mostrar el top de la pila");
            System.out.println("Introduzca 3 para agregar elemento");
            System.out.println("Introduzca 4 para eliminar elemento");
            try {
                opcion = Integer.parseInt(br.readLine());
            } catch (IOException | NumberFormatException ex) {
                opcion = -1;
            }
            switch (opcion) {
                case OPCION_SALIR:
                    System.out.println("Adios...");
                    break;
                case OPCION_MOSTRAR:
                    System.out.println("OPCION MOSTRAR");
                    if (pila.isEmpty()) {
                        System.out.println("No hay elementos en la pila");
                    } else {
                        for (int i = 0; i < pila.size(); i++) {
                            System.out.println(i + ": " + pila.get(i));
                        }
                        System.out.println("-------------------------------------");
                    }
                    break;
                case OPCION_MOSTRAR_TOP:
                    System.out.println("OPCION MOSTRAR TOP");
                    if (pila.isEmpty()) {
                        System.out.println("No hay elementos en la pila");
                    } else {
                        System.out.println("El elemento que se encuentra en el tope de la pila es: " + pila.peek());
                    }
                    break;
                case OPCION_AGREGAR_ELEMENTO:
                    System.out.println("OPCION AGREGAR ELEMENTO");
                    System.out.println("Introduzca el nuevo elemento que quiere agregar a la pila");
                    try {
                        pila.add(Integer.parseInt(br.readLine()));
                    } catch (IOException | NumberFormatException ex) {
                        System.out.println("No se pudo agregar el elemento, solo se permiten números enteros");
                    }
                    break;
                case OPCION_ELIMINAR_ELEMENTO:
                    System.out.println("OPCION ELIMINAR ELEMENTO");
                    if (pila.isEmpty()) {
                        System.out.println("No hay elementos en la pila");
                    } else {
                        int elemento_eliminado = pila.pop();
                        System.out.println("Se ha eliminado el elemento: " + elemento_eliminado);
                    }
                    break;
                default:
                    System.out.append("opción incorrecta");
                    break;
            }
        }
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar