#include #include #include #include typedef void (*bint)(int); struct ElemListBint { bint fun; struct ElemListBint* nextfun; }; typedef struct ElemListBint ElemListBint; typedef ElemListBint* List; void kv(int x) { printf("Квадрат: %d\n", x * x); } void kub(int x) { printf("Куб: %d\n", x * x * x); } List createListFun() { List list = calloc(1, sizeof(ElemListBint)); return list; } List addFun(List list, bint value) { List head = calloc(1, sizeof(ElemListBint)); if (list->fun == NULL && list->nextfun == NULL)//если пришел пустым, то новая голова { head->fun = value; head->nextfun = NULL; } else //если пришел не пустым, то голова старая { head = list; //долистываем до последнего while (list->nextfun) { list = list->nextfun; } List newTask = calloc(1, sizeof(ElemListBint)); newTask->fun = value; newTask->nextfun = NULL; list->nextfun = newTask; } return head; } void watchList(List list, int a) { if (list == NULL) return NULL; while (list) { list->fun(a); if (list->nextfun)list = list->nextfun; else break; } printf("\n"); } List changePath(List list, List task) { if (!list) return NULL; List head = list; while (list) { if (list->nextfun == task) { list->nextfun = list->nextfun->nextfun; return head; } list = list->nextfun; } return head; } List delFun(List list, bint nameFun) { if (!list) return NULL; List head = list; while (list) { List thisFun = list; list = list->nextfun; if (nameFun == thisFun->fun && thisFun == head) { head = thisFun->nextfun; free(thisFun); } else if (nameFun == thisFun->fun) { head = changePath(head, thisFun); free(thisFun); } if (!list) { thisFun->nextfun = NULL; } } return head; } void main() { system("chcp 1251"); bint funKv = kv; bint funKub = kub; List listFun = createListFun(); listFun = addFun(listFun, funKv); listFun = addFun(listFun, funKub); listFun = addFun(listFun, funKub); watchList(listFun, 10); listFun = delFun(listFun, funKv); watchList(listFun, 10); listFun = deldobleFun(listFun); }