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.

51 lines
1.1KB

  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. example_user_t *user, *tmp, *users=NULL;
  13. /* create elements */
  14. for(i=0; i<10; i++) {
  15. user = (example_user_t*)malloc(sizeof(example_user_t));
  16. if (user == NULL) {
  17. exit(-1);
  18. }
  19. user->id = i;
  20. user->cookie = i*i;
  21. HASH_ADD_INT(users,id,user);
  22. }
  23. /* delete each even ID */
  24. for(i=0; i<10; i+=2) {
  25. HASH_FIND_INT(users,&i,tmp);
  26. if (tmp != NULL) {
  27. HASH_DEL(users,tmp);
  28. free(tmp);
  29. } else {
  30. printf("user id %d not found\n", i);
  31. }
  32. }
  33. i=9;
  34. HASH_FIND_INT(users,&i,tmp);
  35. if (tmp != NULL) {
  36. while (tmp != NULL) {
  37. printf("id %d, following prev...\n", tmp->id);
  38. tmp = (example_user_t*)tmp->hh.prev;
  39. }
  40. } else {
  41. printf("user id %d not found\n", i);
  42. }
  43. return 0;
  44. }