> 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/colas.md).

# Colas

1. Cola&#x20;

```java
package Colas;

public class Nodo<E> {
    private E elemento;
    private Nodo<E> 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
package Colas;

public class Cola<E> {
    private Nodo<E> primeiro;
    private Nodo<E> ultimo;
    private int tamaño;

    public Cola() {
        this.primeiro = null;
        this.ultimo = null;
        this.tamaño = 0;
    }

    public void add(E elemento) {
        Nodo<E> novo = new Nodo<>(elemento);
        if (primeiro == null) {
            primeiro = novo;
            ultimo = novo;
        } else {
            ultimo.setSiguiente(novo);
            ultimo = novo;
        }
        tamaño++;
    }

    public E peek() {
        return (primeiro == null) ? null : primeiro.getElemento();
    }

    public E poll() {
        if (primeiro == null) return null;

        E dato = primeiro.getElemento();
        primeiro = primeiro.getSiguiente();
        tamaño--;

        if (primeiro == null) {
            ultimo = null;
        }
        return dato;
    }

    public int size() {
        return tamaño;
    }

    @Override
    public String toString() {
        StringBuilder res =  new StringBuilder("Cola [ ");
        Nodo<E> actual = this.primeiro;
        while (actual!=null){
            res.append(actual.getElemento());
            actual = actual.getSiguiente();
            if (actual!=null)
                res.append(",");
        }
        res.append("]");
        return res.toString();
    }
}
```

```java
package Colas;

public class Persona {
    private String nome;

    public Persona(String nome) {
        this.nome = nome;
    }

    public String getNome() { return nome; }
    public void setNome(String nome) { this.nome = nome; }

    @Override
    public String toString() {
        return "Persona: " + nome;
    }
}

```

```java
package Colas;

public class App {

        public static void main(String[] args) {
            Cola<Persona> cola = new Cola<>();

            // 1. Crear e engadir 4 persoas
            cola.add(new Persona("Eva"));
            cola.add(new Persona("Rois"));
            cola.add(new Persona("Anxo"));
            cola.add(new Persona("Sabela"));

            // 2. Mostrar contido e tamaño
            System.out.println(cola.toString());
            System.out.println("Tamaño actual: " + cola.size());

            // 3. Recuperar primeiro (peek)
            System.out.println("Primeiro na fila (sen saír): " + cola.peek());
            System.out.println(cola.toString());

            // 4. Desencolar os 3 primeiros
            for (int i = 1; i <= 3; i++) {
                System.out.println("\nDesencolando... " + cola.poll());
                System.out.println("Estado da cola: " + cola.toString());
            }
        }
}

```

2. Cola doblemente enlazada:
   1. A clase Nodo debe de incluir un atributo denominado anterior de tipo Nodo, que apunte ao Nodo anterior
   2. Ao engadir un elemento, hai que xestionar catro punteiros:
      1. O seguinte de NodoNovo apunta a NodoSeguinte
         1. O anterior de NodoNovo apunta a NodoAnterior
         2. O  seguinte do NodoAnterior apunta a NodoNovo
         3. O anterior de NodoSeguinte apunta a NodoNovo
         4. Compre ter en conta que
            1. O anterior do primeiro e NULL
            2. O seguinte do último e NULL
   3. Ao eliminar o primeiro elemento, hai que xestionar adicionalmente, que ao eliminar o primeiro elemento, o anterior do segundo de debe de apuntar a nulo.
   4. peek() funciona igual
3. Cola Deque
   1. A clase Nodo debe de incluir un atributo denominado anterior de tipo Nodo, que apunte ao Nodo anterior
   2. Ao engadir un elemento, hai que xestionar catro punteiros:
      1. O seguinte de NodoNovo apunta a NodoSeguinte
         1. O anterior de NodoNovo apunta a NodoAnterior
         2. O  seguinte do NodoAnterior apunta a NodoNovo
         3. O anterior de NodoSeguinte apunta a NodoNovo
         4. Compre ter en conta que
            1. O anterior do primeiro e NULL
            2. O seguinte do último e NULL
   3. Ao eliminar un elemento
      1. Se borramos o primeiro:
         1. O seguinte ao primerio será o novo NodoPrimeiro
      2. Se borramos o ultimo:
         1. O anterior ao último será o novo NodoUltimo
   4. pee() non cambia.


---

# 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/colas.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.
