Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio .h>
- #include <stdlib .h>
- #include <stdbool .h>
- // Definicja typu bazowego jednokierunkowej listy liniowej oraz
- // podstawowych operacji wykonywanych na liście ( patrz kod źródłowy 1).
- void perform_operation(struct sll_node *node,void (*operation)( struct sll_node *node))
- {
- if((NULL != node) && ( NULL != operation))
- {
- operation(node);
- perform_operation(node->next,operation);
- }
- }
- void print_node(struct sll_node *node)
- {
- printf("%d ", node->data);
- }
- int evaluate_operation(struct sll_node *node,int (*operation)(int result, struct sll_node *node))
- {
- if (( NULL != node ) && ( NULL !=operation))
- return operation(evaluate_operation(node->next ,operation),node);
- return 0;
- }
- int count_nodes(int result ,struct sll_node *node)
- {
- return result+1;
- }
- int sum_nodes(int result,struct sll_node *node)
- {
- return result+node->data;
- }
- int main ()
- {
- struct sll_node *front = NULL;
- // Utworzenie listy i dodanie do niej przyk ł adowych element ów
- // ( patrz kod źródłowy 1).
- printf ("List elements printed with higher order function:\n");
- perform_operation (front,print_node);
- printf ("\n");
- printf ("Number of list elements: %d\n",
- evaluate_operation (front, count_nodes));
- printf ("Sum of list elements : %d\n",
- evaluate_operation (front, sum_nodes));
- remove_list (&front);
- front = NULL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement