SHOW:
|
|
- or go back to the newest paste.
| 1 | package tp2punto3; | |
| 2 | ||
| 3 | public class Pila {
| |
| 4 | int[] contenedor; | |
| 5 | int cuenta; | |
| 6 | int capacidad; | |
| 7 | ||
| 8 | ||
| 9 | public Pila() {
| |
| 10 | this.cuenta = 0; | |
| 11 | this.capacidad = 10; | |
| 12 | this.contenedor = new int[capacidad]; | |
| 13 | } | |
| 14 | ||
| 15 | public Pila(int capacidad) {
| |
| 16 | this.cuenta = 0; | |
| 17 | this.capacidad = capacidad; | |
| 18 | this.contenedor = new int[this.capacidad]; | |
| 19 | } | |
| 20 | ||
| 21 | public boolean estaLlena() {
| |
| 22 | return this.cuenta >= capacidad; | |
| 23 | } | |
| 24 | ||
| 25 | public boolean estaVacia() {
| |
| 26 | return this.cuenta <= 0; | |
| 27 | } | |
| 28 | ||
| 29 | public void push(int numero) {
| |
| 30 | if (this.estaLlena()) {
| |
| 31 | throw new RuntimeException("Error, la pila esta llena");
| |
| 32 | } | |
| 33 | this.contenedor[cuenta] = numero; | |
| 34 | ++this.cuenta; | |
| 35 | } | |
| 36 | ||
| 37 | public int pop() {
| |
| 38 | if(this.estaVacia()) {
| |
| 39 | throw new RuntimeException("Error, la pila está vacía");
| |
| 40 | } | |
| 41 | --cuenta; | |
| 42 | return this.contenedor[cuenta]; | |
| 43 | } | |
| 44 | ||
| 45 | public int peek() {
| |
| 46 | if(this.estaVacia()) {
| |
| 47 | throw new RuntimeException("Error, la pila está vacía");
| |
| 48 | } | |
| 49 | return this.contenedor[cuenta -1]; | |
| 50 | } | |
| 51 | } |