Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 1000
- #define MEMCHECK(x) if(x==NULL) { printf("Nema memorija!\n"); exit(-1); }
- typedef char * key_struct;
- typedef char * value_struct;
- typedef struct hashmap_element
- {
- key_struct key; // kluc
- value_struct value; // vrednost
- } hashmap_node; // Definiranje element vo hash mapa
- typedef struct element
- {
- hashmap_node info;
- struct element *link;
- } h_node; // Definiranje na element vo linearna lista (koficka) vo hash mapa
- typedef h_node * h_nodep;
- typedef struct cbht
- {
- int size;
- h_node ** buckets; // niza od koficki (linearni listi)
- } hashmap; // Definiranje na hash mapa
- void make_hash_map(hashmap *tptr, int n) // inicijalizacija na hash mapa
- {
- int i;
- tptr->size=n;
- tptr->buckets=(h_node **) calloc(tptr->size, sizeof(h_node *));
- // alokacija na memorija za nizata od koficki
- for(i=0;i<tptr->size;i++)
- tptr->buckets[i]=NULL;
- }
- int hashCode(key_struct key)
- {
- return key[0]-'a'; // funkcija koja naoga hash kod na char *
- // za drugi tipovi na klucevi treba da se definira druga hashCode funkcija
- }
- int hash(key_struct key, hashmap *tptr)
- {
- // funkcija koja presmetuva hash
- return hashCode(key)%(tptr->size);
- }
- h_node * search(key_struct targetKey, value_struct targetVal, hashmap *tptr)
- // funkcija za prebaruvanje vo hash
- {
- int h;
- h_node *p;
- h=hash(targetKey, tptr);
- for(p=tptr->buckets[h]; p!=NULL; p=p->link)
- {
- if (!strcmp((p->info).key, targetKey))
- {
- if (!strcmp((p->info).value, targetVal))
- return p;
- else
- return NULL;
- }
- }
- return NULL;
- }
- void insert(key_struct key, value_struct val, hashmap *tptr)
- // funkcija za vnesuvanje vo hash
- {
- h_node *tmp;
- h_node *p;
- int h;
- h=hash(key, tptr);
- p=(h_node *) malloc(sizeof(h_node)); // alokacija za noviot jazel
- (p->info).value=val;
- (p->info).key=key;
- for(tmp=tptr->buckets[h]; tmp!=NULL; tmp=tmp->link)
- {
- // ako veke postoi takov zapis
- if (!strcmp((tmp->info).key, key))
- {
- (tmp->info).value=val; // se zamenuva so novata vrednost
- return;
- }
- }
- // se vnesuva noviot jazel na pocetokot na kofickata h
- p->link=tptr->buckets[h];
- tptr->buckets[h]=p;
- }
- void delete(key_struct key, hashmap *tptr)
- // funkcija za brisenje na onie jazli so ist kluc kako dadeniot
- {
- h_node *p, *last;
- int h;
- h=hash(key,tptr);
- for (p=tptr->buckets[h]; p!=NULL; p=p->link)
- {
- if (!strcmp((p->info).key, key))
- break;
- }
- if (p==NULL) return; // ako nema takvi jazli za brisenje
- if(p==tptr->buckets[h]) // ako najdeniot e na pocetok na koficka za brisenje
- tptr->buckets[h]=p->link;
- else
- {
- // naoganje na prethodnikot na jazelot sto treba da se izbrise
- for (last=tptr->buckets[h]; (last!=NULL)&&(last->link!=NULL); last=last->link)
- {
- if (last->link==p)
- break;
- }
- last->link=p->link;
- }
- free(p); // brisenje na jazel
- }
- void print_h_node(h_node *p)
- {
- printf("(%s,%s)",(p->info).key,(p->info).value);
- }
- void print_hashmap(hashmap *tptr)
- {
- int i;
- h_node *p;
- for(i=0; i<tptr->size; i++)
- {
- printf("%d:",i);
- for(p=tptr->buckets[i]; p!=NULL; p=p->link)
- print_h_node(p);
- printf("\n");
- }
- }
- int main()
- {
- hashmap *hashTabela=(hashmap *)malloc(sizeof(hashmap));
- int N,i;
- scanf("%d",&N);
- //--Sami ja postavuvate goleminata na hesh tabelata
- make_hash_map(hashTabela,26);
- char user[100][30];
- char pass[100][30];
- int h, broj_vneseni=0;
- for(i=0; i<N; i++)
- {
- scanf("%s",user[i]);
- scanf("%s",pass[i]);
- insert(user[i],pass[i],hashTabela);
- } //vnesuvanje na userite i lozinkite vo hash
- char chusername[100][100];
- char chlozinka[100][100];
- i=0;
- while(scanf("%s", chusername[i])){
- if(!strcmp("KRAJ",chusername[i]))
- break;
- scanf("%s", chlozinka[i]);
- i++;
- }
- broj_vneseni=i;
- h_node *bara;
- int flag=0;
- for(i=0; i<broj_vneseni-1; i++)
- {
- bara=search(chusername[i],chlozinka[i],hashTabela);
- if(bara==NULL)
- {
- if(flag!=1)
- printf("Nenajaven\n");
- else break;
- }
- else
- {
- printf("Najaven");
- flag=1;
- }
- }
- bara=search(chusername[i],chlozinka[i],hashTabela);
- if(flag==0)
- {
- if(bara==NULL)
- printf("Nenajaven");
- else
- printf("Najaven");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement