Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Stack<Elemento> {
- private int dimension;
- private Elemento [] datos;
- private int cuenta;
- public Stack() {
- this(10);
- }
- public Stack(int dimension) {
- this.dimension = dimension;
- this.datos = (Elemento[]) new Object[dimension];
- this.cuenta = 0;
- }
- public void push(Elemento elemento) {
- if (this.cuenta >= this.dimension) {
- throw new RuntimeException("La pila está llena...");
- }
- this.datos[this.cuenta] = elemento;
- ++this.cuenta;
- }
- public Elemento pop() {
- if (this.empty()) {
- throw new RuntimeException("La pila está vacía...");
- }
- --this.cuenta;
- return this.datos[this.cuenta];
- }
- public Elemento peek() {
- if (this.empty()) {
- throw new RuntimeException("La pila está vacía...");
- }
- return this.datos[this.cuenta - 1];
- }
- public boolean empty() {
- return this.cuenta <= 0;
- }
- public int count() {
- return this.cuenta;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement