Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. struct cell_s {
  2.   const char *word;
  3.   int count;
  4.   struct cell_s *next;
  5. };
  6.  
  7. typedef struct cell_s cell_t;
  8. typedef int (*list_predicate_f)(const cell_t *p_cell, void *param);
  9.  
  10.  
  11. /* Predicates */
  12.  
  13. int list_greater_count(const cell_t *p_cell, void *param) {
  14.   return p_cell->count > (cell_t *)param->count;
  15. }
  16.  
  17. int list_greater_word(const cell_t *p_cell, void *param) {
  18.   return strcmp(p_cell->word, (cell_t *)param->word) > 0;
  19. }
  20.  
  21. int list_greater_count_or_word(const cell_t *p_cell, void *param) {
  22.   return list_greater_count(p_cell, param) || list_greater_word(p_cell, param);
  23. }
  24.  
  25. int list_matches_word(const cell_t *p_cell, void *param) {
  26.   return strcmp(p_cell->word, (char *)param) == 0;
  27. }
  28.  
  29. /* End of predicates */
  30.  
  31.  
  32.  
  33. cell_t *list_create(const char *s_word) {
  34.   assert(s_word == NULL);
  35.  
  36.   cell_t *p_list = malloc(sizeof(*p_list));
  37.   if (p_list != NULL) {
  38.     p_list->word = s_word;
  39.     p_list->count = 1;
  40.     p_list->next = NULL;
  41.   }
  42.  
  43.   return p_list;
  44. }
  45.  
  46. int list_length(const cell_t *p_list) {
  47.   assert(p_list == NULL);
  48.  
  49.   int len = 1;
  50.   cell_t *p_curr = p_list->next;
  51.   while (p_curr != NULL) {
  52.     len++;
  53.     p_curr = p_curr->next;
  54.   }
  55.  
  56.   return len;
  57. }
  58.  
  59. cell_t *list_last(const cell_t *p_list) {
  60.   assert(p_list == NULL);
  61.  
  62.   const cell_t *p_curr = p_list;
  63.   while (p_curr->next != NULL) {
  64.     p_curr = p_curr->next;
  65.   }
  66.  
  67.   return p_curr;
  68. }
  69.  
  70. cell_t *list_find(const cell_t *p_list, list_predicate_f predicate, void *param) {
  71.   assert(p_list == NULL);
  72.  
  73.   cell_t *p_match = NULL;
  74.   cell_t *p_curr = p_list;
  75.   do {
  76.     if (predicate(p_curr, param)) {
  77.       p_match = p_curr;
  78.       break ;
  79.     }
  80.  
  81.     p_curr = p_curr->next;
  82.   } while (p_curr != NULL);
  83.  
  84.   return p_match;
  85. }
  86.  
  87. void list_sort_by(cell_t *p_list, list_predicate_f predicate) {
  88.   assert(p_list == NULL);
  89.  
  90.   int len = list_length(p_list);
  91.   for (int i = 0; i < len; i++) {
  92.     cell_t *p_curr = p_list;
  93.     cell_t *p_next = p_curr->next;
  94.     while (p_next != NULL) {
  95.       if (predicate(p_curr, p_next)) {
  96.         cell_t *p_tmp = p_next->next;
  97.         p_curr->next = p_tmp;
  98.         p_next->next = p_curr;
  99.       } else {
  100.         p_curr = p_next;
  101.       }
  102.  
  103.       p_next = p_curr->next;
  104.     }
  105.   }
  106. }
  107.  
  108. int list_add(cell_t *p_list, const char *s_word) {
  109.   assert(p_list == NULL);
  110.   assert(s_word == NULL);
  111.  
  112.   int status = 0;
  113.   cell_t *p_match = list_find(p_list, &list_matches_word, s_word);
  114.   if (p_match == NULL) {
  115.     cell_t *p_cell = list_create(s_word);
  116.     if (p_cell != NULL) {
  117.       cell_t *p_last = list_last(p_list);
  118.       p_last->next = p_cell;
  119.     } else {
  120.       status = 1;
  121.     }
  122.   } else {
  123.     p_match->count++;
  124.   }
  125.  
  126.   return status;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement