fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct _TNode
  6. {
  7. int value;
  8. struct _TNode* next;
  9. } TNode;
  10.  
  11. //-----------------------------------------------------------------------------
  12. TNode* Push(TNode** list, int value)
  13. {
  14. TNode* node = malloc(sizeof(TNode));
  15. node->value = value;
  16. node->next = *list;
  17.  
  18. *list = node;
  19.  
  20. return *list;
  21. }
  22. //-----------------------------------------------------------------------------
  23. int AllElements(int i)
  24. {
  25. return 1;
  26. }
  27. //-----------------------------------------------------------------------------
  28. int OnlyOddElements(int i)
  29. {
  30. return i & 1;
  31. }
  32. //-----------------------------------------------------------------------------
  33. void Print(const TNode* list, int (*Func)(int))
  34. {
  35. int i = 0;
  36. for (; list; list = list->next)
  37. {
  38. if (Func(++i))
  39. {
  40. printf("%d ", list->value);
  41. }
  42. }
  43. printf("\n");
  44. }
  45. //-----------------------------------------------------------------------------
  46. int GetMultiplication(const TNode* list)
  47. {
  48. int result = 1;
  49.  
  50. for (; list; list = list->next)
  51. {
  52. result *= list->value;
  53. }
  54.  
  55. return result;
  56. }
  57. //-----------------------------------------------------------------------------
  58.  
  59. int main(int argc, char* argv[])
  60. {
  61. TNode* list = NULL;
  62. int i = 10;
  63.  
  64. srand(time(NULL));
  65.  
  66. while (i--)
  67. {
  68. Push(&list, rand() % 10 + 1);
  69. }
  70.  
  71. Print(list, AllElements);
  72.  
  73. printf("multiplication = %d\n", GetMultiplication(list));
  74.  
  75. Print(list, OnlyOddElements);
  76.  
  77. return EXIT_SUCCESS;
  78. }
Success #stdin #stdout 0s 10304KB
stdin
Standard input is empty
stdout
10 8 7 2 7 2 9 4 1 4 
multiplication = 2257920
10 7 7 9 1