work in progress...1

This commit is contained in:
ada.dmitry
2023-05-16 09:40:30 +03:00
parent 0cf222ed89
commit 58a2573b52
14 changed files with 224 additions and 165 deletions
Binary file not shown.
@@ -1,6 +1,6 @@
#include "head.h" #include "head.h"
void node_print(Node *n) void node_print(Unit *n)
{ {
while (n != NULL) while (n != NULL)
{ {
@@ -9,7 +9,7 @@ void node_print(Node *n)
} }
} }
Node get(Node *node, int n) Unit get(Unit *node, int n)
{ {
int i; int i;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
@@ -19,7 +19,7 @@ Node get(Node *node, int n)
return *node; return *node;
} }
int len(Node *n) int len(Unit *n)
{ {
int res = 0; int res = 0;
while (n != NULL) while (n != NULL)
@@ -30,11 +30,11 @@ int len(Node *n)
return res; return res;
} }
Node *xor_find(Node *n1, Node *n2) Unit *xor_find(Unit *n1, Unit *n2)
{ {
int i, j, f = 0; int i, j, f = 0;
Node *res = NULL; Unit *res = NULL;
if ((res = malloc(sizeof(Node))) == NULL) if ((res = malloc(sizeof(Unit))) == NULL)
{ {
printf("could not malloc\n"); printf("could not malloc\n");
exit(3); exit(3);
@@ -42,10 +42,10 @@ Node *xor_find(Node *n1, Node *n2)
for (i = 0; i < len(n1); i++) for (i = 0; i < len(n1); i++)
{ {
Node tmp1 = get(n1, i); Unit tmp1 = get(n1, i);
for (j = 0; j < len(n2); j++) for (j = 0; j < len(n2); j++)
{ {
Node tmp2 = get(n2, j); Unit tmp2 = get(n2, j);
if (strcmp(tmp1.name, tmp2.name) == 0) if (strcmp(tmp1.name, tmp2.name) == 0)
{ {
f = 1; f = 1;
@@ -53,7 +53,7 @@ Node *xor_find(Node *n1, Node *n2)
} }
if (f == 0) if (f == 0)
{ {
Node tmp = get(n1, i); Unit tmp = get(n1, i);
res = add_tail(res, tmp.name); res = add_tail(res, tmp.name);
} }
f = 0; f = 0;
@@ -61,10 +61,10 @@ Node *xor_find(Node *n1, Node *n2)
for (i = 0; i < len(n2); i++) for (i = 0; i < len(n2); i++)
{ {
Node tmp2 = get(n2, i); Unit tmp2 = get(n2, i);
for (j = 0; j < len(n1); j++) for (j = 0; j < len(n1); j++)
{ {
Node tmp1 = get(n1, j); Unit tmp1 = get(n1, j);
if (strcmp(tmp2.name, tmp1.name) == 0) if (strcmp(tmp2.name, tmp1.name) == 0)
{ {
f = 1; f = 1;
@@ -72,7 +72,7 @@ Node *xor_find(Node *n1, Node *n2)
} }
if (f == 0) if (f == 0)
{ {
Node tmp = get(n2, i); Unit tmp = get(n2, i);
res = add_tail(res, tmp.name); res = add_tail(res, tmp.name);
} }
f = 0; f = 0;
@@ -81,11 +81,11 @@ Node *xor_find(Node *n1, Node *n2)
return res; return res;
} }
Node *add_tail(Node *n, char s[]) Unit *add_tail(Unit *n, char s[])
{ {
if (n == NULL) if (n == NULL)
{ {
if ((n = malloc(sizeof(Node))) == NULL) if ((n = malloc(sizeof(Unit))) == NULL)
{ {
printf("could not malloc\n"); printf("could not malloc\n");
exit(1); exit(1);
@@ -103,11 +103,11 @@ Node *add_tail(Node *n, char s[])
return n; return n;
} }
Node *res = n; Unit *res = n;
while (n->next != NULL) while (n->next != NULL)
n = n->next; n = n->next;
Node *tmp = malloc(sizeof(Node)); Unit *tmp = malloc(sizeof(Unit));
memcpy(tmp->name, s, LEN); memcpy(tmp->name, s, LEN);
tmp->prev = n; tmp->prev = n;
n->next = tmp; n->next = tmp;
+19
View File
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 5
#define LEN 5
typedef struct Unit
{
struct Unit *next;
struct Unit *prev;
char name[LEN];
} Unit;
void node_print(Unit *n);
Unit get(Unit *node, int n);
Unit *xor_find(Unit *n1, Unit *n2);
Unit *add_tail(Unit *n, char s[]);
@@ -3,12 +3,12 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
srand(time(0)); srand(time(0));
Node *n1 = NULL; Unit *n1 = NULL;
Node *n2 = NULL; Unit *n2 = NULL;
Node *n3 = NULL; Unit *n3 = NULL;
if ((n1 = malloc(sizeof(Node))) == NULL || if ((n1 = malloc(sizeof(Unit))) == NULL ||
(n2 = malloc(sizeof(Node))) == NULL || (n2 = malloc(sizeof(Unit))) == NULL ||
(n3 = malloc(sizeof(Node))) == NULL) (n3 = malloc(sizeof(Unit))) == NULL)
{ {
printf("Could not malloc\n"); printf("Could not malloc\n");
exit(2); exit(2);
@@ -32,14 +32,15 @@ int main(int argc, char *argv[])
n2 = add_tail(n2, s); n2 = add_tail(n2, s);
} }
printf("Begin arrays:\n"); printf("First array:\n");
node_print(n1); node_print(n1);
printf("\n"); printf("\n");
printf("Second array:\n");
node_print(n2); node_print(n2);
n3 = xor_find(n1, n2); n3 = xor_find(n1, n2);
printf("\nFinal array:\n"); printf("\nThird array:\n");
node_print(n3); node_print(n3);
free(n1); free(n1);
BIN
View File
Binary file not shown.
+38 -29
View File
@@ -1,21 +1,20 @@
#include "head.h" #include "head.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h>
Stack get(Stack *l, int n) { void linked_print(Stack *l)
if (n > len(l)) {
exit(3); while (l != NULL)
int i; {
for (i = 0; i < n; i++) { printf("%lF\n", l->x);
l = l->next; l = l->next;
} }
return *l;
} }
Stack *add_head(Stack *l, double x) { Stack *add_head(Stack *l, double x)
if (l == NULL) { {
if (l == NULL)
{
l = malloc(sizeof(Stack)); l = malloc(sizeof(Stack));
if (l == NULL) if (l == NULL)
exit(1); exit(1);
@@ -23,7 +22,8 @@ Stack *add_head(Stack *l, double x) {
l->next = NULL; l->next = NULL;
} }
if (l->x == 0.0) { if (l->x == 0.0)
{
l->x = x; l->x = x;
l->next = NULL; l->next = NULL;
return l; return l;
@@ -38,26 +38,12 @@ Stack *add_head(Stack *l, double x) {
return tmp; return tmp;
} }
void stack_print(Stack *l) { Stack *del_head(Stack *l)
while (l != NULL) { {
printf("%lF\n", l->x);
l = l->next;
}
}
int len(Stack *l) {
int ans = 0;
while (l != NULL) {
ans++;
l = l->next;
}
return ans;
}
Stack *del_head(Stack *l) {
if (l == NULL) if (l == NULL)
return NULL; return NULL;
else if (l->next == NULL) { else if (l->next == NULL)
{
free(l); free(l);
l = NULL; l = NULL;
return NULL; return NULL;
@@ -67,3 +53,26 @@ Stack *del_head(Stack *l) {
l = NULL; l = NULL;
return tmp; 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;
}
+6 -6
View File
@@ -1,13 +1,13 @@
#define N 5
#define N 6 typedef struct Stack
{
typedef struct Stack {
double x; double x;
struct Stack *next; struct Stack *next;
} Stack; } Stack;
Stack get(Stack *l, int n); void linked_print(Stack *l);
Stack *add_head(Stack *l, double x); Stack *add_head(Stack *l, double x);
void stack_print(Stack *l);
int len(Stack *l);
Stack *del_head(Stack *l); Stack *del_head(Stack *l);
int len(Stack *l);
Stack get(Stack *l, int n);
+23 -14
View File
@@ -1,32 +1,40 @@
#include "head.h" #include "head.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
// Выдает неадекватные значения
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
srand(time(0)); srand(time(0));
Stack *numbers = malloc(sizeof(Stack)); Stack *numbers = malloc(sizeof(Stack));
if (numbers == NULL) if (numbers == NULL)
exit(1); exit(1);
char c; char c;
char flag = 'f'; char flag = 'n';
while ((c = getchar()) != EOF) { while ((c = getchar()) != EOF)
if (c == '\n') { {
if (c == '\n')
{
break; break;
} else if (c == ' ') { }
flag = 't'; else if (c == ' ')
{
flag = 's';
continue; continue;
} }
switch (c) { switch (c)
{
case '0' ... '9': case '0' ... '9':
if (flag == 't') { if (flag == 's')
{
numbers = add_head(numbers, (double)(c - '0')); numbers = add_head(numbers, (double)(c - '0'));
flag = 'f'; flag = 'n';
} else { }
else
{
double a = get(numbers, 0).x; double a = get(numbers, 0).x;
numbers = del_head(numbers); numbers = del_head(numbers);
a = a * 10 + c - '0'; a = a * 10 + c - '0';
@@ -38,7 +46,8 @@ int main(int argc, char *argv[]) {
case '-': case '-':
case '*': case '*':
case '/': case '/':
if (len(numbers) < 2) { if (len(numbers) < 2)
{
printf("Invalid expression\n"); printf("Invalid expression\n");
exit(2); exit(2);
} }
@@ -58,7 +67,7 @@ int main(int argc, char *argv[]) {
} }
} }
printf("result: %lF\n", get(numbers, -1).x); printf("res: %lF\n", get(numbers, 0).x);
free(numbers); free(numbers);
return 0; return 0;
BIN
View File
Binary file not shown.
+82 -52
View File
@@ -2,12 +2,12 @@
#include <stdlib.h> #include <stdlib.h>
#include "head.h" #include "head.h"
tree *init_root(tree *root, int field) // Выделение памяти под корень дереваnode tree *init_root(tree *root, int field) // Выделение памяти под корень дерева
{ {
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
tmp -> field = field;// Присваивание указателю на родителя значения NULL tmp->field = field; // Присваивание указателю на родителя значения NULL
tmp -> parent = NULL; // Присваивание указателю на левое и правое поддерево значения NULL tmp->parent = NULL; // Присваивание указателю на левое и правое поддерево значения NULL
tmp -> left = tmp -> right = NULL; tmp->left = tmp->right = NULL;
root = tmp; root = tmp;
return root; return root;
} }
@@ -16,38 +16,41 @@ tree *add_node(tree *root, int field)
{ {
tree *root2 = root, *root3 = NULL; // Выделение памяти под узел дерева tree *root2 = root, *root3 = NULL; // Выделение памяти под узел дерева
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
tmp -> field = field; tmp->field = field;
while (root2 != NULL) while (root2 != NULL)
{ {
root3 = root2; root3 = root2;
if (field < root2 -> field) if (field < root2->field)
root2 = root2 -> left; root2 = root2->left;
else else
root2 = root2 -> right; root2 = root2->right;
} }
tmp -> parent = root3; // Присваивание указателю на левое и правое поддерево значения NULL tmp->parent = root3; // Присваивание указателю на левое и правое поддерево значения NULL
tmp -> left = NULL; tmp->left = NULL;
tmp -> right = NULL; tmp->right = NULL;
if (field < root3 -> field) root3 -> left = tmp; if (field < root3->field)
else root3 -> right = tmp; root3->left = tmp;
else
root3->right = tmp;
return root; return root;
} }
tree *find_node(tree * root, int field) // Если дерево пусто или ключ корня равен искомому ключу, то возвращается указатель на корень tree *find_node(tree *root, int field) // Если дерево пусто или ключ корня равен искомому ключу, то возвращается указатель на корень
{ {
if ((root == NULL) || (root -> field == field)) if ((root == NULL) || (root->field == field))
return root; // Поиск нужного узла return root; // Поиск нужного узла
if (field < root -> field) if (field < root->field)
return find_node(root -> left, field); return find_node(root->left, field);
else return find_node(root -> right, field); else
return find_node(root->right, field);
} }
// Минимальный элемент дерева // Минимальный элемент дерева
tree *minim(tree *root) tree *minim(tree *root)
{ {
tree *lef = root; tree *lef = root;
while (lef -> left != NULL) while (lef->left != NULL)
lef = lef -> left; lef = lef->left;
return lef; return lef;
} }
@@ -55,73 +58,100 @@ tree *minim(tree *root)
tree *maxim(tree *root) tree *maxim(tree *root)
{ {
tree *rig = root; tree *rig = root;
while (rig -> right != NULL) while (rig->right != NULL)
rig = rig -> right; rig = rig->right;
return rig; return rig;
} }
tree *findForDel(tree *root) tree *findForDel(tree *root)
{ {
tree *rig = root, *lef = NULL; // Если есть правое поддерево, то ищем минимальный элемент в этом поддереве tree *rig = root, *lef = NULL; // Если есть правое поддерево, то ищем минимальный элемент в этом поддереве
if (rig -> right != NULL) if (rig->right != NULL)
return minim(rig -> right); return minim(rig->right);
lef = rig -> parent; lef = rig->parent;
while ((lef != NULL) && (rig == lef -> right)) while ((lef != NULL) && (rig == lef->right))
{ {
rig = lef; rig = lef;
lef = lef -> parent; lef = lef->parent;
} }
return lef; return lef;
} }
tree *del_node(tree *root, int field) tree *del_node(tree *root, int field)
{ {
// Поиск удаляемого узла по ключу // Поиск удаляемого узла по ключу
tree *rig = root, *lef = NULL, *tmp = NULL; tree *rig = root, *lef = NULL, *tmp = NULL;
lef = find_node(root, field); lef = find_node(root, field);
// 1 случай: у узла нет потомков // 1 случай: у узла нет потомков
if ((lef -> left == NULL) && (lef -> right == NULL)) if ((lef->left == NULL) && (lef->right == NULL))
{ {
tmp = lef -> parent; tmp = lef->parent;
if (lef == tmp -> right) tmp -> right = NULL; if (lef == tmp->right)
else tmp -> left = NULL; tmp->right = NULL;
else
tmp->left = NULL;
free(lef); free(lef);
} }
// 2 случай: потомок один - поддерево справа // 2 случай: потомок один - поддерево справа
if ((lef -> left == NULL) && (lef -> right != NULL)) if ((lef->left == NULL) && (lef->right != NULL))
{ {
tmp = lef -> parent; tmp = lef->parent;
if (lef == tmp -> right) tmp -> right = lef -> right; if (lef == tmp->right)
else tmp -> left = lef -> right; tmp->right = lef->right;
else
tmp->left = lef->right;
free(lef); free(lef);
} }
// 2 случай: потомок один - поддерево слева // 2 случай: потомок один - поддерево слева
if ((lef -> left != NULL) && (lef -> right == NULL)) if ((lef->left != NULL) && (lef->right == NULL))
{ {
tmp = lef -> parent; tmp = lef->parent;
if (lef == tmp -> right) tmp -> right = lef -> left; if (lef == tmp->right)
else tmp -> left = lef -> left; tmp->right = lef->left;
else
tmp->left = lef->left;
free(lef); free(lef);
} }
// 3 случай: оба потомка // 3 случай: оба потомка
if ((lef -> left != NULL) && (lef -> right != NULL)) if ((lef->left != NULL) && (lef->right != NULL))
{ {
tmp = findForDel(lef); tmp = findForDel(lef);
lef -> field = tmp -> field; lef->field = tmp->field;
if (tmp -> right == NULL) if (tmp->right == NULL)
tmp -> parent -> left = NULL; tmp->parent->left = NULL;
else tmp -> parent -> left = tmp -> right; else
tmp->parent->left = tmp->right;
free(tmp); free(tmp);
} }
return root; return root;
} }
void print_tree(tree *root) void preorder(tree *root)
{ {
if (root == NULL) if (root == NULL)
return; return;
if (root -> field) if (root -> field)
printf("%d ", root -> field); printf("%d ", root -> field);
print_tree(root -> left); preorder(root -> left);
print_tree(root -> right); preorder(root -> right);
}
void postorder(tree *root)
{
if (root == NULL)
return;
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);
} }
+6 -3
View File
@@ -1,4 +1,5 @@
typedef struct tnode { typedef struct tnode
{
int field; int field;
struct tnode *left; struct tnode *left;
struct tnode *right; struct tnode *right;
@@ -7,9 +8,11 @@ typedef struct tnode {
tree *init_root(tree *root, int field); tree *init_root(tree *root, int field);
tree *add_node(tree *root, int field); tree *add_node(tree *root, int field);
tree *find_node(tree * root, int field); tree *find_node(tree *root, int field);
tree *minim(tree *root); tree *minim(tree *root);
tree *maxim(tree *root); tree *maxim(tree *root);
tree *findForDel(tree *root); tree *findForDel(tree *root);
tree *del_node(tree *root, int field); tree *del_node(tree *root, int field);
void print_tree(tree *root); void preorder(tree *root);
void inorder(tree *root);
void postorder(tree *root);
+24 -18
View File
@@ -1,25 +1,31 @@
#include <stdio.h>
#include <stdio.h>
#include "head.h" #include "head.h"
#include <stdio.h>
int main(int argc, char *argv[]){ #include <stdlib.h>
tree *root=NULL; #include <string.h>
root = init_root(root, 65); // Дерево улетает в SF
print_tree(root); int main(int argc, char *argv[])
{
tree *root = NULL;
root = init_root(root, 'A');
preorder(root);
printf("\n"); printf("\n");
root=del_node(root, 65); root = del_node(root, 'A');
print_tree(root); preorder(root);
printf("\n"); printf("\n");
root = add_node(root, 70); root = add_node(root, 'F');
root = add_node(root, 66); root = add_node(root, 'B');
root = add_node(root, 68); root = add_node(root, 'D');
root = add_node(root, 71); root = add_node(root, 'G');
root = add_node(root, 67); root = add_node(root, 'C');
root = add_node(root, 72); root = add_node(root, 'H');
print_tree(root); preorder(root);
printf("\n"); printf("\n");
root = del_node(root, 66); postorder(root);
print_tree(root); printf("\n");
inorder(root);
printf("\n");
root = del_node(root, 'B');
preorder(root);
printf("\n"); printf("\n");
return 0; return 0;
} }
Binary file not shown.
-18
View File
@@ -1,18 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 3
#define LEN 3
typedef struct Node {
struct Node* next;
struct Node* prev;
char name[LEN];
} Node;
void node_print(Node* n);
Node get(Node* node, int n);
Node* xor_find(Node* n1, Node* n2);
Node* add_tail(Node* n, char s[]);