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.

66 lines
1.3KB

  1. /* Minified version of code from tinydtls 0.9 */
  2. /* See https://projects.eclipse.org/projects/iot.tinydtls */
  3. #include "utlist.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. typedef int clock_time_t;
  8. typedef struct netq_t {
  9. struct netq_t *next;
  10. clock_time_t t;
  11. } netq_t;
  12. void dump_queue(struct netq_t *queue)
  13. {
  14. struct netq_t *p;
  15. int i = 0;
  16. if (!queue) {
  17. printf("(null)\n");
  18. } else {
  19. LL_FOREACH(queue, p) {
  20. printf("node #%d, timeout: %d\n", i++, p->t);
  21. }
  22. }
  23. }
  24. int netq_insert_node(netq_t **queue, netq_t *node)
  25. {
  26. netq_t *p = *queue;
  27. while (p && p->t <= node->t) {
  28. p = p->next;
  29. }
  30. /* *INDENT-OFF* */
  31. if (p)
  32. LL_PREPEND_ELEM(*queue, p, node);
  33. else
  34. LL_APPEND(*queue, node);
  35. /* *INDENT-ON* */
  36. return 1;
  37. }
  38. int main()
  39. {
  40. struct netq_t *nq = NULL;
  41. size_t i;
  42. clock_time_t timestamps[] = { 300, 100, 200, 400, 500 };
  43. for (i = 0; i < sizeof(timestamps)/sizeof(clock_time_t); i++) {
  44. struct netq_t *node = (struct netq_t *)malloc(sizeof *node);
  45. memset(node, '\0', sizeof *node);
  46. node->t = timestamps[i];
  47. if (netq_insert_node(&nq, node) != 1) {
  48. puts("ERROR");
  49. }
  50. }
  51. dump_queue(nq);
  52. return 0;
  53. }