Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- #define VELIME 14
- #define M 7
- #define ever (;;)
- struct zapis {
- char ime[VELIME + 1];
- struct zapis *sljed;
- };
- typedef struct zapis zapis;
- int Adresa(char ime[]){
- int suma = 0, i;
- for (i=0; i < strlen(ime); i++){
- suma += (int) ime[i];
- }
- return suma % M;
- }
- void Upis (zapis *hash[], struct zapis *element){
- int i = Adresa (element -> ime);
- element -> sljed = hash [i];
- hash [i] = element;
- }
- void Isprazni(zapis *hash[], int n){
- int i;
- for (i=0; i<n; i++){
- hash[i] = NULL;
- }
- }
- int Trazi(zapis *hash[], zapis *element){
- int i = Adresa (element->ime);
- zapis *pom = hash[i];
- while (pom != NULL){
- if (strcmp (pom->ime, element -> ime) == 0)
- return 1;
- pom = pom -> sljed;
- }
- return 0;
- }
- void Ispis(zapis* hash[], int n){
- int i;
- zapis *pom;
- printf ("\nIspis sadrzaja hash-a\n");
- for (i=0; i<n; i++){
- printf ("%d > ", i);
- pom = hash [i];
- while (pom != NULL){
- printf (" %s", pom -> ime);
- pom = pom -> sljed;
- }
- printf ("\n");
- }
- }
- void Obrisi_Hash (zapis **hash, int n){
- int i;
- zapis *pom;
- for (i=0; i<n; i++){
- pom = hash[i];
- while (pom != NULL){
- hash[i] = pom -> sljed;
- free (pom);
- pom = hash[i];
- }
- }
- free (hash);
- }
- int main (void){
- zapis **hash;
- zapis *element = NULL;
- int find;
- hash = (zapis **) malloc (sizeof (struct zapis *) * M);
- Isprazni (hash, M);
- printf ("Upisite imena koja zelite staviti u hash tablicu.\n");
- printf ("Ta kraj upisite EOL (end of list):\n");
- for ever {
- element = (zapis *) malloc (sizeof (zapis));
- gets (element -> ime);
- if (strcmp(element -> ime, "EOL") == 0){
- break;
- }
- Upis (hash, element);
- }
- printf ("Upisite ime koje zelite pronaci u hashu (\"End\" za prestanak trazenja):\n");
- for ever{
- gets (element -> ime);
- if (strcmp (element -> ime, "End") == 0)
- break;
- find = Trazi (hash, element);
- if (find == 1)
- printf (" ->Ime je pronadeno u hashu.\n");
- else
- printf (" ->Ime nije pronadeno u hashu.\n");
- }
- Ispis (hash, M);
- Obrisi_Hash (hash, M);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement