> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud6-solucions/pilas.md).

# Pilas

1. Clase Pila

```java
package Listas;

public class Nodo<E> {
    private E elemento;
    private Nodo siguiente;

    public Nodo(E elemento) {
        this.elemento = elemento;
        this.siguiente = null;
    }

    public E getElemento() {
        return elemento;
    }

    public void setElemento(E elemento) {
        this.elemento = elemento;
    }

    public Nodo getSiguiente() {
        return siguiente;
    }

    public void setSiguiente(Nodo siguiente) {
        this.siguiente = siguiente;
    }

    @Override
    public String toString() {
        return "Nodo{" +
                "elemento=" + elemento +
                '}';
    }
}
```

```java
public class Pila<E> {

    private Nodo<E> cima;
    private int tamano;

    public Pila() {
        this.cima = null;
        this.tamano = 0;
    }

    // Engade un elemento á parte superior
    public void push(E elemento) {
        Nodo<E> novoNodo = new Nodo<>(elemento);
        if (cima != null) {
            novoNodo.setSiguiente(cima);
        }
        cima = novoNodo;
        tamano++;
    }

    // Retira e devolve o elemento da cima
    public E pop() {
        if (isEmpty()) {
            return null; 
        }
        E elemento = cima.getElemento();
        cima = cima.getSiguiente();
        tamano--;
        return elemento;
    }

    // Mira o elemento da cima sen retiralo
    public E peek() {
        if (isEmpty()) {
            return null;
        }
        return cima.getElemento();
    }

    // Devolve o número de elementos
    public int size() {
        return tamano;
    }

    // Comproba se a pila está baleira (O que che faltaba!)
    public boolean isEmpty() {
        return cima == null;
    }
    
    @Override
    public String toString() {
        if (isEmpty()) {
            return "Pila baleira";
        }
    
        StringBuilder sb = new StringBuilder();
        sb.append("PILA{");
        
        Nodo<E> actual = cima;
        while (actual != null) {
            sb.append("[ ").append(actual.getElemento()).append(" ]\n");
            actual = actual.getSiguiente();
        }
        sb.append(" }");
        
        return sb.toString();
    }
}
```

```java
package Pilas;

import javax.swing.*;
import java.awt.*;

public class Navegador extends JFrame {

    private JPanel panSup, panCent, panInf;
    private JLabel campoURL, webActual, pilaHistorial;
    private JTextField nuevaURL;
    private JButton anadir, atras;

    public Navegador(){
        // Configuración da ventá
        setTitle("Simulador de Historial");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        this.campoURL = new JLabel("URL:");
        this.nuevaURL = new JTextField("Introduzca una web");
        this.nuevaURL.setPreferredSize(new Dimension(80,20));
        this.webActual = new JLabel("Pagina de inicio");
        this.webActual.setFont(new Font("Arial", Font.BOLD, 16));
        this.pilaHistorial = new JLabel("Pila[]");
        this.anadir = new JButton("Visitar");
        this.atras = new JButton("Atrás");


        this.panSup = new JPanel();
        this.panSup.setLayout(new FlowLayout());
        this.panSup.setSize(new Dimension(400,50));
        this.panSup.add(campoURL);
        this.panSup.add(nuevaURL);

        this.panCent = new JPanel(new GridLayout(2,1));
        this.panCent.add(webActual);
        this.panCent.add(pilaHistorial);

        this.panInf = new JPanel();
        this.panInf.setLayout(new FlowLayout());
        this.panInf.setSize(new Dimension(400,50));
        this.panInf.add(atras);
        this.panInf.add(anadir);


        this.add(panSup, BorderLayout.NORTH);
        this.add(panCent, BorderLayout.CENTER);
        this.add(panInf, BorderLayout.SOUTH);

        this.setVisible(true);


    }

    public JPanel getPanSup() {
        return panSup;
    }

    public JPanel getPanCent() {
        return panCent;
    }

    public JPanel getPanInf() {
        return panInf;
    }

    public JLabel getCampoURL() {
        return campoURL;
    }

    public JLabel getWebActual() {
        return webActual;
    }

    public JTextField getNuevaURL() {
        return nuevaURL;
    }

    public JButton getAnadir() {
        return anadir;
    }

    public JButton getAtras() {
        return atras;
    }

    public JLabel getPilaHistorial() {
        return pilaHistorial;
    }
}

```

```java
 package Pilas;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Controlador {

    private Navegador navegador;
    private Pila<String> pila;

    public Controlador(Navegador n, Pila<String> pilaHistorial){
        this.navegador = n;
        this.pila = pilaHistorial;

        this.navegador.getAnadir().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String url = navegador.getNuevaURL().getText();
                if (!url.isEmpty()){
                    pila.push(url);
                    navegador.getWebActual().setText(url);
                    navegador.getNuevaURL().setText("");
                    navegador.getPilaHistorial().setText(pila.toString());
                }
            }
        });

        this.navegador.getAtras().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!pila.isEmpty()){
                    String anterior = pila.pop();
                    navegador.getWebActual().setText(anterior);
                    navegador.getPilaHistorial().setText(pila.toString());
                }
            }
        });

    }


}

```

```java
package Pilas;

import ClasesAbstractas.dispositivos.Conectable;

public class App {

    public static void main(String[] args) {
        Navegador navegador = new Navegador();
        Pila<String> pila = new Pila<>();
        Controlador cont = new Controlador(navegador,pila);
    }
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud6-solucions/pilas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
