rclist.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <grass/gis.h>
  2. #include <grass/glocale.h>
  3. #include "rclist.h"
  4. void rclist_init(struct rclist *list)
  5. {
  6. list->head = list->tail = NULL;
  7. return;
  8. }
  9. void rclist_add(struct rclist *list, int row, int col)
  10. {
  11. struct rc *new = G_malloc(sizeof(struct rc));
  12. if (!new)
  13. G_fatal_error(_("rclist out of memory"));
  14. new->next = NULL;
  15. new->row = row;
  16. new->col = col;
  17. if (list->head) {
  18. list->head->next = new;
  19. list->head = new;
  20. }
  21. else {
  22. list->head = list->tail = new;
  23. }
  24. return;
  25. }
  26. /* return 1 if an element was dropped
  27. * return 0 if list is empty
  28. */
  29. int rclist_drop(struct rclist *list, struct rc *rc)
  30. {
  31. if (list->tail) {
  32. struct rc *next = list->tail->next;
  33. rc->row = list->tail->row;
  34. rc->col = list->tail->col;
  35. G_free(list->tail);
  36. list->tail = next;
  37. if (!list->tail)
  38. list->head = NULL;
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. void rclist_destroy(struct rclist *list)
  44. {
  45. struct rc *next = list->tail;
  46. while (next) {
  47. next = next->next;
  48. G_free(list->tail);
  49. list->tail = next;
  50. }
  51. list->head = NULL;
  52. return;
  53. }