diff --git a/Homework/SC/app b/Homework/SC/app index ed9480c..39ab8d7 100755 Binary files a/Homework/SC/app and b/Homework/SC/app differ diff --git a/Homework/SC/func.c b/Homework/SC/func.c index 306e491..bd3270c 100644 --- a/Homework/SC/func.c +++ b/Homework/SC/func.c @@ -2,7 +2,7 @@ #include #include -void linked_print(Stack *l) +void linked_print(Stack *l) // Вывод стека { while (l != NULL) { @@ -11,7 +11,7 @@ void linked_print(Stack *l) } } -Stack *add_head(Stack *l, double x) +Stack *add_head(Stack *l, double x) // Добавление головы, причем в двух вариациях(если голова изначально есть и если её нет) { if (l == NULL) { @@ -22,13 +22,6 @@ Stack *add_head(Stack *l, double x) l->next = NULL; } - if (l->x == 0.0) - { - l->x = x; - l->next = NULL; - return l; - } - Stack *tmp = malloc(sizeof(Stack)); if (tmp == NULL) exit(1); @@ -53,26 +46,3 @@ Stack *del_head(Stack *l) l = NULL; return tmp; } - -int len(Stack *l) -{ - int res = 0; - while (l != NULL) - { - res++; - l = l->next; - } - return res; -} - -Stack get(Stack *l, int n) -{ - if (n > len(l)) - exit(3); - int i; - for (i = 0; i < n; i++) - { - l = l->next; - } - return *l; -} \ No newline at end of file diff --git a/Homework/SC/head.h b/Homework/SC/head.h index 50c53fd..6a1e8bc 100644 --- a/Homework/SC/head.h +++ b/Homework/SC/head.h @@ -9,5 +9,3 @@ typedef struct Stack void linked_print(Stack *l); Stack *add_head(Stack *l, double x); Stack *del_head(Stack *l); -int len(Stack *l); -Stack get(Stack *l, int n); \ No newline at end of file diff --git a/Homework/SC/main.c b/Homework/SC/main.c index 897ed20..a91125d 100644 --- a/Homework/SC/main.c +++ b/Homework/SC/main.c @@ -2,16 +2,12 @@ #include #include #include -// Выдает неадекватные значения + int main(int argc, char *argv[]) { srand(time(0)); - Stack *numbers = malloc(sizeof(Stack)); - if (numbers == NULL) - exit(1); - + Stack *numbers = NULL; char c; - char flag = 'n'; while ((c = getchar()) != EOF) { @@ -21,40 +17,23 @@ int main(int argc, char *argv[]) } else if (c == ' ') { - flag = 's'; continue; } switch (c) { case '0' ... '9': - if (flag == 's') - { - numbers = add_head(numbers, (double)(c - '0')); - flag = 'n'; - } - else - { - double a = get(numbers, 0).x; - numbers = del_head(numbers); - a = a * 10 + c - '0'; - numbers = add_head(numbers, a); - } + numbers = add_head(numbers, (double)(c - '0')); break; case '+': case '-': case '*': case '/': - if (len(numbers) < 2) - { - printf("Invalid expression\n"); - exit(2); - } - double a = get(numbers, 0).x; + double a = numbers->x; numbers = del_head(numbers); - double b = get(numbers, 0).x; + double b = numbers->x; numbers = del_head(numbers); b = (c == '+') ? b + a : b; @@ -67,7 +46,7 @@ int main(int argc, char *argv[]) } } - printf("res: %lF\n", get(numbers, 0).x); + printf("res: %lF\n", numbers->x); free(numbers); return 0; diff --git a/Homework/Trees/app b/Homework/Trees/app index 9bb9420..0a84233 100755 Binary files a/Homework/Trees/app and b/Homework/Trees/app differ diff --git a/Homework/Trees/func.c b/Homework/Trees/func.c index a16b95b..7946aa3 100644 --- a/Homework/Trees/func.c +++ b/Homework/Trees/func.c @@ -2,12 +2,12 @@ #include #include "head.h" -tree *init_root(tree *root, int field) // Выделение памяти под корень дерева +tree *init_root(tree *root, int field) { - tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу - tmp->field = field; // Присваивание указателю на родителя значения NULL - tmp->parent = NULL; // Присваивание указателю на левое и правое поддерево значения NULL - tmp->left = tmp->right = NULL; + tree *tmp = malloc(sizeof(tree)); // Выделение памяти под корень дерева + tmp->field = field; // Присваивание значения ключу + tmp->parent = NULL; // Присваивание указателю на родителя значения NULL + tmp->left = tmp->right = NULL; // Присваивание указателю на левое и правое поддерево значения NULL root = tmp; return root; } @@ -79,9 +79,13 @@ tree *findForDel(tree *root) tree *del_node(tree *root, int field) { + printf("%d\n", root->field); // Поиск удаляемого узла по ключу tree *rig = root, *lef = NULL, *tmp = NULL; + // preorder(root); + printf("test"); lef = find_node(root, field); + // 1 случай: у узла нет потомков if ((lef->left == NULL) && (lef->right == NULL)) { @@ -130,28 +134,28 @@ void preorder(tree *root) { if (root == NULL) return; - if (root -> field) - printf("%d ", root -> field); - preorder(root -> left); - preorder(root -> right); + if (root->field) + printf("%d ", root->field); + preorder(root->left); + preorder(root->right); } void postorder(tree *root) { if (root == NULL) return; - postorder(root -> left); - postorder(root -> right); - if (root -> field) - printf("%d ", root -> field); + postorder(root->left); + postorder(root->right); + if (root->field) + printf("%d ", root->field); } void inorder(tree *root) { if (root == NULL) return; - inorder(root -> left); - if (root -> field) - printf("%d ", root -> field); - inorder(root -> right); + inorder(root->left); + if (root->field) + printf("%d ", root->field); + inorder(root->right); } \ No newline at end of file diff --git a/Homework/Trees/main.c b/Homework/Trees/main.c index 5a04383..aa24cbc 100644 --- a/Homework/Trees/main.c +++ b/Homework/Trees/main.c @@ -7,10 +7,10 @@ int main(int argc, char *argv[]) { tree *root = NULL; root = init_root(root, 'A'); - preorder(root); - printf("\n"); + // preorder(root); root = del_node(root, 'A'); - preorder(root); + exit(0); + // preorder(root); printf("\n"); root = add_node(root, 'F'); root = add_node(root, 'B'); diff --git a/other/Vova/2_in_1.c b/other/Vova/2_in_1.c new file mode 100644 index 0000000..48bd84d --- /dev/null +++ b/other/Vova/2_in_1.c @@ -0,0 +1,160 @@ +#include +#include +#include +#include + +#define N 3 +#define LENG 3 + +typedef struct Node { + struct Node* next; + struct Node* pr; + char name[LENG]; +} Node; + +void node_print(Node *n) { + while (n != NULL) { + printf("%s\n", n->name); + n = n->next; + } +} + +Node *add_tail(Node *n, char s[]) { + if (n == NULL) { + if ((n = malloc(sizeof(Node))) == NULL) { + printf("Deny malloc\n"); + exit(1); + } + n->next = NULL; + n->pr = NULL; + memcpy(n->name, s, LENG); + return n; + } + if (!strcmp(n->name, "\0")) { + memcpy(n->name, s, LENG); + n->next = NULL; + n->pr = NULL; + return n; + } + + Node *res = n; + while (n->next != NULL) + n = n->next; + + Node *tmp = malloc(sizeof(Node)); + memcpy(tmp->name, s, LENG); + tmp->pr = n; + n->next = tmp; + return res; +} + +Node get(Node *node, int n) { + int i; + for (i = 0; i < n; i++) { + node = node->next; + } + return *node; +} + +int len(Node *n) { + int res = 0; + while (n != NULL) { + res++; + n = n->next; + } + return res; +} + +Node *find_0(Node *n1, Node *n2) { + int i, j, f = 0; + Node *res = NULL; + if ((res = malloc(sizeof(Node))) == NULL) { + printf("Deny malloc\n"); + exit(3); + } + + for (i = 0; i < len(n1); i++) { + Node tmp1 = get(n1, i); + for (j = 0; j < len(n2); j++) { + Node tmp2 = get(n2, j); + if (strcmp(tmp1.name, tmp2.name) == 0) { + f = 1; + } + } + if (f == 0) { + Node tmp = get(n1, i); + res = add_tail(res, tmp.name); + } + f = 0; + } + + for (i = 0; i < len(n2); i++) { + Node tmp2 = get(n2, i); + for (j = 0; j < len(n1); j++) { + Node tmp1 = get(n1, j); + if (strcmp(tmp2.name, tmp1.name) == 0) { + f = 1; + } + } + if (f == 0) { + Node tmp = get(n2, i); + res = add_tail(res, tmp.name); + } + f = 0; + } + + return res; +} + + +int main(int argc, char* argv[]) { + printf("Hello. Let's begin\n"); + printf("\n"); + srand(time(0)); + Node* n1 = NULL; + Node* n2 = NULL; + Node* n3 = NULL; + if ((n1 = malloc(sizeof(Node))) == NULL || + (n2 = malloc(sizeof(Node))) == NULL || + (n3 = malloc(sizeof(Node))) == NULL) { + printf("Deny malloc\n"); + exit(2); + } + + int i; + for (i = 0; i < N; i++) { + char s[LENG]; + int j; + + for (j = 0; j < LENG - 1; j++) + s[j] = 'a' + rand() % 2; + s[LENG - 1] = '\0'; + + n1 = add_tail(n1, s); + + for (j = 0; j < LENG - 1; j++) + s[j]= 'a' + rand() % 2; + s[LENG - 1] = '\0'; + n2 = add_tail(n2, s); + } + + printf("Initial arrays:\n"); + node_print(n1); + printf("\n"); + node_print(n2); + + n3 = find_0(n1, n2); + + printf("\n"); + printf("Final array:\n"); + node_print(n3); + + free(n1); + free(n2); + free(n3); + + printf("\n"); + printf("That's all for now\n"); + + return 0; +} diff --git a/other/Vova/app b/other/Vova/app new file mode 100755 index 0000000..a4e631d Binary files /dev/null and b/other/Vova/app differ