Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct zaznam {
- char ID[9];
- struct zaznam *dalsi;
- } ZAZNAM;
- int hash(char *string)
- {
- int i, len, result=1;
- len = strlen(string);
- for (i=0; i<len; i++)
- {
- if (i != 0)
- {
- result = result*2 + string[i];
- }
- else
- {
- result = string[i];
- }
- }
- return result;
- }
- // spracuje cisla OP: vrati pocet najdenych duplikatov.
- int vyhadzovac(char *a[], int n)
- {
- ZAZNAM **pole = (ZAZNAM**)malloc(5*n*sizeof(ZAZNAM*));
- int pocet=0, i, hashCode;
- ZAZNAM *pom;
- for (i=0; i<5*n; i++)
- {
- pole[i] = NULL;
- }
- for (i=0; i<n; i++)
- {
- hashCode = hash(a[i]);
- if (pole[hashCode % (5*n)] == NULL)
- {
- pole[hashCode % (5*n)] = malloc(sizeof(ZAZNAM));
- strcpy(pole[hashCode % (5*n)] -> ID, a[i]);
- pole[hashCode % (5*n)] -> dalsi = NULL;
- }
- else
- {
- pom = pole[hashCode % (5*n)];
- while (pom-> dalsi != NULL)
- {
- if (strcmp(pom -> ID, a[i]) == 0)
- {
- pocet+=1;
- break;
- }
- else
- {
- pom = pom -> dalsi;
- }
- }
- if (pom -> dalsi == NULL)
- {
- if (strcmp(pom -> ID, a[i]) == 0)
- {
- pocet+=1;
- }
- else
- {
- pom -> dalsi = malloc(sizeof(ZAZNAM));
- strcpy(pom -> dalsi -> ID, a[i]);
- pom -> dalsi -> dalsi = NULL;
- }
- }
- }
- }
- return pocet;
- }
- // ukazkovy test
- int main(void)
- {
- char **a = NULL, buf[100];
- int n = 0, len = 0;
- // nacitanie retazcov
- while(scanf("%s", buf) > 0)
- {
- if (n == len)
- {
- len = len + len + (len==0);
- a = (char**)realloc(a, len*sizeof(char*));
- }
- a[n++] = strdup(buf);
- }
- printf("Pocet duplikatov: %d\n", vyhadzovac(a, n));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement