46 lines
943 B
C++
46 lines
943 B
C++
#include "tablahash.hpp"
|
|
TablaHash::TablaHash() {
|
|
nElem = 0;
|
|
}
|
|
|
|
Cuac* TablaHash::insertar(Cuac nuevo) {
|
|
int pos = h(nuevo.usuario);
|
|
list<Cuac>::iterator it = lista[pos].begin();
|
|
while (it != lista[pos].end() && nuevo.comparar(*it)){
|
|
it++;
|
|
}
|
|
if (it==lista[pos].end() || !nuevo.comparar(*it)) {
|
|
lista[pos].insert(it, nuevo);
|
|
it--;
|
|
nElem++;
|
|
}
|
|
return &*it;
|
|
}
|
|
|
|
void TablaHash::consultar(string clave) {
|
|
int pos = h(clave);
|
|
int i = 0;
|
|
for (list<Cuac>::iterator it = lista[pos].begin(); it != lista[pos].end(); it++) {
|
|
Cuac c = *it;
|
|
if (c.usuario == clave) {
|
|
i++;
|
|
cout << i << ". " << c.usuario << " ";
|
|
c.fecha.escribir();
|
|
cout << '\n' << " " << c.mensaje << endl;
|
|
}
|
|
}
|
|
cout << "Total: " << i << " cuac" << endl;
|
|
|
|
}
|
|
|
|
// suma posicional
|
|
unsigned int TablaHash::h(string clave) {
|
|
unsigned int res = 0;
|
|
for (int i = 0; i < (int) clave.length(); i++) {
|
|
res = 67 * res + clave[i];
|
|
}
|
|
return res % TH_TAM;
|
|
}
|
|
|
|
|