Main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <malloc.h>
  5. typedef void (*bint)(int);
  6. struct ElemListBint
  7. {
  8. bint fun;
  9. struct ElemListBint* nextfun;
  10. };
  11. typedef struct ElemListBint ElemListBint;
  12. typedef ElemListBint* List;
  13. void kv(int x)
  14. {
  15. printf("Êâàäðàò: %d\n", x * x);
  16. }
  17. void kub(int x)
  18. {
  19. printf("Êóá: %d\n", x * x * x);
  20. }
  21. List createListFun()
  22. {
  23. List list = calloc(1, sizeof(ElemListBint));
  24. return list;
  25. }
  26. List addFun(List list, bint value)
  27. {
  28. List head = calloc(1, sizeof(ElemListBint));
  29. if (list->fun == NULL && list->nextfun == NULL)//åñëè ïðèøåë ïóñòûì, òî íîâàÿ ãîëîâà
  30. {
  31. head->fun = value;
  32. head->nextfun = NULL;
  33. }
  34. else //åñëè ïðèøåë íå ïóñòûì, òî ãîëîâà ñòàðàÿ
  35. {
  36. head = list;
  37. //äîëèñòûâàåì äî ïîñëåäíåãî
  38. while (list->nextfun)
  39. {
  40. list = list->nextfun;
  41. }
  42. List newTask = calloc(1, sizeof(ElemListBint));
  43. newTask->fun = value;
  44. newTask->nextfun = NULL;
  45. list->nextfun = newTask;
  46. }
  47. return head;
  48. }
  49. void watchList(List list, int a)
  50. {
  51. if (list == NULL) return NULL;
  52. while (list)
  53. {
  54. list->fun(a);
  55. if (list->nextfun)list = list->nextfun;
  56. else break;
  57. }
  58. printf("\n");
  59. }
  60. List changePath(List list, List task)
  61. {
  62. if (!list) return NULL;
  63. List head = list;
  64. while (list)
  65. {
  66. if (list->nextfun == task)
  67. {
  68. list->nextfun = list->nextfun->nextfun;
  69. return head;
  70. }
  71. list = list->nextfun;
  72. }
  73. return head;
  74. }
  75. List delFun(List list, bint nameFun)
  76. {
  77. if (!list) return NULL;
  78. List head = list;
  79. while (list)
  80. {
  81. List thisFun = list;
  82. list = list->nextfun;
  83. if (nameFun == thisFun->fun && thisFun == head)
  84. {
  85. head = thisFun->nextfun;
  86. free(thisFun);
  87. }
  88. else if (nameFun == thisFun->fun)
  89. {
  90. head = changePath(head, thisFun);
  91. free(thisFun);
  92. }
  93. if (!list)
  94. {
  95. thisFun->nextfun = NULL;
  96. }
  97. }
  98. return head;
  99. }
  100. void main()
  101. {
  102. system("chcp 1251");
  103. bint funKv = kv;
  104. bint funKub = kub;
  105. List listFun = createListFun();
  106. listFun = addFun(listFun, funKv);
  107. listFun = addFun(listFun, funKub);
  108. listFun = addFun(listFun, funKub);
  109. watchList(listFun, 10);
  110. listFun = delFun(listFun, funKv);
  111. watchList(listFun, 10);
  112. listFun = deldobleFun(listFun);
  113. }