EldiraSesto

Hash a i b

Nov 30th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct hashentry_s
  6. {
  7. char songtitle [256] , interpreter [256];
  8. } hashentry_t ;
  9.  
  10. typedef struct hasharray_s
  11. {
  12. hashentry_t * entries ;
  13. long num_entries ;
  14. } hasharray_t ;
  15.  
  16. typedef struct hashcontainer_s
  17. {
  18. hasharray_t * hasharrays ;
  19. long hashsize ;
  20. } hashcontainer_t ;
  21.  
  22. long hash_key (char songtitle [], char interpreter [], long
  23. hash_max )
  24. {
  25. unsigned long index =0, i;
  26. for (i = 0; i < strlen ( songtitle ); ++i)
  27. index = 64 * index + (long)( songtitle [i]);
  28. for (i = 0; i < strlen ( interpreter ); ++i)
  29. index = 64 * index + (long)( interpreter [i]);
  30. return index % hash_max ;
  31. }
  32.  
  33. //funkcija da napravi hash
  34. hashcontainer_t *create_hash(long hashsize){
  35. hashcontainer_t *newcontainer;
  36. newcontainer = malloc(sizeof(hashcontainer_t));
  37. if(newcontainer){
  38.     newcontainer->hasharrays = calloc(newcontainer->hashsize, sizeof(hasharray_t));
  39.     if(newcontainer->hasharrays){
  40.         newcontainer->hashsize = hashsize;
  41.         return newcontainer;
  42.  
  43.     }
  44.     else{
  45.         printf("Fehler....");
  46.         return 0;
  47.     }
  48.  
  49. }
  50. else{
  51. printf("Fehler beim Allozieren\n");
  52. return 0;
  53. }
  54. }
  55.  
  56. void delete_hash(hashcontainer_t *hashcontainer){
  57. if(hashcontainer == 0){
  58.     printf("Its already empty....");
  59.     return;
  60. }
  61. if(hashcontainer->hasharrays == 0){
  62.     free(hashcontainer);
  63.     hashcontainer = 0;
  64.     return;
  65. }
  66. long i;
  67. for(i=0; i<hashcontainer->hashsize; i++){
  68.     if(hashcontainer->hasharrays[i].entries){
  69.         free(hashcontainer->hasharrays->entries);
  70.     }
  71. }
  72.  
  73. free(hashcontainer->hasharrays);
  74. free(hashcontainer);
  75. }
  76.  
  77.  
  78. void insert_entry ( hashcontainer_t * hashcontainer , char songtitle [], char interpreter []){
  79.  
  80. hashentry_t *tmp = 0;
  81. long index = hash_key(songtitle, interpreter, hashcontainer->hashsize);
  82. long velicina = hashcontainer->hasharrays[index].num_entries;
  83.  
  84. tmp = realloc(hashcontainer->hasharrays[index].entries, (velicina+1)*sizeof(hashentry_t));
  85.  
  86. //dodaj novi element na kraj jednog arraya
  87. strcpy(tmp[velicina].interpreter, interpreter);
  88. strcpy(tmp[velicina].songtitle, songtitle);
  89.  
  90. hashcontainer->hasharrays[index].entries = tmp;
  91. hashcontainer->hasharrays[index].num_entries++;
  92.  
  93. }
  94.  
  95. hashentry_t * search_entry (hashcontainer_t * hashcontainer, char songtitle [], char interpreter []){
  96.  
  97. long key = hash_key(songtitle, interpreter, hashcontainer->hashsize);
  98. hasharray_t tmp = hashcontainer ->hasharrays[key];
  99. long i;
  100.  
  101. for(i=0; i<tmp.num_entries; i++){
  102.     if((strcmp(tmp.entries[i].interpreter, interpreter)==0) && (strcmp(tmp.entries[i].songtitle, songtitle)==0))
  103.         return &(tmp.entries[i]);
  104. }
  105.  
  106. printf("Element nicht gefunden\n");
  107. return 0;
  108.  
  109. }
  110.  
  111. void delete_entry ( hashcontainer_t * hashcontainer , char songtitle [], char interpreter []){
  112. long key = hash_key(songtitle, interpreter, hashcontainer->hashsize);
  113. hasharray_t *tmp = &(hashcontainer->hasharrays[key]);
  114. hashentry_t *tmp_entry = malloc(tmp->num_entries * sizeof(hashentry_t));
  115.  
  116. if(tmp->num_entries==0){
  117.     printf("Das zu loeschende Elemenet nicht gefunden.....");
  118.     return;
  119. }
  120. else if(tmp->num_entries == 1){
  121.     free(tmp->entries);
  122.     tmp->entries = 0;
  123.     tmp->num_entries = 0;
  124.     printf("Das einzige Element in diesem Array wurde geloescht\n");
  125. }
  126. else{
  127.     long i;
  128.     for(i=0; i<tmp->num_entries; i++){
  129.         if(strcmp(tmp->entries[i].interpreter, interpreter) ==0 && strcmp(tmp->entries[i].songtitle, songtitle)==0){
  130.             memcpy(tmp_entry, tmp->entries, sizeof(hashentry_t)*i);
  131.  
  132.            memcpy(&(tmp_entry[i]), &(tmp->entries[i+1]), sizeof(hashentry_t)*tmp->num_entries -i -1);
  133.             //memcpy(&(tmp_entry[i]),&(tmp->entries[i+1]),sizeof(hashentry_t)*tmp->num_entries -i -1);
  134.             tmp->entries = tmp_entry;
  135.             tmp->num_entries--;
  136.             tmp->entries = realloc(tmp->entries, sizeof(hashentry_t)*tmp->num_entries);
  137.             printf("Gotovo....");
  138.             return;
  139.         }
  140.  
  141.  
  142.     }
  143.  
  144.  
  145. }
  146.  
  147.  
  148. }
  149.  
  150. long print_hash ( hashcontainer_t * hashcontainer ){
  151. long i,j;
  152. if(hashcontainer!=0){
  153.     for(i=0; i<hashcontainer->hashsize; i++){
  154.         printf("%ld: ", i);
  155.         if(hashcontainer->hasharrays[i].entries){
  156.             for(j=0; j<hashcontainer->hasharrays[i].num_entries; j++){
  157.                 printf(" %ld. %s - %s", j, hashcontainer->hasharrays[i].entries[j].interpreter, hashcontainer->hasharrays[i].entries[j].songtitle );
  158.                 printf("\n");
  159.  
  160.             }
  161.  
  162.  
  163.         }
  164.  
  165.     }
  166.  
  167.     return i;
  168.  
  169. }
  170.  
  171. return 0;
  172. }
  173.  
  174. long count_entries_with_interpreter ( hashcontainer_t* hashcontainer , char interpreter []){
  175. long i,j;
  176. long summe = 0;
  177. if(hashcontainer==0){
  178.     printf("Prazan kontejner....");
  179.     return 0;
  180. }
  181. for(i=0; i<hashcontainer->hashsize; i++){
  182.     if(hashcontainer->hasharrays[i].num_entries!=0){
  183.         for(j=0; j<hashcontainer->hasharrays[i].num_entries; j++){
  184.             if(strcmp(hashcontainer->hasharrays[i].entries[j].interpreter , interpreter)==0)
  185.                 summe=summe++;
  186.  
  187.             }
  188.     }
  189. }
  190.  
  191. return summe;
  192. }
  193.  
  194. int main()
  195. {
  196.    int w=0;
  197.    long z;
  198.    hashcontainer_t *myhash=0;
  199.    char songtitle[256], interpreter[256];
  200.    hashentry_t *find;
  201.  
  202.    myhash = create_hash(3);
  203.    insert_entry(myhash, "Lijepaje", "Eldira");
  204.    insert_entry(myhash, "Glupost", "Glupan");
  205.    insert_entry(myhash, "NezBgm", "Niko");
  206.    insert_entry(myhash, "Smor", "Dosada");
  207.  
  208.    do{
  209.      printf("1. CREATE HASH\n");
  210.         printf("2. DELETE HASH\n");
  211.         printf("3. INSERT ENTRY\n");
  212.         printf("4. SEARCH ENTRY\n");
  213.         printf("5. DELETE ENTRY\n");
  214.         printf("6. PRINT HASH\n");
  215.         printf("7. COUNT WITH songtitle\n");
  216.         printf("0. EXIT\n");
  217.  
  218.         scanf("%d", &w);
  219.  
  220.         switch(w){
  221.     case 1:
  222.         printf("Size? ");
  223.         scanf("%ld", &z);
  224.         delete_hash(myhash);
  225.         myhash=0;
  226.         myhash=create_hash(z);
  227.         break;
  228.     case 2:
  229.         delete_hash(myhash);
  230.             myhash=0;
  231.             break;
  232.         case 3:
  233.             while(getchar()!='\n');
  234.             printf("Songtitle:");
  235.             fgets(songtitle,255,stdin);
  236.             *strchr(songtitle,'\n')=0;
  237.             printf("Interpreter:");
  238.             fgets(interpreter,255,stdin);
  239.             *(strchr(interpreter,'\n'))=0;
  240.  
  241.             insert_entry(myhash,songtitle,interpreter);
  242.             break;
  243.         case 4:
  244.             while(getchar()!='\n');
  245.             printf("Songtitle:");
  246.             fgets(songtitle,255,stdin);
  247.             *(strchr(songtitle,'\n'))=0;
  248.             printf("Interpreter:");
  249.             fgets(interpreter,255,stdin);
  250.             *(strchr(interpreter,'\n'))=0;
  251.  
  252.             find = search_entry(myhash,songtitle,interpreter);
  253.             printf("Gefunden auf Adresse : %p\n",find);
  254.             break;
  255.         case 5:
  256.             while(getchar()!='\n');
  257.             printf("Songtitle:");
  258.             fgets(songtitle,255,stdin);
  259.             *(strchr(songtitle,'\n'))=0;
  260.             printf("Interpreter:");
  261.             fgets(interpreter,255,stdin);
  262.             *(strchr(interpreter,'\n'))=0;
  263.  
  264.             delete_entry(myhash,songtitle,interpreter);
  265.             break;
  266.         case 6:
  267.             if(myhash)
  268.                 print_hash(myhash);
  269.             break;
  270.         case 7:
  271.             while(getchar()!='\n');
  272.             printf("Interpreter:");
  273.             fgets(interpreter,255,stdin);
  274.             *(strchr(interpreter,'\n'))=0;
  275.  
  276.             printf("Count: %ld\n",count_entries_with_interpreter(myhash,interpreter));
  277.             break;
  278.         case 0:
  279.             break;
  280.         default:
  281.             printf("Falsche Eingabe\n");
  282.         }
  283.         }while(w!=0);
  284.  
  285.     return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment