Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio .h>
  2. #include <stdlib .h>
  3. #include <stdbool .h>
  4.  
  5. // Definicja typu bazowego jednokierunkowej listy liniowej oraz
  6. // podstawowych operacji wykonywanych na liście ( patrz kod źródłowy 1).
  7.  
  8. void perform_operation(struct sll_node *node,void (*operation)( struct sll_node *node))
  9. {
  10.     if((NULL != node) && ( NULL != operation))
  11.     {
  12.         operation(node);
  13.         perform_operation(node->next,operation);
  14.     }
  15. }
  16.  
  17. void print_node(struct sll_node *node)
  18. {
  19.     printf("%d ", node->data);
  20. }
  21.  
  22. int evaluate_operation(struct sll_node *node,int (*operation)(int result, struct sll_node *node))
  23. {
  24.     if (( NULL != node ) && ( NULL !=operation))
  25.         return operation(evaluate_operation(node->next ,operation),node);
  26.         return 0;
  27. }
  28.  
  29. int count_nodes(int result ,struct sll_node *node)
  30. {
  31.     return result+1;
  32. }
  33.  
  34. int sum_nodes(int result,struct sll_node *node)
  35. {
  36.     return result+node->data;
  37. }
  38.  
  39. int main ()
  40. {
  41.     struct sll_node *front = NULL;
  42.  
  43. // Utworzenie listy i dodanie do niej przyk ł adowych element ów
  44. // ( patrz kod źródłowy 1).
  45.  
  46.     printf ("List elements printed with higher order function:\n");
  47.     perform_operation (front,print_node);
  48.     printf ("\n");
  49.     printf ("Number of list elements: %d\n",
  50.         evaluate_operation (front, count_nodes));
  51.     printf ("Sum of list elements : %d\n",
  52.         evaluate_operation (front, sum_nodes));
  53.  
  54.     remove_list (&front);
  55.     front = NULL;
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement