Advertisement
Guest User

Untitled

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