58 lines
1.1 KiB
C++
58 lines
1.1 KiB
C++
#include "tablahash.hpp"
|
|
TablaHash::TablaHash(int M) {
|
|
nElem = 0;
|
|
this -> M = M;
|
|
this -> lista = new list<Cuac>[M];
|
|
}
|
|
|
|
TablaHash::TablaHash() {
|
|
}
|
|
|
|
TablaHash::~TablaHash() {
|
|
}
|
|
|
|
void 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++;
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// probamos suma posicional
|
|
// 19s
|
|
// probamos suma posicional por trozos
|
|
// ruina
|
|
// es disp. abierta, no necesita redispersión
|
|
// 19s a superar
|
|
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 % M;
|
|
}
|
|
|
|
|