-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolaDinamica.hpp
88 lines (80 loc) · 2.06 KB
/
colaDinamica.hpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
using namespace std;
template <typename T> struct colaDinamica;
template <typename T> void inicializar(colaDinamica<T>& c);
template <typename T> bool esVacia(colaDinamica<T> c);
template <typename T> void anyadir(colaDinamica<T>& c, T elemento);
template <typename T> void mostrar(colaDinamica<T> c);
template <typename T> void eliminar(colaDinamica<T>& c);
template <typename T>
struct colaDinamica {
private:
struct Nodo{
T dato;
Nodo* sig;
};
Nodo* primero;
Nodo* ultimo;
int numElem;
public:
friend void inicializar<T>(colaDinamica<T>& c);
friend bool esVacia<T>(colaDinamica<T> c);
friend void anyadir<T>(colaDinamica<T>& c, T elemento);
friend void mostrar<T>(colaDinamica<T> c);
friend void eliminar<T>(colaDinamica<T>& c);
};
template <typename T>
void inicializar(colaDinamica<T>& c){
c.numElem=0;
c.primero=nullptr;
c.ultimo=nullptr;
}
template <typename T>
bool esVacia(colaDinamica<T> c){
if(c.numElem==0){
return true;
}
else{
return false;
}
}
template <typename T>
void anyadir(colaDinamica<T>& c, T elemento){
if(esVacia(c)){
typename colaDinamica<T>::Nodo* aux= new typename colaDinamica<T>::Nodo;
aux->dato=elemento;
aux->sig=nullptr;
c.primero= aux;
c.ultimo= aux;
aux=nullptr;
c.numElem++;
}
else{
typename colaDinamica<T>::Nodo* aux= new typename colaDinamica<T>::Nodo;
aux->dato=elemento;
aux->sig=nullptr;
c.ultimo->sig=aux;
c.ultimo=aux;
aux=nullptr;
c.numElem++;
}
}
template <typename T>
void mostrar(colaDinamica<T> c){
if(!esVacia(c)){
typename colaDinamica<T>::Nodo* aux;
aux=c.primero;
while(aux->sig!=nullptr){
cout << aux->dato << endl;
aux= aux->sig;
}
cout << aux->dato << endl;
}
}
template <typename T>
void eliminar(colaDinamica<T>& c){
typename colaDinamica<T>::Nodo* aux;
aux=c.primero;
c.primero= c.primero->sig;
delete(aux);
}