Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8KB

  1. #include "uthash.h"
  2. #include <stdio.h>
  3. #include <stdlib.h> /* malloc */
  4. typedef struct person_t {
  5. char first_name[10];
  6. int id;
  7. UT_hash_handle hh;
  8. } person_t;
  9. int main()
  10. {
  11. person_t *people=NULL, *person, *new_person, *tmp;
  12. const char **name;
  13. const char * names[] = { "bob", "jack", "gary", "ty", "bo", "phil", "art",
  14. "gil", "buck", "ted", NULL
  15. };
  16. int id=0;
  17. for(name=names; *name!=NULL; name++) {
  18. person = (person_t*)malloc(sizeof(person_t));
  19. if (person == NULL) {
  20. exit(-1);
  21. }
  22. strcpy(person->first_name, *name);
  23. person->id = id++;
  24. HASH_ADD_STR(people,first_name,person);
  25. printf("added %s (id %d)\n", person->first_name, person->id);
  26. }
  27. person=NULL;
  28. person_t **p=&person;
  29. for(name=names; *name!=NULL; name++) {
  30. HASH_FIND_STR(people,*name,*p);
  31. if (person != NULL) {
  32. printf("found %s (id %d)\n", person->first_name, person->id);
  33. new_person = (person_t *)malloc(sizeof(person_t));
  34. if (new_person == NULL) {
  35. exit(-1);
  36. }
  37. memcpy(new_person, person, sizeof(person_t));
  38. new_person->id = person->id*10;
  39. HASH_REPLACE_STR(people,first_name,new_person,tmp);
  40. printf("replaced (%c) with %s (id %d)\n", (tmp!=NULL)?'y':'n', new_person->first_name, new_person->id);
  41. if (tmp != NULL) {
  42. free(tmp);
  43. }
  44. } else {
  45. printf("failed to find %s\n", *name);
  46. }
  47. }
  48. printf("traversing... \n");
  49. HASH_ITER(hh, people, person, tmp) {
  50. printf("%s (id %d)\n", person->first_name, person->id);
  51. HASH_DEL(people,person);
  52. free(person);
  53. }
  54. return 0;
  55. }