Documente Academic
Documente Profesional
Documente Cultură
SDD4
SDD4
Cheia de cautare
functia Hash
Valoare Valoare
alfanumerica numerica
Valoare[elem1,ele
m n]
HashStudent size=
2
elem[0] elem[1]
inf
NodDat inf
next
e next
nr_matricol
**elem
info
nume
inserare
next
-trebuie sa punem nodul inNULL
vectorul de liste
-verifica daca elem[index] e liber, daca este pune nodul acolo
**elem
ex: index =1
elem[0] elem[1]
s nod_no
u
next
NULL
t
s
next
**elem
elem[0] elem[1]
next
t
info
next
info
};
//functie simpla de dispersie care utilizeaza operatorul modulo pentru a genera pozitia in
tabela
//cheia=nr_matricol, size=101 => pt nr_matricol =1000 => index=1000%101=91
int HashFunction(int cheie, HashStudent hs)
{
return cheie%hs.size;
};
if (s.nr_matricol<0)
return index;
//adresa nu e nula, avem elem
if (hs.elem != NULL)
{
//genereaza pozitia in tabela
//apeleaza HashFunction care va face nr_matricol%101 pentru a genera o
pozitie
index = HashFunction(s.nr_matricol, hs);
//prima inregistrare
NodDate *nod_nou = new NodDate;
nod_nou->next = NULL;
nod_nou->inf = s;
if (hs.elem[index] == NULL)
{
hs.elem[index] = nod_nou;
}
else //coliziune
{
NodDate *t = hs.elem[index];
while (t->next != NULL)
t = t->next;
t->next = nod_nou;
}
}
return index;
}
if (nr_matricol<0)
return s_negasit;
if (hs.elem != NULL)
{
int index = HashFunction(nr_matricol, hs);
if (hs.elem[index] == NULL)
{
return s_negasit;
}
else
{
if (hs.elem[index]->inf.nr_matricol == nr_matricol)
return hs.elem[index]->inf;
else//coliziune
{
NodDate *t = hs.elem[index];
while (t != NULL && t->inf.nr_matricol != nr_matricol)
t = t->next;
if (t == NULL)
return s_negasit;
else
return t->inf;
}
}
}
//neinitializat sau nu exista inregistrarea
return s_negasit;
}
//dupa ce am cautat dupa nr matricol, daca acesta e gasit in tabela de dispersie se va afisa pe
ecran nr_matricol si numele studentului
void Afisare(Student s)
{
cout << "Nr_matricol: " << s.nr_matricol << endl;
cout << "Nume: " << s.nume << endl;
}
void main()
{
Student grupa[] = {
{ 1000, "Ion Vlad" },
{ 204, "Mihai Vlad" },
{ 406, "Dan Vlad" },
{ 305, "Lili Vlad" },
{ 1022, "Silviu Vlad" },
{ 1021, "Alina Vlad" },
{ 1030, "Sorin Vlad" },
{ 1032, "Titi Vlad" },
{ 1200, "Gigel Vlad" },
{ 2021, "Anca Vlad" },
{ 1230, "Maria Vlad" },
{ 1008, "Elena Vlad" }
};
HashStudent hs;
hs = AlocaMemorie();
// i<12
for (int i = 0; i<sizeof(grupa) / sizeof(Student); i++)
{
Insereaza(grupa[i], hs);
}
Afiseaza(hs);
int m;
//in m retin nr_matricol
cout << "Nr_matricol: ";
cin >> m;
while (m != -1)
{
Student rez = Cauta(m, hs);
if (rez.nr_matricol != -1)
Afisare(rez);
cout << "nr_matricol: ";
cin >> m;
}
Afiseaza(hs);
_getch();
}
#include <conio.h>
#include <iostream>
using namespace std;
void Afisare(Student s)
{
cout << "Nr_matricol: " << s.nr_matricol << endl;
cout << "Nume: " << s.nume << endl;
}
if (s.nr_matricol<0)
return index;
if (hs.elem != NULL)
{
index = HashFunction(s.nr_matricol, hs);
//prima inregistrare
NodDate *nod_nou = new NodDate;
nod_nou->next = NULL;
nod_nou->inf = s;
if (hs.elem[index] == NULL)
{
hs.elem[index] = nod_nou;
}
else //coliziune
{
NodDate *t = hs.elem[index];
while (t->next != NULL) t = t->next;
t->next = nod_nou;
}
}
return index;
}
if (hs.elem != NULL)
{
int index = HashFunction(nr_matricol, hs);
if (hs.elem[index] == NULL)
{
return s_negasit;
}
else
{
if (hs.elem[index]->inf.nr_matricol == nr_matricol)
return hs.elem[index]->inf;
else//coliziune
{
NodDate *t = hs.elem[index];
while (t != NULL && t->inf.nr_matricol != nr_matricol)
t = t->next;
if (t == NULL)
return s_negasit;
else
return t->inf;
}
}
}
//neinitializat sau nu exista inregistrarea
return s_negasit;
}
if (hs.elem != NULL)
{
int index = HashFunction(nr_matricol, hs);
if (hs.elem[index] == NULL)
{
return -1;
}
else
{
//este primul
if (hs.elem[index]->inf.nr_matricol == nr_matricol)
{
if (hs.elem[index]->next == NULL)
{
delete hs.elem[index];
hs.elem[index] = NULL;
}
else//mai sunt si alte elemente in lista
{
NodDate *t = hs.elem[index];
hs.elem[index] = t->next;
delete t;
}
}
else//coliziune, nu este primul, il cautam
{
NodDate *t = hs.elem[index];
while (t->next != NULL && t->next->inf.nr_matricol !=
nr_matricol)
t = t->next;
if (t->next == NULL)
return -1;
else//t->next->inf.nr_matricol == nr_matricol
{
NodDate *p = t->next;
if (p->next == NULL)
{
t->next = NULL;
delete p;
}
else
{
t->next = p->next;
delete p;
}
}
}
return index;
}
return -1;
}
}
void main()
{
Student grupa[] = {
{ 1000, "Ion Vlad" },
{ 204, "Mihai Vlad" },
{ 406, "Dan Vlad" },
{ 305, "Lili Vlad" },
{ 1022, "Silviu Vlad" },
{ 1021, "Alina Vlad" },
{ 1030, "Sorin Vlad" },
{ 1032, "Titi Vlad" },
{ 1200, "Gigel Vlad" },
{ 2021, "Anca Vlad" },
{ 1230, "Maria Vlad" },
{ 1008, "Elena Vlad" }
};
HashStudent hs;
hs = AlocaMemorie();
while (m != -1)
{
_getch();
}
AlocareMemorie-se aloca memorie mai intai pentru vectorul de liste, apoi pentru fiecare
element din vector, toate elementele din vector sunt nule.
**ele
m
NULL NULL
hs.elem[0] hs.elem[1]
hs.size=2
hs.elem=new NodData*[hs.size]
->daca avem o tabela de dispersie in care avem elemente (hs.elem!=NULL ->adresa la care
se afla tabela de dispersie):
info
nod_nou
next
NULL
nod_nou->info=s;
nod_nou->next=NULL;
nod_nou
next
NULL
hs.elem
next
NULL
nod_nou
s
next
NULL
hs.ele
hs.elem[index=1] hs.elem[index=2]
next
t
nod_nou
nod_no
t->next!=NULL =>
t->next=nod_nou next
s
next
Afisare:
Daca avem:
creaza un nod t in care pune hs.elem[i](adresa la care se afla nodul care contine inregistrare),
t!=NULL => afiseaza si nodul t va fi urmatorul din lista, daca t->next=NULL, adica nu mai
avem alt nod in lista respectiva, t=NULL si trece la urmatorul element.
Cauta:
->daca e ceva in index, compara nr_matricol din informatia nodului cu nr_matricol introdus de
la tastatura( retinut in m)
daca nu, verifica urmatorul nod din lista (il pune pe hs.elem[index] intr-un nod t si il verifica pe
t->next)
verifica daca mai are vreun nod care se leaga de el, daca nu are, il sterge si face elem din
indexul respectiv 0
daca nu e primul nod din indexul respectiv, avem coliziune si trebuie sa-l cautam in lista, pune
intr-un nod t, primul element din lista,daca nu se potriveste cu nr_matricol si mai are inca un
nod dupa el, il face pe el t
hs.elem[index=1] hs.elem[index=2]
hs.elem[index=3]
200300
info info
100 info
400 info
next
next
next
next
NULL NULL
verifica daca
hs.elem[index]-
>info.nr_matricol
==nr_matricol
hs.elem[index=1] hs.elem[index=2]
hs.elem[index=3]
t
t->next=p-
200 info >next
next
p
300 info
next
400 info
hs.elem[index=1] hs.elem[index=2]
hs.elem[index=3]
200 info t
next
p
400 info
next NULL