Vous êtes sur la page 1sur 8

TABELE DE DISPERSIE - CHAINING

#include<iostream>
using namespace std;
struct Student{
char* nume;
int nrNote;
int* note;
};
struct nod{
Student stud;
nod* next;
};
struct HashTable{
int dim;
(nod*) *vector;
};
HashTable initializareHash(int dim)
{
HashTable h;
h.dim=dim;
h.vector=new nod*[dim];
return h;
}
//functia hash, care va procesa numele studentului si va determina pozitia pe care ar
trebui sa se afle acesta in tabel
int fctHash(char* input, int n)
{ //n=dimensiunea hash-table-lui
int suma=0;
for(int i=0; i<strlen(input); i++)
suma += input[i];
return suma%n;
}
nod* creareNod(Student s)
{
nod* nou= new nod();
nou->next=NULL;
nou->stud=s;
return nou;
}
//inserare la inceputul listei listei
nod* inserareNod(nod* capat,Student s)
{
nod* nou= creareNod(s);
if(capat==NULL) {capat=nou; return capat;}
else
{
nou->next=capat;
capat=nou;
return capat;
}
}
void inserareInHash(HashTable ht, Student s)
{

int poz=fctHash(s.nume, ht.dim);


nod* capat= ht.vector[poz];
capat=inserareNod(capat,s);
ht.vector[poz]=capat;
}
//cautare dupa nume
nod* cautareInHash(HashTable ht, char* nume)
{
int poz= fctHash(nume, ht.dim);
nod* capat= ht.vector[poz];
if(capat==NULL) {cout<<"nu s-a gasit studentul "; return NULL;}
else{
nod* p= capat;
while(p!=NULL)
if(strcmp(p->stud.nume, nume)==0) return p;
else p=p->next;
}
return NULL;
}
void afisareConsola(nod* p)
{
printf("Studentul %s are %d note: ", p->stud.nume, p->stud.nrNote);
for(int i=0; i<p->stud.nrNote;i++)
printf("%d ,", p->stud.note[i]);
printf("\n");
}
void main()
{
HashTable ht=initializareHash(101);
FILE* f=fopen("studenti.txt", "r");
Student s; char buffer[100], pren[100];
fscanf(f,"%s", &buffer);
while(!feof(f))
{
strcat(buffer," ");
fscanf(f,"%s", &pren);
strcat(buffer,pren);
s.nume=new char[strlen(buffer)+1];
strcpy(s.nume, buffer);
fscanf(f,"%d", &s.nrNote);
s.note=new int[s.nrNote];
for(int i=0; i<s.nrNote; i++)
{
fscanf(f,"%d", &s.note[i]);
}
inserareInHash(ht,s);
fscanf(f, "%s", buffer);
}
fclose(f);

nod* s1 = cautareInHash(ht, "Pavel Alexandra");


afisareConsola(s1);
}

TABELE DE DISPERSIE - LINEAR PROBING


#include<iostream>
using namespace std;
struct Student{
char* nume;
char* grupa;
int id;
};

int TranslatareKey(char* nume) {


int s;
if(nume)
return nume[0];
return 0;
}
int HashFunction(int key,int n) {
//n dimensiunea tabelei de dispersie
return key % n;
}
struct HashTable{
Student* tabel;
int n;
};
int inserarelaPoz(HashTable h, Student s, int poz){
h.tabel[poz].id = s.id;
h.tabel[poz].grupa = s.grupa;
h.tabel[poz].nume = s.nume;
return 2;
}

int inserare(HashTable h,Student s) {


int pozi;
int poz; int cod; cod = 0;
poz = HashFunction(TranslatareKey(s.nume),h.n);
if(h.tabel[poz].id == -1)
inserarelaPoz(h,s,poz); // inserare student pe pozitia data de functia
hash
else{ pozi=poz+1;
while((h.tabel[pozi].id != -1) && (pozi<h.n))
{
pozi++; //cautare prima pozitie disponibila de inserat in interval
poz+1 pana la n-1
}
if(pozi<h.n)//pozitia disponibila inainte de sfarsitul tabelei
inserarelaPoz(h,s,pozi);
else // cautare pozitie disponibila in intervalul 0, pozi
{
pozi = 0;
int j = 0;

while(pozi<poz && !j)


{ if(h.tabel[pozi].id==-1) //Verific daca pozitia este disponibila
j=inserarelaPoz(h,s,pozi);
else
pozi++; //cautare pozitie disponibila in stanga pozitiei
calculata de functia hash
}
if(poz==pozi)
//cout<<"Nu s-a patut face inserarea - Tabela e plina!
"<<endl;
cod = -1;
}
} return cod;
}
void initializare(HashTable &h,int n) {
h.n = n;
h.tabel = new Student[n];
for(int i=0; i<h.n;i++)
h.tabel[i].id = -1;
}
void afisare(HashTable h){
for(int i=0;i<h.n;i++)
if(h.tabel[i].id!=-1)
cout<<"Pe pozitia "<<i<<" se gaseste studentul "<<h.tabel[i].nume<<" cu
pozitia calc: "<<HashFunction(TranslatareKey(h.tabel[i].nume),h.n)<<endl;//" cu id-ul
"<<h.tabel[i].id <<" si grupa "<<h.tabel[i].grupa<<endl;
}

void main() {
HashTable h ;
/*h.n = 256;
h.tabel= new Student[h.n];*/
initializare(h,256);

FILE *f;
f= fopen("stud.txt","r");
Student st;
//populare tabela de dispersie cu linear probing
fscanf(f,"%d",&st.id);
while(!feof(f))
{
char buf[100], pren[100];
fscanf(f,"%s",buf);
strcat(buf, " ");
fscanf(f,"%s",pren);
strcat(buf, pren);
st.nume=new char[strlen(buf)+1];
strcpy(st.nume,buf);
fscanf(f, "%s", buf);
st.grupa=new char[strlen(buf)+1];
strcpy(st.grupa,buf);
int i=inserare(h,st);

if(i==-1)
cout<<"Problema inserare - Tabela este plina. Inserare se opreste
la studentul cu id-ul "<<st.id<<")."<<endl;
else
if(!i)
cout<<"Inserare cu succes"<<endl;
else
cout<<"A fost o problema la inserare!Inserare se
opreste la studentul cu id-ul "<<st.id<<")."<<endl;

fscanf(f,"%d",&st.id);
}
cout<<endl;
afisare(h);
cout<<endl;
//dezalocare tabela de dispersie
for(int i=0;i<h.n;i++)
{ if(h.tabel[i].id!=-1) {
delete[] h.tabel[i].nume;
delete[] h.tabel[i].grupa;} }
delete[] h.tabel;
//
f = fopen();
fclose(f);
}

HEAP
#include<iostream>
using namespace std;
struct transferBancar{
char* titular;
char IBAN[50];
char* titularDest;
char IBANdestinatie[50];
int valoare;
int prioritate;
};
struct heap{
transferBancar* coadaMesaje;
int nrMesaje;
int dimCoada;
};
//functie de initializare heap
heap initHeap(int dimInit)
{
heap h;
h.coadaMesaje=new transferBancar[dimInit];
h.nrMesaje=0;
h.dimCoada=dimInit;

return h;
}
//functie de redimensionare heap
heap redimHeap(heap h, int DimNoua)
{
heap nou;
nou.dimCoada=DimNoua;
nou.coadaMesaje=new transferBancar[DimNoua];
nou.nrMesaje=h.nrMesaje;
for(int i=0; i<h.dimCoada;i++)
nou.coadaMesaje[i]=h.coadaMesaje[i];
delete[]h.coadaMesaje;
return nou;
}
//functie de determinare parinte
int parinte(int j)
{
return (j-1)/2;
}
//functii de determinare a pozitiilor din vector a copiiilor
int FiuSt(int j)
{
return (j*2)+1;
}
int FiurDr(int j)
{
return (j+1)*2;
}
heap adaugareHeap(heap h, transferBancar tr)
{
if(h.dimCoada==h.nrMesaje) h=redimHeap(h,h.dimCoada*2);
else
{
//adaugam la capat, iar apoi incepem sa comparam cu parintii succesiv, pana
cand parinti/copii sunt pusi in ordinea descresc a prioritatii
//sau pana cand ajung in varf(primul element)
h.coadaMesaje[h.nrMesaje]=tr;
//pozitia curenta
int j=h.nrMesaje;
h.nrMesaje++;
while(true)
{
if(j==0) break;
int i=parinte(j);
if(tr.prioritate>h.coadaMesaje[i].prioritate)
{
//interschimbare
transferBancar aux = h.coadaMesaje[i];
h.coadaMesaje[i] = h.coadaMesaje[j];
h.coadaMesaje[j]=aux;
j=i;
}
else break; //parintele are prioritatea mai mare, ceea ce este ok.
}
}
return h;

int maximFII(heap h, int i)


{
int jst=FiuSt(i);
int jdr=FiurDr(i);
//verificam cati fii are
if(jst<h.nrMesaje && jdr<h.nrMesaje)
{
if(h.coadaMesaje[jst].prioritate>h.coadaMesaje[jdr].prioritate)
return jst;
else return jdr;
}
else
if(jst<h.nrMesaje) return jst;
else
if(jdr<h.nrMesaje) return jdr;
return -1;
}
//stergere din heap (INTOTDEAUNA INCEAND CU PRIMUL ELEMENT din vectorul de mesaje)
transferBancar stergereElem(heap h)
{
//inlocuiesc primul element cu ultimul
transferBancar returnare=h.coadaMesaje[0];
h.coadaMesaje[0]=h.coadaMesaje[h.nrMesaje-1];
//pozitia parintelui
int i=0;
while(true){
int max=maximFII(h,i);
if(max==-1) break; //transferul meu e o frunza
else
if(h.coadaMesaje[i].prioritate<h.coadaMesaje[max].prioritate)
{
transferBancar aux = h.coadaMesaje[i];
h.coadaMesaje[i] = h.coadaMesaje[max];
h.coadaMesaje[max]=aux;
i=max;
}
else break;
}
return returnare;
}

void afisareHeap(heap h)
{
transferBancar tr;
for(int i=0; i<h.nrMesaje;i++)
{tr = h.coadaMesaje[i];
printf("%s, cu IBAN %s, transfera %d, catre %s, cu IBAN %d'\n",tr.titular,
tr.IBAN, tr.valoare, tr.titularDest, tr.IBANdestinatie);}
}
void main()
{
heap h= initHeap(40);

FILE* f=fopen("transferuri.txt", "r");


if(!f) cout<<"eroare deschidere fisier";
else
{
transferBancar t;
char buffer[100], pren[100];
fscanf(f,"%s", &buffer);
while(!feof(f))
{
strcat(buffer, " ");
fscanf(f,"%s", &pren);
strcat(buffer,pren);
t.titular=new char[strlen(buffer)+1];
strcpy(t.titular, buffer);
fscanf(f,"%s", &t.IBAN);
fflush(stdin);
fscanf(f,"%s", &buffer);
strcat(buffer, " ");
fflush(stdin);
fscanf(f,"%s", &pren);
strcat(buffer,pren);
t.titularDest=new char[strlen(buffer)+1];
strcpy(t.titularDest,buffer);
fscanf(f, "%d", &t.valoare);
fscanf(f, "%d", &t.prioritate);
adaugareHeap(h,t);
fscanf(f,"%s", &buffer);
}
}
fclose(f);
afisareHeap(h);
transferBancar h1= stergereElem(h);
h1= stergereElem(h);
afisareHeap(h);
}

Vous aimerez peut-être aussi