fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5. int info;
  6. node *next;
  7. };
  8.  
  9. int Sum_Even_Values(node *head)
  10. {
  11. int sum = 0;
  12. if (head) {
  13. if ((head->info % 2) == 0) {
  14. cout << " + " << head->info;
  15. sum = head->info;
  16. }
  17. sum += Sum_Even_Values(head->next);
  18. }
  19. return sum;
  20. }
  21.  
  22. int main() {
  23. // ignore that the nodes are on the stack instead of the heap,
  24. // this is just for demonstration purposes, how the nodes are
  25. // allocated is irrelevant...
  26. node nodes[10];
  27. for(int i = 0; i < 10; ++i) {
  28. nodes[i].info = i+1;
  29. nodes[i].next = &nodes[i+1];
  30. }
  31. nodes[9].next = 0;
  32.  
  33. int sum = Sum_Even_Values(nodes);
  34. cout << " = " << sum;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 4216KB
stdin
Standard input is empty
stdout
 + 2 + 4 + 6 + 8 + 10 = 30