Advertisement
jtentor

DemoStack1 - Java - Stack.java

May 10th, 2020
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1.  
  2. public class Stack {
  3.  
  4.     private int dimension;
  5.     private char [] datos;
  6.     private int cuenta;
  7.  
  8.     public Stack() {
  9.         this(10);
  10.     }
  11.  
  12.     public Stack(int dimension) {
  13.         this.dimension = dimension;
  14.         this.datos = new char[this.dimension];
  15.         this.cuenta = 0;
  16.     }
  17.  
  18.     public void push(char elemento) {
  19.         if (this.cuenta >= this.dimension) {
  20.             throw new RuntimeException("La pila está llena...");
  21.         }
  22.         this.datos[this.cuenta] = elemento;
  23.         ++this.cuenta;
  24.     }
  25.  
  26.     public char pop() {
  27.         if (this.empty()) {
  28.             throw new RuntimeException("La pila está vacía...");
  29.         }
  30.         --this.cuenta;
  31.         return this.datos[this.cuenta];
  32.     }
  33.  
  34.     public char peek() {
  35.         if (this.empty()) {
  36.             throw new RuntimeException("La pila está vacía...");
  37.         }
  38.         return this.datos[this.cuenta - 1];
  39.     }
  40.  
  41.     public boolean empty() {
  42.         return this.cuenta <= 0;
  43.     }
  44.  
  45.     public int count() {
  46.         return this.cuenta;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement