Advertisement
dtung

dup.c

Apr 30th, 2020
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. /*
  2. Copyright © 2020 David Tung.
  3. dup.c provide examples of how to use hash.c and hash.h.
  4.  
  5. see readme.md for more information.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "hash.h"
  10.  
  11. #define MAXLINE 512
  12. #define HASHSIZE 1000
  13.  
  14. int main() {
  15.     char s[MAXLINE];
  16.     int one = 1;
  17.     int *p;
  18.  
  19.     Hash counts = create(HASHSIZE, (Hashdup) intdup, NULL);
  20.     while (fgets(s, MAXLINE, stdin) != NULL)
  21.         if ((p = lookup(counts, s)) == NULL)
  22.             insert(counts, s, &one);
  23.         else
  24.             (*p)++;
  25.  
  26.     for (Hnode np = firsthnode(counts); np != NULL; np = nexthnode(counts)) {
  27.         p = np->value;
  28.         if (*p > 1)
  29.             printf("%3d %s", *p, np->key);
  30.     }
  31.  
  32.     destroy(counts);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement