Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /clase
- package termaturgia;
- public class Apilar {
- int vectorpila[];
- int cima;
- public Apilar(int tamaño) {
- vectorpila = new int[tamaño];
- cima = 0;
- }
- public void insertar(int dato) {
- vectorpila[cima] = dato;
- cima++;
- }
- public int eliminar() {
- int eliminar = 0;
- if (cima == 0) {
- System.out.println("no hay datos en la pila");
- } else {
- eliminar = vectorpila[cima];
- cima--;
- }
- return eliminar;
- }
- public boolean vacio() {
- if (cima == 0) {
- return true;
- } else {
- return false;
- }
- }
- public int tamaño() {
- return vectorpila.length;
- }
- }
- /main
- package termaturgia;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- public class Termaturgia {
- public static void main(String[] args) {
- // codigo
- Scanner sc = new Scanner (System.in);
- System.out.println("Bienvenido");
- System.out.println("Digite el tamaño de la pila");
- int tamaño = sc.nextInt();
- int dato = 0, opc = 0;
- Apilar a = new Apilar (tamaño);
- do {
- System.out.println("Que desea hacer");
- System.out.println("1. Ingresar dato ---- 2. Eliminar dato --- 3. Conocer si la pila esta vacia");
- System.out.println("4. Tamaño de la pila --- 5. Salir");
- opc = sc.nextInt();
- switch (opc){
- case 1:
- System.out.println("Inserte los datos:");
- a.insertar(dato = sc.nextInt());
- System.out.println("el dato ha sido insertado");
- System.out.println("");
- break;
- case 2:
- a.eliminar();
- break;
- case 3:
- System.out.println(a.vacio());
- System.out.println("");
- break;
- case 4:
- System.out.println(a.tamaño());
- break;
- }
- } while (opc !=5);}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement