sorted_list.c 946 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * sorted_list.c:
  3. *
  4. */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include "sorted_list.h"
  8. #include "iftop.h"
  9. void sorted_list_insert(sorted_list_type* list, void* item) {
  10. sorted_list_node *node, *p;
  11. p = &(list->root);
  12. while(p->next != NULL && list->compare(item, p->next->data) > 0) {
  13. p = p->next;
  14. }
  15. node = xmalloc(sizeof *node);
  16. node->next = p->next;
  17. node->data = item;
  18. p->next = node;
  19. }
  20. sorted_list_node* sorted_list_next_item(sorted_list_type* list, sorted_list_node* prev) {
  21. if(prev == NULL) {
  22. return list->root.next;
  23. }
  24. else {
  25. return prev->next;
  26. }
  27. }
  28. void sorted_list_destroy(sorted_list_type* list) {
  29. sorted_list_node *p, *n;
  30. p = list->root.next;
  31. while(p != NULL) {
  32. n = p->next;
  33. free(p);
  34. p = n;
  35. }
  36. list->root.next = NULL;
  37. }
  38. void sorted_list_initialise(sorted_list_type* list) {
  39. list->root.next = NULL;
  40. }