123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <malloc.h>
- 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);
- }
|