This commit is contained in:
ada.dmitry
2023-05-16 12:35:39 +03:00
parent 58a2573b52
commit dc4a1158f6
9 changed files with 192 additions and 81 deletions
BIN
View File
Binary file not shown.
+2 -32
View File
@@ -2,7 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
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;
}
-2
View File
@@ -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);
+6 -27
View File
@@ -2,16 +2,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Выдает неадекватные значения
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;
Binary file not shown.
+21 -17
View File
@@ -2,12 +2,12 @@
#include <stdlib.h>
#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);
}
+3 -3
View File
@@ -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');
+160
View File
@@ -0,0 +1,160 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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;
}
Executable
BIN
View File
Binary file not shown.