add Vova`s list

This commit is contained in:
ada-dmitry
2023-05-09 17:49:42 +03:00
parent 31a9fdc3f7
commit 875630a713
7 changed files with 213 additions and 9 deletions
Executable
BIN
View File
Binary file not shown.
+7 -7
View File
@@ -62,13 +62,13 @@ tree *maxim(tree *root)
tree *findForDel(tree *root) tree *findForDel(tree *root)
{ {
tree *p = root, *lef = NULL; // Если есть правое поддерево, то ищем минимальный элемент в этом поддереве tree *rig = root, *lef = NULL; // Если есть правое поддерево, то ищем минимальный элемент в этом поддереве
if (p -> right != NULL) if (rig -> right != NULL)
return minim(p -> right); return minim(rig -> right);
lef = p -> parent; lef = rig -> parent;
while ((lef != NULL) && (p == lef -> right)) while ((lef != NULL) && (rig == lef -> right))
{ {
p = lef; rig = lef;
lef = lef -> parent; lef = lef -> parent;
} }
return lef; return lef;
@@ -77,7 +77,7 @@ tree *findForDel(tree *root)
tree *del_node(tree *root, int field) tree *del_node(tree *root, int field)
{ {
// Поиск удаляемого узла по ключу // Поиск удаляемого узла по ключу
tree *p = 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))
+20 -2
View File
@@ -2,6 +2,24 @@
#include <stdio.h> #include <stdio.h>
#include "head.h" #include "head.h"
int main(){ int main(int argc, char *argv[]){
return 126; tree *root=NULL;
root = init_root(root, 65);
print_tree(root);
printf("\n");
root=del_node(root, 65);
print_tree(root);
printf("\n");
root = add_node(root, 70);
root = add_node(root, 66);
root = add_node(root, 68);
root = add_node(root, 71);
root = add_node(root, 67);
root = add_node(root, 72);
print_tree(root);
printf("\n");
root = del_node(root, 66);
print_tree(root);
printf("\n");
return 0;
} }
BIN
View File
Binary file not shown.
+60
View File
@@ -0,0 +1,60 @@
#include "head.h"
int main(int argc, char *argv[])
{
printf("Hello! Let's begin\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("No memory for you\n");
exit(2);
}
n1 = add_tail(n1, "tail");
printf("First error\n");
int i;
for (i = 0; i < CO; i++)
{
char s[LEN];
int m;
printf("Let it be\n");
for (m = 0; m < LEN - 1; m++)
s[m] = 'V' + rand() % 2;
s[LEN - 1] = '\0';
n1 = add_tail(n1, s);
printf("10");
for (m = 0; m < LEN - 1; m++)
{
s[m] = 'V' + rand() % 2;
printf("20");
}
s[LEN - 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 version:\n");
node_print(n3);
free(n1);
free(n2);
free(n3);
printf("That's all for now\n");
return 0;
}
+106
View File
@@ -0,0 +1,106 @@
#include "head.h"
void node_print(Node *n)
{
while (n != NULL)
{
printf("%s\n", n->name);
n = n->ne;
}
}
int len(Node *n)
{
int f = 0;
while (n != NULL)
{
f++;
n = n->ne;
}
return f;
}
Node *add_tail(Node *n, char s[])
{
if (n == NULL)
{
if ((n = malloc(sizeof(Node))) == NULL)
{
printf("No memory for you\n");
exit(1);
}
n->ne = NULL;
n->prv = NULL;
memcpy(n->name, s, LEN);
return n;
}
if (!strcmp(n->name, "\0"))
{
memcpy(n->name, s, LEN);
n->ne = NULL;
n->prv = NULL;
return n;
}
Node *res = n;
while (n->ne != NULL)
{
printf("%p\n", n);
n = n->ne;
}
Node *tmp = malloc(sizeof(Node));
memcpy(tmp->name, s, LEN);
tmp->prv = n;
n->ne = tmp;
return res;
}
Node get(Node *node, int n)
{
int k;
for (k = 0; k < n; k++)
{
node = node->ne;
}
return *node;
}
Node *find_0(Node *n1, Node *n2)
{
int i, j, f = 0;
Node *res = NULL;
if ((res = malloc(sizeof(Node))) == NULL)
{
printf("No memory for you\n");
exit(3);
}
for (i = 0; i < len(n1); i++)
{
for (j = 0; j < len(n2); j++)
{
if (strcmp(get(n1, i).name, get(n2, j).name) == 0)
{
f = 1;
}
}
if (f == 0)
res = add_tail(res, get(n1, i).name);
f = 0;
}
for (i = 0; i < len(n2); i++)
{
for (j = 0; j < len(n1); j++)
{
if (strcmp(get(n2, i).name, get(n1, j).name) == 0)
{
f = 1;
}
}
if (f == 0)
res = add_tail(res, get(n2, i).name);
f = 0;
}
return res;
}
+20
View File
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define CO 3
#define LEN 3
typedef struct Node
{
char name[LEN];
struct Node *ne;
struct Node *prv;
} Node;
void node_print(Node *n);
Node *add_tail(Node *n, char s[]);
Node get(Node *node, int n);
Node *find_0(Node *n1, Node *n2);
int len(Node *n);