Advertisement
Guest User

Untitled

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