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.

61 lines
1.5KB

  1. #include "uthash.h"
  2. #include <stdlib.h> /* malloc */
  3. #include <stdio.h> /* printf */
  4. typedef struct example_user_t {
  5. int id;
  6. int cookie;
  7. UT_hash_handle hh;
  8. } example_user_t;
  9. int main()
  10. {
  11. int i;
  12. unsigned c;
  13. example_user_t *user, *tmp, *users=NULL;
  14. /* create elements */
  15. for(i=0; i<10; i++) {
  16. if ( (user = (example_user_t*)malloc(sizeof(example_user_t))) == NULL) {
  17. exit(-1);
  18. }
  19. user->id = i;
  20. user->cookie = i*i;
  21. HASH_ADD_INT(users,id,user);
  22. }
  23. /* show the hash */
  24. for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
  25. printf("user %d, cookie %d\n", user->id, user->cookie);
  26. }
  27. c = HASH_COUNT(users);
  28. printf("%u users. Deleting odd id's...\n", c);
  29. /* delete the odd id's */
  30. HASH_ITER(hh, users, user, tmp) {
  31. if ((user->id & 1) != 0) {
  32. HASH_DEL(users,user);
  33. }
  34. }
  35. /* show the hash */
  36. for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
  37. printf("user %d, cookie %d\n", user->id, user->cookie);
  38. }
  39. c = HASH_COUNT(users);
  40. printf("%u users. Deleting remaining id's...\n", c);
  41. /* delete all that are left */
  42. HASH_ITER(hh, users, user, tmp) {
  43. HASH_DEL(users,user);
  44. }
  45. c = HASH_COUNT(users);
  46. printf("%u users.\n", c);
  47. /* show the hash */
  48. for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
  49. printf("user %d, cookie %d\n", user->id, user->cookie);
  50. }
  51. return 0;
  52. }