restruct 1

This commit is contained in:
ada-dmitry
2023-09-11 17:15:34 +03:00
parent 096d8dea1a
commit 6809e2ef5f
67 changed files with 0 additions and 0 deletions
BIN
View File
Binary file not shown.
+115
View File
@@ -0,0 +1,115 @@
#include "head.h"
void node_print(Unit *n)
{
while (n != NULL)
{
printf("%s\n", n->name);
n = n->next;
}
}
Unit get(Unit *node, int n)
{
int i;
for (i = 0; i < n; i++)
{
node = node->next;
}
return *node;
}
int len(Unit *n)
{
int res = 0;
while (n != NULL)
{
res++;
n = n->next;
}
return res;
}
Unit *xor_find(Unit *n1, Unit *n2)
{
int i, j, f = 0;
Unit *res = NULL;
if ((res = malloc(sizeof(Unit))) == NULL)
{
printf("could not malloc\n");
exit(3);
}
for (i = 0; i < len(n1); i++)
{
Unit tmp1 = get(n1, i);
for (j = 0; j < len(n2); j++)
{
Unit tmp2 = get(n2, j);
if (strcmp(tmp1.name, tmp2.name) == 0)
{
f = 1;
}
}
if (f == 0)
{
Unit tmp = get(n1, i);
res = add_tail(res, tmp.name);
}
f = 0;
}
for (i = 0; i < len(n2); i++)
{
Unit tmp2 = get(n2, i);
for (j = 0; j < len(n1); j++)
{
Unit tmp1 = get(n1, j);
if (strcmp(tmp2.name, tmp1.name) == 0)
{
f = 1;
}
}
if (f == 0)
{
Unit tmp = get(n2, i);
res = add_tail(res, tmp.name);
}
f = 0;
}
return res;
}
Unit *add_tail(Unit *n, char s[])
{
if (n == NULL)
{
if ((n = malloc(sizeof(Unit))) == NULL)
{
printf("could not malloc\n");
exit(1);
}
n->next = NULL;
n->prev = NULL;
memcpy(n->name, s, LEN);
return n;
}
if (!strcmp(n->name, "\0"))
{
memcpy(n->name, s, LEN);
n->next = NULL;
n->prev = NULL;
return n;
}
Unit *res = n;
while (n->next != NULL)
n = n->next;
Unit *tmp = malloc(sizeof(Unit));
memcpy(tmp->name, s, LEN);
tmp->prev = n;
n->next = tmp;
return res;
}
+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[]);
+51
View File
@@ -0,0 +1,51 @@
#include "head.h"
int main(int argc, char *argv[])
{
srand(time(0));
Unit *n1 = NULL;
Unit *n2 = NULL;
Unit *n3 = NULL;
if ((n1 = malloc(sizeof(Unit))) == NULL ||
(n2 = malloc(sizeof(Unit))) == NULL ||
(n3 = malloc(sizeof(Unit))) == NULL)
{
printf("Could not malloc\n");
exit(2);
}
int i;
for (i = 0; i < N; i++)
{
char s[LEN];
int j;
for (j = 0; j < LEN - 1; j++)
s[j] = 'a' + rand() % 2;
s[LEN - 1] = '\0';
n1 = add_tail(n1, s);
for (j = 0; j < LEN - 1; j++)
s[j] = 'a' + rand() % 2;
s[LEN - 1] = '\0';
n2 = add_tail(n2, s);
}
printf("First array:\n");
node_print(n1);
printf("\n");
printf("Second array:\n");
node_print(n2);
n3 = xor_find(n1, n2);
printf("\nThird array:\n");
node_print(n3);
free(n1);
free(n2);
free(n3);
return 0;
}