Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // Uloha_6.2
- //
- // Created by Marcel Choma on 28.10.2014.
- // Copyright (c) 2014 Marcel Choma. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct hash_table{
- char *kod_preukazu;
- struct hash_table *next;
- }HASH_TABLE;
- int opakovania = 0;
- int hash(char *retazec, int pocet)
- {
- int i = 0;
- int hash = 0;
- for (i=0; retazec[i]!='\0'; i++)
- hash = ((i * hash) + (retazec[i]))/2;
- return (hash % pocet);
- }
- void pridaj(char *retazec, int pocet, HASH_TABLE *pole[])
- {
- int index = hash(retazec, pocet);
- HASH_TABLE *aktualny, *predosly = NULL;
- aktualny = pole[index];
- if (pole[index] == NULL){
- pole[index] = (HASH_TABLE*) malloc(sizeof(HASH_TABLE));
- pole[index]->kod_preukazu = retazec;
- return;
- }
- while (aktualny != NULL){
- if((strcmp(aktualny->kod_preukazu, retazec) == 0)){
- opakovania++;
- return;
- }
- predosly = aktualny;
- aktualny = aktualny->next;
- }
- predosly->next = (HASH_TABLE*) malloc(sizeof(HASH_TABLE));
- predosly->next->kod_preukazu = retazec;
- predosly->next->next = NULL;
- }
- // spracuje cisla OP: vrati pocet najdenych duplikatov.
- int vyhadzovac(char *a[], int n)
- {
- int i,j;
- HASH_TABLE *pole[n];
- for (i = 0; i < n; i++)
- pole[i] = 0;
- for (j = 0; j < n; j++)
- pridaj(a[j], n, pole);
- return opakovania;
- }
- // ukazkovy test
- int main(void)
- {
- // , "BA987689","AA123456","AA123456","BA987689", "AA123123", "BA987689", "AA312312", "BA987689","AA123123", "AA123123", "BA987689", "AA312312", "BA987689","AA123123"
- char *a[] = {"abc", "acb", "cba", "cab", "cab", "bca", "bac", "bca", "bac"};
- printf("Pocet duplikatov: %d\n", vyhadzovac(a, 9));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement