Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. typedef struct bucket{
  5.  
  6.     char cislo[8];
  7.     struct bucket *dalsi;
  8.  
  9. }BUCKET;
  10.  
  11. BUCKET *buckety[21000];
  12.  
  13. int vrat_hash(char a[])
  14. {
  15.     int hash=0;
  16.     int i;
  17.     for(i=0;i<8;i++)
  18.     {
  19.         hash=2*hash+(int)a[i];
  20.     }
  21.     return hash;
  22. }
  23.  
  24. // spracuje cisla OP: vrati pocet najdenych duplikatov.
  25. int vyhadzovac(char *a[], int n)
  26. {
  27.     int i,hash,zhody=0;
  28.     int nastala_zhoda=0;
  29.     BUCKET *cislo_OP,*pom;
  30.     for(i=0;i<n;i++)
  31.     {
  32.         cislo_OP=(BUCKET*)malloc(sizeof(BUCKET));
  33.         strcpy(cislo_OP->cislo,a[i]);
  34.         cislo_OP->dalsi=NULL;
  35.         hash=vrat_hash(a[i]);
  36.  
  37.         if(buckety[hash]==NULL)
  38.         {
  39.             buckety[hash]=cislo_OP;
  40.         }else
  41.         {
  42.             nastala_zhoda=0;
  43.             pom=buckety[hash];
  44.             while(pom!=NULL)
  45.             {
  46.                 if((strcmp(pom->cislo,a[i]))==0)
  47.                 {
  48.                     zhody++;
  49.                     nastala_zhoda=1;
  50.                 }
  51.             pom=pom->dalsi;
  52.             }
  53.  
  54.             if(nastala_zhoda==0)
  55.             {
  56.                 pom=buckety[hash];
  57.                 buckety[hash]=cislo_OP;
  58.                 cislo_OP->dalsi=pom;
  59.             }
  60.         }
  61.     }
  62.     return zhody;
  63. }
  64.  
  65. // ukazkovy test
  66. int main(void)
  67. {
  68.   char *a[] = {"AA123456", "BA987689", "AA123123", "AA312312", "BB345345", "AA123123","BA987689", "BA987689", "BA987699"};
  69.   printf("%d",vrat_hash("BA987689"));
  70.   getchar();
  71.   printf("Pocet duplikatov: %d\n", vyhadzovac(a, 9));
  72.   getchar();
  73.   return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement