-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCola.hpp
More file actions
51 lines (43 loc) · 974 Bytes
/
Cola.hpp
File metadata and controls
51 lines (43 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef BATTLESHIP_COLA_HPP
#define BATTLESHIP_COLA_HPP
#include "Nodo.hpp"
template <class T> class Cola {
private:
// variables de instancia
Nodo<T>* inicio = nullptr;
Nodo<T>* fin = nullptr;
public:
// funciones
Cola() {};
Cola(T dato) {encolar(dato);};
void encolar(T dato);
T desencolar();
};
// definiciones de las funciones no-inline de la clase
template<typename T>
void Cola<T>::encolar(T dato) {
if(inicio) {
// si no esta vacia
Nodo<T>* nuevoNodo = new Nodo<T>(dato);
fin->next = nuevoNodo;
fin = nuevoNodo;
} else {
// si esta vacia
fin = inicio = new Nodo<T>(dato);
}
}
template<typename T>
T Cola<T>::desencolar() {
// el primer de la fila debe salir
if(inicio) {
// copy initialization
T datoADevolver(inicio->dato);
Nodo<T>* nodoABorrar = inicio;
inicio = inicio->next;
delete nodoABorrar;
return datoADevolver;
} else {
return nullptr;
}
}
#endif