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.

64 lines
1.7KB

  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. static int rev(void *_a, void *_b)
  10. {
  11. example_user_t *a = (example_user_t*)_a;
  12. example_user_t *b = (example_user_t*)_b;
  13. printf("called for a:%d, b:%d\n",a->id, b->id);
  14. return (a->id - b->id);
  15. }
  16. int main()
  17. {
  18. int i;
  19. example_user_t *user, *users=NULL;
  20. /* create elements */
  21. for(i=9; i>=0; i--) {
  22. user = (example_user_t*)malloc(sizeof(example_user_t));
  23. if (user == NULL) {
  24. exit(-1);
  25. }
  26. user->id = i;
  27. user->cookie = i*i;
  28. HASH_ADD_INT(users,id,user);
  29. }
  30. for(user=users; user != NULL; user=(example_user_t*)user->hh.next) {
  31. printf("user %d, cookie %d\n", user->id, user->cookie);
  32. }
  33. printf("sorting\n");
  34. HASH_SORT(users,rev);
  35. for(user=users; user != NULL; user=(example_user_t*)user->hh.next) {
  36. printf("user %d, cookie %d\n", user->id, user->cookie);
  37. }
  38. printf("adding 10-20\n");
  39. for(i=20; i>=10; i--) {
  40. user = (example_user_t*)malloc(sizeof(example_user_t));
  41. if (user == NULL) {
  42. exit(-1);
  43. }
  44. user->id = i;
  45. user->cookie = i*i;
  46. HASH_ADD_INT(users,id,user);
  47. }
  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. printf("sorting\n");
  52. HASH_SORT(users,rev);
  53. for(user=users; user != NULL; user=(example_user_t*)user->hh.next) {
  54. printf("user %d, cookie %d\n", user->id, user->cookie);
  55. }
  56. return 0;
  57. }