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