Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct bucket{
- char cislo[8];
- struct bucket *dalsi;
- }BUCKET;
- BUCKET *buckety[21000];
- int vrat_hash(char a[])
- {
- int hash=0;
- int i;
- for(i=0;i<8;i++)
- {
- hash=2*hash+(int)a[i];
- }
- return hash;
- }
- // spracuje cisla OP: vrati pocet najdenych duplikatov.
- int vyhadzovac(char *a[], int n)
- {
- int i,hash,zhody=0;
- int nastala_zhoda=0;
- BUCKET *cislo_OP,*pom;
- for(i=0;i<n;i++)
- {
- cislo_OP=(BUCKET*)malloc(sizeof(BUCKET));
- strcpy(cislo_OP->cislo,a[i]);
- cislo_OP->dalsi=NULL;
- hash=vrat_hash(a[i]);
- if(buckety[hash]==NULL)
- {
- buckety[hash]=cislo_OP;
- }else
- {
- nastala_zhoda=0;
- pom=buckety[hash];
- while(pom!=NULL)
- {
- if((strcmp(pom->cislo,a[i]))==0)
- {
- zhody++;
- nastala_zhoda=1;
- }
- pom=pom->dalsi;
- }
- if(nastala_zhoda==0)
- {
- pom=buckety[hash];
- buckety[hash]=cislo_OP;
- cislo_OP->dalsi=pom;
- }
- }
- }
- return zhody;
- }
- // ukazkovy test
- int main(void)
- {
- char *a[] = {"AA123456", "BA987689", "AA123123", "AA312312", "BB345345", "AA123123","BA987689", "BA987689", "BA987699"};
- printf("%d",vrat_hash("BA987689"));
- getchar();
- printf("Pocet duplikatov: %d\n", vyhadzovac(a, 9));
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement