This commit is contained in:
ada.dmitry
2023-05-17 14:51:46 +03:00
parent dc4a1158f6
commit 68f659165b
11 changed files with 128 additions and 92 deletions
BIN
View File
Binary file not shown.
+1 -3
View File
@@ -8,7 +8,7 @@ int main()
if (professions1 == NULL || professions2 == NULL) if (professions1 == NULL || professions2 == NULL)
{ {
printf("Mesta net\n"); printf("Mesta net\n");
exit(1); exit(1);
} }
@@ -59,8 +59,6 @@ int main()
st_fscanf(professions3, line_number, "professions.txt"); st_fscanf(professions3, line_number, "professions.txt");
sort_sel(professions3, line_number); sort_sel(professions3, line_number);
Sleep(50000);
printf("\nTTretiy massiv is:\n"); printf("\nTTretiy massiv is:\n");
print_struc(professions3, line_number); print_struc(professions3, line_number);
free(professions1); free(professions1);
-1
View File
@@ -5,7 +5,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <windows.h>
typedef struct People typedef struct People
{ {
+8 -8
View File
@@ -1,8 +1,8 @@
1 pkaeoew 1 fkmigss
1 ujqrkpqP 3 btuawlx
2 nllvevc= 3 kakvuam
2 wrqqkwf( 3 roiiaox
1 krxtlgme 1 kufhggv
1 srgyisre 2 qaasoax
3 tiflqbd( 3 rummwyo
3 opukfbci 3 qkvwdqj
Binary file not shown.
Binary file not shown.
Binary file not shown.
+21 -12
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) tree *init_root(tree *root, int field)
{ {
tree *tmp = malloc(sizeof(tree)); // Выделение памяти под корень дерева tree *tmp = malloc(sizeof(tree)); // Выделение памяти под корень дерева
tmp->field = field; // Присваивание значения ключу tmp->field = field; // Присваивание значения ключу
tmp->parent = NULL; // Присваивание указателю на родителя значения NULL tmp->parent = NULL; // Присваивание указателю на родителя значения NULL
tmp->left = tmp->right = NULL; // Присваивание указателю на левое и правое поддерево значения NULL tmp->left = tmp->right = NULL; // Присваивание указателю на левое и правое поддерево значения NULL
root = tmp; root = tmp;
return root; return root;
} }
@@ -79,46 +79,55 @@ tree *findForDel(tree *root)
tree *del_node(tree *root, int field) tree *del_node(tree *root, int field)
{ {
printf("%d\n", root->field); if (root == NULL)
return NULL;
// Поиск удаляемого узла по ключу // Поиск удаляемого узла по ключу
tree *rig = root, *lef = NULL, *tmp = NULL; tree *rig = root, *lef = NULL, *tmp = NULL;
// preorder(root);
printf("test");
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; printf("no children\n");
if (lef == tmp->right) if (lef == lef->parent->right)
tmp->right = NULL; lef->parent->right = NULL;
else else
tmp->left = NULL; lef->parent->left = NULL;
free(lef); free(lef);
return root;
} }
// 2 случай: потомок один - поддерево справа // 2 случай: потомок один - поддерево справа
if ((lef->left == NULL) && (lef->right != NULL)) if ((lef->left == NULL) && (lef->right != NULL))
{ {
tmp = lef->parent; tmp = lef->parent;
printf("left\n");
if (lef == tmp->right) if (lef == tmp->right)
{
tmp->right = lef->right; tmp->right = lef->right;
}
else else
tmp->left = lef->right; tmp->left = lef->right;
free(lef); free(lef);
return tmp;
} }
// 2 случай: потомок один - поддерево слева // 2 случай: потомок один - поддерево слева
if ((lef->left != NULL) && (lef->right == NULL)) if ((lef->left != NULL) && (lef->right == NULL))
{ {
printf("right\n");
tmp = lef->parent; tmp = lef->parent;
if (lef == tmp->right) if (lef == tmp->right)
tmp->right = lef->left; tmp->right = lef->left;
else else
tmp->left = lef->left; tmp->left = lef->left;
free(lef); free(lef);
return tmp;
} }
// 3 случай: оба потомка // 3 случай: оба потомка
if ((lef->left != NULL) && (lef->right != NULL)) if ((lef->left != NULL) && (lef->right != NULL))
{ {
printf("both\n");
tmp = findForDel(lef); tmp = findForDel(lef);
lef->field = tmp->field; lef->field = tmp->field;
if (tmp->right == NULL) if (tmp->right == NULL)
@@ -135,7 +144,7 @@ void preorder(tree *root)
if (root == NULL) if (root == NULL)
return; return;
if (root->field) if (root->field)
printf("%d ", root->field); printf("%d\n", root->field);
preorder(root->left); preorder(root->left);
preorder(root->right); preorder(root->right);
} }
+7 -1
View File
@@ -7,8 +7,14 @@ int main(int argc, char *argv[])
{ {
tree *root = NULL; tree *root = NULL;
root = init_root(root, 'A'); root = init_root(root, 'A');
// preorder(root); preorder(root);
root = add_node(root, 'r');
preorder(root);
root = del_node(root, 'A'); root = del_node(root, 'A');
preorder(root);
exit(0); exit(0);
// preorder(root); // preorder(root);
printf("\n"); printf("\n");
+91 -67
View File
@@ -6,22 +6,30 @@
#define N 3 #define N 3
#define LENG 3 #define LENG 3
typedef struct Node { typedef struct Node
struct Node* next; {
struct Node* pr; struct Node *next;
char name[LENG]; struct Node *pr;
char name[LENG];
} Node; } Node;
void node_print(Node *n) { void node_print(Node *n)
while (n != NULL) { {
while (n != NULL)
{
printf("%s\n", n->name); printf("%s\n", n->name);
n = n->next; n = n->next;
} }
} }
Node *add_tail(Node *n, char s[]) { Node *add_tail(Node *n, char s[])
if (n == NULL) { {
if ((n = malloc(sizeof(Node))) == NULL) { Node *res = n;
Node *tmp = malloc(sizeof(Node));
if (n == NULL)
{
if ((n = malloc(sizeof(Node))) == NULL)
{
printf("Deny malloc\n"); printf("Deny malloc\n");
exit(1); exit(1);
} }
@@ -30,73 +38,86 @@ Node *add_tail(Node *n, char s[]) {
memcpy(n->name, s, LENG); memcpy(n->name, s, LENG);
return n; return n;
} }
if (!strcmp(n->name, "\0")) { if (!strcmp(n->name, "\0"))
{
memcpy(n->name, s, LENG); memcpy(n->name, s, LENG);
n->next = NULL; n->next = NULL;
n->pr = NULL; n->pr = NULL;
return n; return n;
} }
Node *res = n;
while (n->next != NULL) while (n->next != NULL)
n = n->next; n = n->next;
Node *tmp = malloc(sizeof(Node));
memcpy(tmp->name, s, LENG); memcpy(tmp->name, s, LENG);
tmp->pr = n; tmp->pr = n;
n->next = tmp; n->next = tmp;
return res; return res;
} }
Node get(Node *node, int n) { Node get(Node *node, int n)
{
int i; int i;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++)
{
node = node->next; node = node->next;
} }
return *node; return *node;
} }
int len(Node *n) { int len(Node *n)
{
int res = 0; int res = 0;
while (n != NULL) { while (n != NULL)
{
res++; res++;
n = n->next; n = n->next;
} }
return res; return res;
} }
Node *find_0(Node *n1, Node *n2) { Node *find_0(Node *n1, Node *n2)
{
int i, j, f = 0; int i, j, f = 0;
Node *res = NULL; Node *res = NULL;
if ((res = malloc(sizeof(Node))) == NULL) { if ((res = malloc(sizeof(Node))) == NULL)
{
printf("Deny malloc\n"); printf("Deny malloc\n");
exit(3); exit(3);
} }
for (i = 0; i < len(n1); i++) { for (i = 0; i < len(n1); i++)
{
Node tmp1 = get(n1, i); Node tmp1 = get(n1, i);
for (j = 0; j < len(n2); j++) { for (j = 0; j < len(n2); j++)
{
Node tmp2 = get(n2, j); Node tmp2 = get(n2, j);
if (strcmp(tmp1.name, tmp2.name) == 0) { if (strcmp(tmp1.name, tmp2.name) == 0)
{
f = 1; f = 1;
} }
} }
if (f == 0) { if (f == 0)
{
Node tmp = get(n1, i); Node tmp = get(n1, i);
res = add_tail(res, tmp.name); res = add_tail(res, tmp.name);
} }
f = 0; f = 0;
} }
for (i = 0; i < len(n2); i++) { for (i = 0; i < len(n2); i++)
{
Node tmp2 = get(n2, i); Node tmp2 = get(n2, i);
for (j = 0; j < len(n1); j++) { for (j = 0; j < len(n1); j++)
{
Node tmp1 = get(n1, j); Node tmp1 = get(n1, j);
if (strcmp(tmp2.name, tmp1.name) == 0) { if (strcmp(tmp2.name, tmp1.name) == 0)
{
f = 1; f = 1;
} }
} }
if (f == 0) { if (f == 0)
{
Node tmp = get(n2, i); Node tmp = get(n2, i);
res = add_tail(res, tmp.name); res = add_tail(res, tmp.name);
} }
@@ -106,55 +127,58 @@ Node *find_0(Node *n1, Node *n2) {
return res; return res;
} }
int main()
{
Node *n1 = NULL;
Node *n2 = NULL;
Node *n3 = NULL;
int i;
printf("Hello. Let's begin\n");
printf("\n");
srand(time(0));
int main(int argc, char* argv[]) { if ((n1 = malloc(sizeof(Node))) == NULL ||
printf("Hello. Let's begin\n"); (n2 = malloc(sizeof(Node))) == NULL ||
printf("\n"); (n3 = malloc(sizeof(Node))) == NULL)
srand(time(0)); {
Node* n1 = NULL; printf("Deny malloc\n");
Node* n2 = NULL; exit(2);
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++)
for (i = 0; i < N; i++) { {
char s[LENG]; char s[LENG];
int j; int j;
for (j = 0; j < LENG - 1; j++) for (j = 0; j < LENG - 1; j++)
s[j] = 'a' + rand() % 2; s[j] = 'a' + rand() % 2;
s[LENG - 1] = '\0'; s[LENG - 1] = '\0';
n1 = add_tail(n1, s); n1 = add_tail(n1, s);
for (j = 0; j < LENG - 1; j++) for (j = 0; j < LENG - 1; j++)
s[j]= 'a' + rand() % 2; s[j] = 'a' + rand() % 2;
s[LENG - 1] = '\0'; s[LENG - 1] = '\0';
n2 = add_tail(n2, s); n2 = add_tail(n2, s);
} }
printf("Initial arrays:\n"); printf("Initial arrays:\n");
node_print(n1); node_print(n1);
printf("\n"); printf("\n");
node_print(n2); node_print(n2);
n3 = find_0(n1, n2); n3 = find_0(n1, n2);
printf("\n"); printf("\n");
printf("Final array:\n"); printf("Final array:\n");
node_print(n3); node_print(n3);
free(n1); free(n1);
free(n2); free(n2);
free(n3); free(n3);
printf("\n");
printf("That's all for now\n");
return 0; printf("\n");
printf("That's all for now\n");
return 0;
} }
BIN
View File
Binary file not shown.