Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- #include<malloc.h>
- using namespace std;
- struct Telefon
- {
- char* serie;
- float pret;
- int memorie;
- };
- struct nod
- {
- Telefon info;
- nod* next;
- };
- struct Hash
- {
- nod** vector;
- int dim;
- };
- Telefon creareTelefon(char* serie, float pret, int memorie) {
- Telefon t;
- t.serie = (char*)malloc(sizeof(char)*(strlen(serie) + 1));
- strcpy(t.serie, serie);
- t.pret = pret;
- t.memorie = memorie;
- return t;
- }
- void afisareTelefon(Telefon t) {
- printf("Telefonul cu seria %s costa %5.2f si are %d Gb memorie.\n", t.serie, t.pret, t.memorie);
- }
- nod* creareNod(Telefon info, nod* next) {
- nod* nodNou = (nod*)malloc(sizeof(nod));
- nodNou->info = creareTelefon(info.serie, info.pret, info.memorie);
- nodNou->next = next;
- return nodNou;
- }
- nod* inserareSfarsit(nod*cap, Telefon t)
- {
- nod* nou = creareNod(t, NULL);
- if (cap)
- {
- nod*p = cap;
- while (p->next)
- {
- p = p->next;
- }
- p->next = nou;
- return cap;
- }
- else
- {
- return nou;
- }
- }
- int functionHash(char* serie, int dim)
- {
- int s = 0;
- for (int i = 0; i < strlen(serie); i++)
- {
- s += serie[i];
- }
- return s%dim;
- }
- Hash initializareHash(int dim)
- {
- Hash hash;
- hash.dim = dim;
- hash.vector = (nod**)malloc(sizeof(nod*)*dim);
- for (int i = 0; i < dim; i++)
- {
- hash.vector[i] = NULL;
- }
- return hash;
- }
- int inserareInTabela(Telefon t, Hash hs)
- {
- if (hs.vector)
- {
- int hashCode = functionHash(t.serie, hs.dim);
- if (hs.vector[hashCode])
- {
- hs.vector[hashCode] = inserareSfarsit(hs.vector[hashCode], t);
- return hashCode;
- }
- else
- {
- hs.vector[hashCode] = creareNod(t, NULL);
- return hashCode;
- }
- }
- else
- {
- return -1;
- }
- }
- void afisareLista(nod* cap)
- {
- if (cap)
- {
- nod* p = cap;
- while (p)
- {
- afisareTelefon(p->info);
- p = p->next;
- }
- }
- }
- void afisareHash(Hash hs)
- {
- if (hs.vector)
- {
- for (int i = 0; i < hs.dim; i++)
- {
- nod*p = hs.vector[i];
- if (hs.vector[i])
- {
- printf("pozitia: %d\n", i);
- afisareLista(p);
- }
- }
- }
- }
- Telefon cautareDupaSerie(char*serie, Hash ht)
- {
- if (ht.vector)
- {
- int pozitie = functionHash(serie, ht.dim);
- nod* p = ht.vector[pozitie];
- while (p&&strcmp(serie, p->info.serie) != 0)
- {
- p = p->next;
- }
- if (p)
- {
- return p->info;
- }
- }
- Telefon t;
- t.serie = NULL;
- t.pret = -1;
- t.memorie = -1;
- return t;
- }
- void stergereSpatiu(Hash *hash)
- {
- for (int i = 0; i < hash->dim; i++)
- {
- nod* p = hash->vector[i];
- while (p)
- {
- free(p->info.serie);
- nod* aux = p;
- p = p->next;
- free(aux);
- }
- hash->vector[i] = NULL;
- }
- free(hash->vector);
- hash->dim = 0;
- hash->vector = NULL;
- }
- void main()
- {
- Hash hash;
- hash = initializareHash(6);
- int pozitie = inserareInTabela(creareTelefon("C45D3", 1000, 4), hash);
- inserareInTabela(creareTelefon("D34C5", 1200, 6), hash);
- inserareInTabela(creareTelefon("F36D5", 1700, 12), hash);
- afisareHash(hash);
- Telefon t = cautareDupaSerie("F36D5", hash);
- if (t.serie)
- {
- afisareTelefon(t);
- }
- stergereSpatiu(&hash);
- afisareHash(hash);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement