mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 01:00:15 +00:00
restruct 1
This commit is contained in:
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,68 @@
|
||||
#include "funcHeader.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
srand(time(0));
|
||||
People *professions1 = malloc(sizeof(People) * COUN);
|
||||
People *professions2 = malloc(sizeof(People) * COUN);
|
||||
|
||||
if (professions1 == NULL || professions2 == NULL)
|
||||
{
|
||||
printf("Mesta net\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
st_rand(professions1, COUN);
|
||||
st_rand(professions2, COUN);
|
||||
|
||||
printf("Perviy massiv:\n");
|
||||
print_struc(professions1, COUN);
|
||||
printf("\nVtoroi massiv:\n");
|
||||
print_struc(professions2, COUN);
|
||||
|
||||
sort_sel(professions1, COUN);
|
||||
sort_sel(professions2, COUN);
|
||||
|
||||
printf("\nPerviy massiv posle sort:\n");
|
||||
print_struc(professions1, COUN);
|
||||
printf("\nVtoroi massiv posle sort:\n");
|
||||
print_struc(professions2, COUN);
|
||||
|
||||
People *ans = binSearch(professions1, COUN, 15);
|
||||
|
||||
if (ans == NULL)
|
||||
printf("\nPeoples net\n");
|
||||
else
|
||||
printf("\nLength: %2d; name: %s\n", ans->count, ans->name);
|
||||
|
||||
printf("\nElement in perviy massiv: ");
|
||||
pst(professions1, COUN, (int)(COUN * 0.7));
|
||||
printf("Element in vtoroi massiv: ");
|
||||
pst(professions2, COUN, (int)(COUN * 0.3));
|
||||
|
||||
printf("\nNumber of para : %d\n", find_para(professions1, COUN, professions2, COUN));
|
||||
|
||||
st_fprintf(professions1, COUN, "professions.txt", "w");
|
||||
st_fprintf(professions2, COUN, "professions.txt", "a");
|
||||
|
||||
int line_number = lines_numf("professions.txt");
|
||||
printf("\nNumber of lines in professions.txt is %d\n", line_number);
|
||||
|
||||
People *professions3 = malloc(sizeof(People) * line_number);
|
||||
|
||||
if (professions3 == NULL)
|
||||
{
|
||||
printf("Mesta net\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
st_fscanf(professions3, line_number, "professions.txt");
|
||||
sort_sel(professions3, line_number);
|
||||
|
||||
printf("\nTTretiy massiv is:\n");
|
||||
print_struc(professions3, line_number);
|
||||
free(professions1);
|
||||
free(professions2);
|
||||
free(professions3);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#define COUN 4
|
||||
#define LEN 8
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct People
|
||||
{
|
||||
int count;
|
||||
char name[LEN];
|
||||
} People;
|
||||
|
||||
void print_struc(People professions[], int n);
|
||||
void sort_sel(People professions[], int n);
|
||||
void st_rand(People professions[], int n);
|
||||
People *binSearch(People professions[], int n, int k);
|
||||
void pst(People professions[], int s, int n);
|
||||
int find_para(People t1[], int n1, People t2[], int n2);
|
||||
void st_fprintf(People professions[], int n, char path[], char access[]);
|
||||
int lines_numf(char path[]);
|
||||
void st_fscanf(People t[], int n, char path[]);
|
||||
void swap(People *t1, People *t2);
|
||||
@@ -0,0 +1,149 @@
|
||||
#include "funcHeader.h"
|
||||
|
||||
void print_struc(People professions[], int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("count: %3d; name: %s\n", professions[i].count, professions[i].name);
|
||||
}
|
||||
|
||||
void sort_sel(People professions[], int n)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < n - 1; i++)
|
||||
{
|
||||
for (j = i + 1; j < n; j++)
|
||||
{
|
||||
if (professions[j].count < professions[i].count)
|
||||
swap(professions + i, professions + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void st_rand(People professions[], int n)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
professions[i].count = rand() % 3 + 1;
|
||||
for (j = 0; j < LEN - 1; j++)
|
||||
professions[i].name[j] = 'a' + rand() % 25;
|
||||
professions[i].name[j + 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
People *binSearch(People professions[], int n, int k)
|
||||
{
|
||||
People *ans = NULL;
|
||||
int right = 0, left = n - 1;
|
||||
|
||||
while (right <= left)
|
||||
{
|
||||
int mid = (left + right) / 2;
|
||||
|
||||
if (professions[mid].count == k)
|
||||
{
|
||||
ans = &professions[mid];
|
||||
break;
|
||||
}
|
||||
right = (professions[mid].count < k) ? mid + 1 : right;
|
||||
left = (professions[mid].count > k) ? mid - 1 : left;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
void pst(People professions[], int s, int n)
|
||||
{
|
||||
if (n >= s)
|
||||
{
|
||||
printf("Out of range\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
printf("count: %2d; name: %s\n", professions[n].count, professions[n].name);
|
||||
}
|
||||
|
||||
int find_para(People t1[], int n1, People t2[], int n2)
|
||||
{
|
||||
int ans = 0, i;
|
||||
for (i = 0; i < n1; i++)
|
||||
{
|
||||
People *tans = binSearch(t2, n2, t1[i].count);
|
||||
if (tans == NULL)
|
||||
continue;
|
||||
ans++;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
void st_fprintf(People professions[], int n, char path[], char access[])
|
||||
{
|
||||
int i;
|
||||
FILE *f = fopen(path, access);
|
||||
if (f == NULL)
|
||||
{
|
||||
printf("Could not open file %s\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
fprintf(f, "%d %s\n", professions[i].count, professions[i].name);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
int lines_numf(char path[])
|
||||
{
|
||||
int ans = 0;
|
||||
char c;
|
||||
FILE *f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
{
|
||||
printf("Faila %s net...\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
while ((c = fgetc(f)) != EOF)
|
||||
{
|
||||
if (c == '\n')
|
||||
ans++;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return ans;
|
||||
}
|
||||
|
||||
void st_fscanf(People t[], int n, char path[])
|
||||
{
|
||||
int i;
|
||||
FILE *f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
{
|
||||
printf("Faila %s net...\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
fscanf(f, "%d %s\n", &t[i].count, t[i].name);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
void swap(People *t1, People *t2)
|
||||
{
|
||||
People *tmp = malloc(sizeof(People));
|
||||
|
||||
if (tmp == NULL)
|
||||
{
|
||||
printf("could not malloc\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(tmp, t1, sizeof(People));
|
||||
memcpy(t1, t2, sizeof(People));
|
||||
memcpy(t2, tmp, sizeof(People));
|
||||
free(tmp);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
1 fkmigss
|
||||
3 btuawlx
|
||||
3 kakvuam
|
||||
3 roiiaox
|
||||
1 kufhggv
|
||||
2 qaasoax
|
||||
3 rummwyo
|
||||
3 qkvwdqj
|
||||
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,212 @@
|
||||
|
||||
#include "prot.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
#define max(x, y) (((x) > (y)) ? (x) : (y))
|
||||
|
||||
void pr(node *tmp)
|
||||
{
|
||||
if (tmp == NULL)
|
||||
{
|
||||
printf("[%p]\n", tmp);
|
||||
return;
|
||||
}
|
||||
while (tmp)
|
||||
{
|
||||
printf("[%14p]->[%14p] ", tmp, tmp->next);
|
||||
printf("size: [%4d]\n", tmp->size);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
printf("-------------\n");
|
||||
}
|
||||
node *add_head(node *head, int hh)
|
||||
{
|
||||
node *tmp = NULL;
|
||||
if ((tmp = malloc(sizeof(node))) == NULL)
|
||||
{
|
||||
perror("malloc: NULL");
|
||||
exit(2);
|
||||
}
|
||||
tmp->size = hh;
|
||||
if (head == NULL)
|
||||
tmp->next = NULL;
|
||||
else
|
||||
{
|
||||
tmp->next = head;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
node *del_head(node *head)
|
||||
{
|
||||
node *tmp = head;
|
||||
if (head == NULL)
|
||||
return NULL;
|
||||
if (head->next == NULL)
|
||||
{
|
||||
free(head);
|
||||
return NULL;
|
||||
}
|
||||
head = head->next;
|
||||
free(tmp);
|
||||
return head;
|
||||
}
|
||||
|
||||
int length(node *list)
|
||||
{
|
||||
node *tmp = list;
|
||||
int res = 0;
|
||||
if (list == NULL || list->size == 0)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
while (tmp != NULL)
|
||||
{
|
||||
res++;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void swap(node *a, node *b)
|
||||
{
|
||||
node *tmp = NULL;
|
||||
if ((tmp = malloc(sizeof(node))) == NULL)
|
||||
exit(1);
|
||||
tmp->size = a->size;
|
||||
a->size = b->size;
|
||||
b->size = tmp->size;
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
node *get(node *list, int n)
|
||||
{
|
||||
if (n >= length(list))
|
||||
exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
list = list->next;
|
||||
return list;
|
||||
}
|
||||
|
||||
node *sort(node *head)
|
||||
{
|
||||
if (head == NULL || head->next == NULL)
|
||||
return head;
|
||||
int i, j, n = length(head);
|
||||
for (i = 0; i < n - 1; i++)
|
||||
{
|
||||
for (j = i + 1; j < n; j++)
|
||||
{
|
||||
if (get(head, j)->size < get(head, i)->size)
|
||||
swap(get(head, i), get(head, j));
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
pointer *add_head_address(pointer *head, node **address)
|
||||
{
|
||||
pointer *tmp = NULL;
|
||||
if ((tmp = malloc(sizeof(pointer))) == NULL)
|
||||
{
|
||||
perror("malloc: NULL");
|
||||
exit(2);
|
||||
}
|
||||
tmp->address = address;
|
||||
if (head == NULL)
|
||||
tmp->next = NULL;
|
||||
else
|
||||
{
|
||||
tmp->next = head;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
node *printextra(node *head, int len, int i)
|
||||
{
|
||||
if (len < i || len == 0)
|
||||
{
|
||||
for (i = 0; i < T - 1; i++)
|
||||
printf(" ");
|
||||
printf("|");
|
||||
return head;
|
||||
}
|
||||
else
|
||||
{
|
||||
disk(head->size);
|
||||
printf("|");
|
||||
return head->next;
|
||||
}
|
||||
}
|
||||
|
||||
void prcount(char s, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%c", s);
|
||||
}
|
||||
}
|
||||
void disk(int size)
|
||||
{
|
||||
int i;
|
||||
prcount(' ', T / 2 - size);
|
||||
for (i = 0; i < size - 1; i++)
|
||||
{
|
||||
printf("* ");
|
||||
}
|
||||
printf("*");
|
||||
prcount(' ', T / 2 - size);
|
||||
}
|
||||
void printftof(pointer *adresses)
|
||||
{
|
||||
node *head1 = *(adresses->address), *head2 = *(adresses->next->address), *head3 = *(adresses->next->next->address);
|
||||
int a = length(head1), b = length(head2), c = length(head3), i; // maximum
|
||||
// maximum = max(max(a, b), c);
|
||||
for (i = T / 2; i > 0; i--)
|
||||
{
|
||||
head1 = printextra(head1, a, i);
|
||||
head2 = printextra(head2, b, i);
|
||||
head3 = printextra(head3, c, i);
|
||||
printf("\n");
|
||||
}
|
||||
prcount('-', 3 * T);
|
||||
printf("\n");
|
||||
printf("Tower 1");
|
||||
prcount(' ', T - 8);
|
||||
printf("|Tower 2");
|
||||
prcount(' ', T - 8);
|
||||
printf("|Tower 3");
|
||||
prcount(' ', T - 8);
|
||||
printf("|\n");
|
||||
prcount('-', 3 * T);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, pointer *adresses)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
printf("No linked list");
|
||||
exit(6);
|
||||
}
|
||||
else if (n == 1)
|
||||
{
|
||||
if (length(*head_to) == 0)
|
||||
printftof(adresses);
|
||||
*head_to = add_head(*head_to, (*head_from)->size);
|
||||
*head_from = del_head(*head_from);
|
||||
if (length(*head_from) == 0)
|
||||
printftof(adresses);
|
||||
return;
|
||||
}
|
||||
hanoi_tower(n - 1, head_from, head_aux, head_to, adresses);
|
||||
printftof(adresses);
|
||||
*head_to = add_head(*head_to, (*head_from)->size);
|
||||
*head_from = del_head(*head_from);
|
||||
printftof(adresses);
|
||||
hanoi_tower(n - 1, head_aux, head_to, head_from, adresses);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "prot.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
node *head = NULL, *head2 = NULL, *head3 = NULL;
|
||||
pointer *addresses = NULL;
|
||||
int r;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
printf("Generated linked list (method of head)\n");
|
||||
/*while ((r=(rand()%T)) < T/2){
|
||||
if (r==0) r+=1;
|
||||
head = add_head(head, r);
|
||||
}*/
|
||||
for (r = 1; r <= T / 2; r++)
|
||||
{
|
||||
head = add_head(head, r);
|
||||
}
|
||||
pr(head);
|
||||
printf("Length of linked list is: %d\n", length(head));
|
||||
printf("Sorted linked list\n");
|
||||
sort(head);
|
||||
pr(head);
|
||||
addresses = add_head_address(addresses, &head3);
|
||||
addresses = add_head_address(addresses, &head2);
|
||||
addresses = add_head_address(addresses, &head);
|
||||
hanoi_tower(length(head), &head, &head3, &head2, addresses);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#define N 10
|
||||
#define T 20
|
||||
|
||||
typedef struct Node
|
||||
{
|
||||
int size;
|
||||
struct Node *next;
|
||||
} node;
|
||||
typedef struct nodepointer
|
||||
{
|
||||
struct nodepointer *next;
|
||||
struct Node **address;
|
||||
} pointer;
|
||||
|
||||
void pr(node *tmp);
|
||||
node *add_head(node *head, int hh);
|
||||
node *del_head(node *head);
|
||||
pointer *add_head_address(pointer *head, node **address);
|
||||
int length(node *list);
|
||||
node *sort(node *head);
|
||||
node *get(node *list, int n);
|
||||
void swap(node *a, node *b);
|
||||
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, pointer *adresses);
|
||||
void printftof(pointer *adresses);
|
||||
node *printextra(node *head, int len, int i);
|
||||
void disk(int size);
|
||||
void prcount(char s, int n);
|
||||
Executable
BIN
Binary file not shown.
@@ -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;
|
||||
}
|
||||
@@ -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[]);
|
||||
@@ -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;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,48 @@
|
||||
#include "head.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void linked_print(Stack *l) // Вывод стека
|
||||
{
|
||||
while (l != NULL)
|
||||
{
|
||||
printf("%lF\n", l->x);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
Stack *add_head(Stack *l, double x) // Добавление головы, причем в двух вариациях(если голова изначально есть и если её нет)
|
||||
{
|
||||
if (l == NULL)
|
||||
{
|
||||
l = malloc(sizeof(Stack));
|
||||
if (l == NULL)
|
||||
exit(1);
|
||||
l->x = x;
|
||||
l->next = NULL;
|
||||
}
|
||||
|
||||
Stack *tmp = malloc(sizeof(Stack));
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
tmp->x = x;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
Stack *del_head(Stack *l)
|
||||
{
|
||||
if (l == NULL)
|
||||
return NULL;
|
||||
else if (l->next == NULL)
|
||||
{
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
}
|
||||
Stack *tmp = l->next;
|
||||
free(l);
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#define N 5
|
||||
|
||||
typedef struct Stack
|
||||
{
|
||||
double x;
|
||||
struct Stack *next;
|
||||
} Stack;
|
||||
|
||||
void linked_print(Stack *l);
|
||||
Stack *add_head(Stack *l, double x);
|
||||
Stack *del_head(Stack *l);
|
||||
@@ -0,0 +1,53 @@
|
||||
#include "head.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
srand(time(0));
|
||||
Stack *numbers = NULL;
|
||||
char c;
|
||||
|
||||
while ((c = getchar()) != EOF)
|
||||
{
|
||||
if (c == '\n')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (c == ' ')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case '0' ... '9':
|
||||
numbers = add_head(numbers, (double)(c - '0'));
|
||||
break;
|
||||
|
||||
case '+':
|
||||
case '-':
|
||||
case '*':
|
||||
case '/':
|
||||
|
||||
double a = numbers->x;
|
||||
numbers = del_head(numbers);
|
||||
double b = numbers->x;
|
||||
numbers = del_head(numbers);
|
||||
|
||||
b = (c == '+') ? b + a : b;
|
||||
b = (c == '-') ? b - a : b;
|
||||
b = (c == '*') ? b * a : b;
|
||||
b = (c == '/') ? b / a : b;
|
||||
|
||||
numbers = add_head(numbers, b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("res: %lF\n", numbers->x);
|
||||
|
||||
free(numbers);
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,170 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
tree *init_root(tree *root, int field)
|
||||
{
|
||||
tree *tmp = malloc(sizeof(tree)); // Выделение памяти под корень дерева
|
||||
tmp->field = field; // Присваивание значения ключу
|
||||
tmp->parent = NULL; // Присваивание указателю на родителя значения NULL
|
||||
tmp->left = tmp->right = NULL; // Присваивание указателю на левое и правое поддерево значения NULL
|
||||
root = tmp;
|
||||
return root;
|
||||
}
|
||||
|
||||
tree *add_node(tree *root, int field)
|
||||
{
|
||||
tree *root2 = root, *root3 = NULL; // Выделение памяти под узел дерева
|
||||
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
|
||||
tmp->field = field;
|
||||
while (root2 != NULL)
|
||||
{
|
||||
root3 = root2;
|
||||
if (field < root2->field)
|
||||
root2 = root2->left;
|
||||
else
|
||||
root2 = root2->right;
|
||||
}
|
||||
tmp->parent = root3; // Присваивание указателю на левое и правое поддерево значения NULL
|
||||
tmp->left = NULL;
|
||||
tmp->right = NULL;
|
||||
if (field < root3->field)
|
||||
root3->left = tmp;
|
||||
else
|
||||
root3->right = tmp;
|
||||
return root;
|
||||
}
|
||||
|
||||
tree *find_node(tree *root, int field) // Если дерево пусто или ключ корня равен искомому ключу, то возвращается указатель на корень
|
||||
{
|
||||
if ((root == NULL) || (root->field == field))
|
||||
return root; // Поиск нужного узла
|
||||
if (field < root->field)
|
||||
return find_node(root->left, field);
|
||||
else
|
||||
return find_node(root->right, field);
|
||||
}
|
||||
|
||||
// Минимальный элемент дерева
|
||||
tree *minim(tree *root)
|
||||
{
|
||||
tree *lef = root;
|
||||
while (lef->left != NULL)
|
||||
lef = lef->left;
|
||||
return lef;
|
||||
}
|
||||
|
||||
// Максимальный элемент дерева
|
||||
tree *maxim(tree *root)
|
||||
{
|
||||
tree *rig = root;
|
||||
while (rig->right != NULL)
|
||||
rig = rig->right;
|
||||
return rig;
|
||||
}
|
||||
|
||||
tree *findForDel(tree *root)
|
||||
{
|
||||
tree *rig = root, *lef = NULL; // Если есть правое поддерево, то ищем минимальный элемент в этом поддереве
|
||||
if (rig->right != NULL)
|
||||
return minim(rig->right);
|
||||
lef = rig->parent;
|
||||
while ((lef != NULL) && (rig == lef->right))
|
||||
{
|
||||
rig = lef;
|
||||
lef = lef->parent;
|
||||
}
|
||||
return lef;
|
||||
}
|
||||
|
||||
tree *del_node(tree *root, int field)
|
||||
{
|
||||
if (root == NULL)
|
||||
return NULL;
|
||||
|
||||
|
||||
// Поиск удаляемого узла по ключу
|
||||
tree *rig = root, *lef = NULL, *tmp = NULL;
|
||||
lef = find_node(root, field);
|
||||
|
||||
// 1 случай: у узла нет потомков
|
||||
if ((lef->left == NULL) && (lef->right == NULL))
|
||||
{
|
||||
printf("no children\n");
|
||||
if (lef == lef->parent->right)
|
||||
lef->parent->right = NULL;
|
||||
else
|
||||
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)
|
||||
tmp->parent->left = NULL;
|
||||
else
|
||||
tmp->parent->left = tmp->right;
|
||||
free(tmp);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
void preorder(tree *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
if (root->field)
|
||||
printf("%d\n", 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);
|
||||
}
|
||||
|
||||
void inorder(tree *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
inorder(root->left);
|
||||
if (root->field)
|
||||
printf("%d ", root->field);
|
||||
inorder(root->right);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
typedef struct tnode
|
||||
{
|
||||
int field;
|
||||
struct tnode *left;
|
||||
struct tnode *right;
|
||||
struct tnode *parent;
|
||||
} tree;
|
||||
|
||||
tree *init_root(tree *root, int field);
|
||||
tree *add_node(tree *root, int field);
|
||||
tree *find_node(tree *root, int field);
|
||||
tree *minim(tree *root);
|
||||
tree *maxim(tree *root);
|
||||
tree *findForDel(tree *root);
|
||||
tree *del_node(tree *root, int field);
|
||||
void preorder(tree *root);
|
||||
void inorder(tree *root);
|
||||
void postorder(tree *root);
|
||||
@@ -0,0 +1,37 @@
|
||||
#include "head.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
// Дерево улетает в SF
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
tree *root = NULL;
|
||||
root = init_root(root, 'A');
|
||||
preorder(root);
|
||||
|
||||
root = add_node(root, 'r');
|
||||
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');
|
||||
root = add_node(root, 'D');
|
||||
root = add_node(root, 'G');
|
||||
root = add_node(root, 'C');
|
||||
root = add_node(root, 'H');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
postorder(root);
|
||||
printf("\n");
|
||||
inorder(root);
|
||||
printf("\n");
|
||||
root = del_node(root, 'B');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,312 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <error.h>
|
||||
#include "head.h"
|
||||
|
||||
Tree *tnew(void)
|
||||
{
|
||||
Tree *tmp;
|
||||
if ((tmp = malloc(sizeof(Tree))) == NULL)
|
||||
{
|
||||
perror("malloc:");
|
||||
exit(1);
|
||||
}
|
||||
tmp->key = 0;
|
||||
tmp->left = NULL;
|
||||
tmp->right = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
Tree *leaf(char c)
|
||||
{
|
||||
Tree *tmp;
|
||||
tmp = tnew();
|
||||
tmp->key = c;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
Tree *maketree(char expr[], int first, int last)
|
||||
{
|
||||
Tree *root;
|
||||
int k;
|
||||
|
||||
if (first == last)
|
||||
{
|
||||
return leaf(expr[first]);
|
||||
}
|
||||
|
||||
k = lastop(expr, first, last);
|
||||
root = tnew();
|
||||
root->key = expr[k];
|
||||
root->left = maketree(expr, first, k - 1);
|
||||
root->right = maketree(expr, k + 1, last);
|
||||
return root;
|
||||
}
|
||||
|
||||
unsigned char priority(char c)
|
||||
{
|
||||
if ((c - '0' >= 0) && (c - '0' <= 9))
|
||||
{
|
||||
return MPRT;
|
||||
}
|
||||
switch (c)
|
||||
{
|
||||
case '+':
|
||||
case '-':
|
||||
return 1;
|
||||
case '*':
|
||||
case '/':
|
||||
return 2;
|
||||
case '^':
|
||||
return 3;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
int lastop(char expr[], int first, int last)
|
||||
{
|
||||
unsigned char min, prt;
|
||||
int i, k;
|
||||
min = MPRT;
|
||||
for (i = first; i <= last; i++)
|
||||
{
|
||||
prt = priority(expr[i]);
|
||||
if (prt <= min)
|
||||
{
|
||||
min = prt;
|
||||
k = i;
|
||||
}
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
int calculate(Tree *root)
|
||||
{
|
||||
int num1, num2;
|
||||
if (!(root->left))
|
||||
{
|
||||
return root->key - 0x30;
|
||||
}
|
||||
num1 = calculate(root->left);
|
||||
num2 = calculate(root->right);
|
||||
switch (root->key)
|
||||
{
|
||||
case '+':
|
||||
return num1 + num2;
|
||||
case '-':
|
||||
return num1 - num2;
|
||||
case '*':
|
||||
return num1 * num2;
|
||||
case '/':
|
||||
return num1 / num2;
|
||||
case '^':
|
||||
return pow(num1, num2);
|
||||
}
|
||||
return 0xffffffff;
|
||||
}
|
||||
|
||||
void showtree(Tree *root, int level)
|
||||
{
|
||||
if ((root->key > 31) && (root->key < 127))
|
||||
{
|
||||
printf("%c ", root->key);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%X ", root->key);
|
||||
}
|
||||
|
||||
if (root->right)
|
||||
{
|
||||
printf("------> ");
|
||||
level++;
|
||||
showtree(root->right, level);
|
||||
level--;
|
||||
}
|
||||
if (root->left)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
printf("\n");
|
||||
for (i = 0; i < level; i++)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
printf("| \n");
|
||||
|
||||
for (i = 0; i < level; i++)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
showtree(root->left, level);
|
||||
}
|
||||
}
|
||||
|
||||
Tree *create(unsigned char key)
|
||||
{
|
||||
Tree *tmp;
|
||||
if ((tmp = malloc(sizeof(Tree))) == NULL)
|
||||
{
|
||||
perror("malloc:");
|
||||
exit(1);
|
||||
}
|
||||
tmp->key = key;
|
||||
tmp->parent = NULL;
|
||||
tmp->left = NULL;
|
||||
tmp->right = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
Tree *add(Tree *root, unsigned char key)
|
||||
{
|
||||
Tree *rtmp = root, *rsave = NULL, *tmp;
|
||||
if ((tmp = malloc(sizeof(Tree))) == NULL)
|
||||
{
|
||||
perror("malloc:");
|
||||
exit(1);
|
||||
}
|
||||
while (rtmp)
|
||||
{
|
||||
rsave = rtmp;
|
||||
if (key < rtmp->key)
|
||||
{
|
||||
rtmp = rtmp->left;
|
||||
}
|
||||
else if (key > rtmp->key)
|
||||
{
|
||||
rtmp = rtmp->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\n There is element %c \n", key);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
tmp->parent = rsave;
|
||||
tmp->left = NULL;
|
||||
tmp->right = NULL;
|
||||
tmp->key = key;
|
||||
if (key < rsave->key)
|
||||
{
|
||||
rsave->left = tmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
rsave->right = tmp;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
Tree *search(Tree *root, unsigned char key)
|
||||
{
|
||||
if ((root == NULL) || (root->key == key))
|
||||
{
|
||||
return root;
|
||||
}
|
||||
if (key < root->key)
|
||||
{
|
||||
return search(root->left, key);
|
||||
}
|
||||
else if (key > root->key)
|
||||
{
|
||||
return search(root->right, key);
|
||||
}
|
||||
}
|
||||
|
||||
Tree *min(Tree *root)
|
||||
{
|
||||
while (root->left != NULL)
|
||||
{
|
||||
root = root->left;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
Tree *max(Tree *root)
|
||||
{
|
||||
while (root->right != NULL)
|
||||
{
|
||||
root = root->right;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
Tree *nextdel(Tree *root)
|
||||
{
|
||||
Tree *l = NULL;
|
||||
if (root->right != NULL)
|
||||
{
|
||||
return min(root->right);
|
||||
}
|
||||
l = root->parent;
|
||||
while ((l != NULL) && (root == l->right))
|
||||
{
|
||||
root = l;
|
||||
l = l->parent;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
void delete(Tree *del)
|
||||
{
|
||||
Tree *tmp;
|
||||
if (del == NULL)
|
||||
{
|
||||
printf("\n no such elem \n");
|
||||
return;
|
||||
}
|
||||
if ((del->left == NULL) && (del->right == NULL))
|
||||
{
|
||||
if (del == del->parent->right)
|
||||
{
|
||||
del->parent->right = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
del->parent->right = NULL;
|
||||
}
|
||||
free(del);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((del->left == NULL) && (del->right != NULL))
|
||||
{
|
||||
if (del == del->parent->right)
|
||||
{
|
||||
del->parent->right = del->right;
|
||||
del->parent->right->parent = del->parent;
|
||||
}
|
||||
else
|
||||
{
|
||||
del->parent->left = del->right;
|
||||
del->parent->left->parent = del->parent;
|
||||
}
|
||||
free(del);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((del->left != NULL) && (del->right == NULL))
|
||||
{
|
||||
if (del == del->parent->right)
|
||||
{
|
||||
del->parent->right = del->left;
|
||||
del->parent->right->parent = del->parent;
|
||||
}
|
||||
else
|
||||
{
|
||||
del->parent->left = del->left;
|
||||
del->parent->left->parent = del->parent;
|
||||
}
|
||||
free(del);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((del->left != NULL) && (del->right != NULL))
|
||||
{
|
||||
tmp = min(del->right);
|
||||
del->key = tmp->key;
|
||||
delete (tmp);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#define MPRT 255
|
||||
#define SIZE 80
|
||||
|
||||
typedef struct tree
|
||||
{
|
||||
unsigned char key;
|
||||
struct tree *parent;
|
||||
struct tree *left;
|
||||
struct tree *right;
|
||||
} Tree;
|
||||
|
||||
Tree *tnew(void);
|
||||
Tree *leaf(char c);
|
||||
Tree *maketree(char expr[], int first, int last);
|
||||
unsigned char priority(char c);
|
||||
int lastop(char expr[], int first, int last);
|
||||
int calculate(Tree *root);
|
||||
void showtree(Tree *root, int level);
|
||||
Tree *create(unsigned char key);
|
||||
Tree *add(Tree *root, unsigned char key);
|
||||
Tree *search(Tree *root, unsigned char key);
|
||||
Tree *min(Tree *root);
|
||||
Tree *max(Tree *root);
|
||||
Tree *nextdel(Tree *root);
|
||||
void delete(Tree *del);
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <error.h>
|
||||
#include "head.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char s[SIZE];
|
||||
Tree *tree = NULL;
|
||||
if (argc < 2)
|
||||
{
|
||||
printf("no expression\n");
|
||||
return 2;
|
||||
}
|
||||
strcpy(s, argv[1]);
|
||||
printf("processing %s\n", s);
|
||||
tree = maketree(s, 0, strlen(s) - 1);
|
||||
|
||||
printf("\n tree: \n");
|
||||
showtree(tree, 0);
|
||||
|
||||
printf("= %d \n", calculate(tree));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Node {
|
||||
int data;
|
||||
struct Node* next;
|
||||
};
|
||||
|
||||
struct Node* top = NULL; // инициализация вершины стека как пустой
|
||||
|
||||
/* функция для добавления элемента в стек */
|
||||
void push(int data) {
|
||||
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
|
||||
new_node->data = data;
|
||||
new_node->next = top;
|
||||
top = new_node;
|
||||
}
|
||||
|
||||
/* функция для удаления элемента из стека */
|
||||
int pop() {
|
||||
if (top == NULL) { // проверка на пустой стек
|
||||
printf("Stack Underflow!");
|
||||
return -1;
|
||||
}
|
||||
int data = top->data;
|
||||
struct Node* temp = top;
|
||||
top = top->next; // Спросить по поводу отсутствия эл-та
|
||||
free(temp);
|
||||
return data;
|
||||
}
|
||||
|
||||
/* функция для просмотра последнего элемента стека */
|
||||
int peek() {
|
||||
if (top == NULL) { // проверка на пустой стек
|
||||
printf("Stack is Empty!");
|
||||
return -1;
|
||||
}
|
||||
return top->data;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%d\n", peek());
|
||||
push(1);
|
||||
printf("%d\n", peek());
|
||||
push(2);
|
||||
printf("%d\n", peek());
|
||||
push(3);
|
||||
|
||||
printf("%d\n", pop()); // выведет 3
|
||||
printf("%d\n", peek()); // выведет 2
|
||||
printf("%d", pop()); // выведет 2
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
#include "header.h"
|
||||
|
||||
|
||||
void linked_print(Node* l) {
|
||||
while (l != NULL) {
|
||||
printf("%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
Node* add_tail(Node* l, int x, char name[]) {
|
||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||
l->x = x;
|
||||
memcpy(l->name, name, STR_LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
Node* res = l;
|
||||
while (l->next != NULL) l = l->next;
|
||||
|
||||
Node* tmp = malloc(sizeof(Node));
|
||||
tmp->x = x;
|
||||
memcpy(tmp->name, name, STR_LEN);
|
||||
tmp->prev = l;
|
||||
l->next = tmp;
|
||||
return res;
|
||||
}
|
||||
|
||||
Node* add_head(Node* l, int x, char name[]) {
|
||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||
l->x = x;
|
||||
memcpy(l->name, name, STR_LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
Node* tmp = malloc(sizeof(Node));
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
l->prev = tmp;
|
||||
tmp->prev = NULL;
|
||||
tmp->x = x;
|
||||
memcpy(tmp->name, name, STR_LEN);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
Node* del_head(Node* l) {
|
||||
if (l == NULL) return NULL;
|
||||
else if (l->next == NULL) {
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
}
|
||||
Node* tmp = l->next;
|
||||
tmp->prev = NULL;
|
||||
free(l);
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void del_tail(Node* l) {
|
||||
if (l == NULL) return;
|
||||
else if (l->next == NULL) {
|
||||
free(l);
|
||||
l = NULL;
|
||||
return;
|
||||
}
|
||||
while (l->next != NULL) l = l->next;
|
||||
Node* tmp = l->prev;
|
||||
l->prev = NULL;
|
||||
free(l);
|
||||
tmp->next = NULL;
|
||||
}
|
||||
|
||||
int len(Node* l) {
|
||||
int res = 0;
|
||||
while (l != NULL) {
|
||||
res++;
|
||||
l = l->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void swap(Node* l, int first, int second) {
|
||||
if (first >= len(l) || second >= len(l)) exit(3);
|
||||
int i, min, max;
|
||||
min = (first <= second) ? first : second;
|
||||
max = first + second - min;
|
||||
|
||||
for (i = 0; i < min; i++) {
|
||||
if (l != NULL) l = l->next;
|
||||
else exit(2);
|
||||
}
|
||||
Node* tmp = l;
|
||||
|
||||
for (i = 0; i < max - min; i++) {
|
||||
if (l != NULL) l = l->next;
|
||||
else exit(2);
|
||||
}
|
||||
|
||||
Node temp = *l;
|
||||
l->x = tmp->x;
|
||||
tmp->x = temp.x;
|
||||
memcpy(l->name, tmp->name, STR_LEN);
|
||||
memcpy(tmp->name, temp.name, STR_LEN);
|
||||
}
|
||||
|
||||
Node get(Node* l, int n) {
|
||||
if (n > len(l)) exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
|
||||
void select_sort(Node* l) {
|
||||
if (l == NULL || l->next == NULL) return;
|
||||
|
||||
int n = len(l), i, j;
|
||||
|
||||
for (i = 0; i < n - 1; i++) {
|
||||
for (j = i + 1; j < n; j++) {
|
||||
if (get(l, j).x < get(l, i).x)
|
||||
swap(l, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void list_fprintf(Node* l, char path[]) {
|
||||
FILE* f = fopen(path, "w");
|
||||
if (f == NULL)
|
||||
exit(4);
|
||||
int i, n = len(l);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
fprintf(f, "%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
Node* list_fscanf(char path[]) {
|
||||
int tmp_x, i;
|
||||
char s[STR_LEN] = {'\0'};
|
||||
FILE* f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
exit(5);
|
||||
|
||||
Node* tmp = malloc(sizeof(Node)); //Дописать проверку на NULL
|
||||
tmp->prev = NULL;
|
||||
tmp->next = NULL;
|
||||
|
||||
|
||||
|
||||
for (i = 0; fscanf(f, "%d %s\n", &tmp_x, s) != EOF; i++) {
|
||||
if (i == 0) {
|
||||
tmp->x = tmp_x;
|
||||
memcpy(tmp->name, s, STR_LEN);
|
||||
} // Перенести в a_t
|
||||
else add_tail(tmp, tmp_x, s);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return tmp;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 5
|
||||
#define STR_LEN 8
|
||||
|
||||
typedef struct Node {
|
||||
int x;
|
||||
struct Node* next;
|
||||
struct Node* prev;
|
||||
char name[STR_LEN];
|
||||
} Node;
|
||||
|
||||
void linked_print(Node* l);
|
||||
Node* add_tail(Node* l, int x, char name[]);
|
||||
Node* add_head(Node* l, int x, char name[]);
|
||||
Node* del_head(Node* l);
|
||||
void del_tail(Node* l);
|
||||
int len(Node* l);
|
||||
Node get(Node* l, int n);
|
||||
|
||||
void swap(Node* l1, int i, int j);
|
||||
void select_sort(Node* l);
|
||||
void list_fprintf(Node* l, char path[]);
|
||||
Node* list_fscanf(char path[]);
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "function.c"
|
||||
|
||||
int main() {
|
||||
srand(time(0));
|
||||
Node* l = malloc(sizeof(Node));
|
||||
if (l == NULL)
|
||||
exit(1);
|
||||
|
||||
int i, j;
|
||||
for (i = 0; i < N; i++) {
|
||||
char s[STR_LEN];
|
||||
for (j = 0; j < STR_LEN - 1; j++)
|
||||
s[j] = 'a' + rand() % 25;
|
||||
s[j + 1] = '\0';
|
||||
add_tail(l, rand() % 100, s);
|
||||
}
|
||||
|
||||
printf("\nAfter add_tail(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
l = add_head(l, 5, "head");
|
||||
printf("\nAfter add_head(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
l = del_head(l);
|
||||
printf("\nAfter del_head(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
swap(l, 0, 1);
|
||||
printf("\nAfter swap first and second el:\n");
|
||||
linked_print(l);
|
||||
|
||||
del_tail(l);
|
||||
printf("\nAfter del_tail(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
select_sort(l);
|
||||
printf("\nAfter sort:\n");
|
||||
linked_print(l);
|
||||
|
||||
printf("\nWriting to file test.txt\n");
|
||||
list_fprintf(l, "test.txt");
|
||||
|
||||
printf("\nRead from file test.txt:\n");
|
||||
Node* l2 = list_fscanf("test.txt");
|
||||
linked_print(l2);
|
||||
|
||||
free(l);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,4 @@
|
||||
2 qjrjqtg
|
||||
7 upivrdd
|
||||
49 bokdbfv
|
||||
74 hlchgxy
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
/* function to solve the Tower of Hanoi puzzle */
|
||||
void tower_of_hanoi(int n, char from, char to, char aux)
|
||||
{
|
||||
/* base case - when there's only one disk present */
|
||||
if (n == 1)
|
||||
{
|
||||
printf("Move disk 1 from rod %c to rod %c\n", from, to);
|
||||
return;
|
||||
}
|
||||
/* move n-1 disks from the source to auxiliary rod */
|
||||
tower_of_hanoi(n - 1, from, aux, to);
|
||||
/* move the remaining one disk from the source to destination rod */
|
||||
printf("Move disk %d from rod %c to rod %c\n", n, from, to);
|
||||
/* move n-1 disks from the auxiliary to destination rod */
|
||||
tower_of_hanoi(n - 1, aux, to, from);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
/* ask user for the number of disks */
|
||||
printf("Enter the number of disks: ");
|
||||
scanf("%d", &n);
|
||||
/* call the tower_of_hanoi function to solve the puzzle */
|
||||
tower_of_hanoi(n, 'A', 'C', 'B');
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
1 fdxyqwb3
|
||||
1 icotltwo
|
||||
2 ysharmos
|
||||
2 xhlbvpd3
|
||||
3 owvischE
|
||||
3 bmmuvuhM
|
||||
@@ -0,0 +1,166 @@
|
||||
#include "head.h"
|
||||
|
||||
|
||||
void st_rand(Book books[], int n) {
|
||||
int i, j;
|
||||
for (i = 0; i < n; i++) {
|
||||
// books[i].len = rand() % 99 + 1;
|
||||
books[i].len = rand() % 3 + 1;
|
||||
for (j = 0; j < STR_LEN - 1; j++)
|
||||
books[i].name[j] = 'a' + rand() % 25;
|
||||
books[i].name[j + 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
void st_print(Book books[], int n) {
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("len: %3d; name: %s\n", books[i].len, books[i].name);
|
||||
}
|
||||
|
||||
void swap(Book* b1, Book* b2) {
|
||||
Book* tmp = malloc(sizeof(Book));
|
||||
|
||||
if (tmp == NULL) {
|
||||
printf("could not malloc\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(tmp, b1, sizeof(Book));
|
||||
memcpy(b1, b2, sizeof(Book));
|
||||
memcpy(b2, tmp, sizeof(Book));
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
void selection_sort(Book books[], int n) {
|
||||
int i, j;
|
||||
for (i = 0; i < n - 1; i++) {
|
||||
for (j = i + 1; j < n; j++) {
|
||||
if (books[j].len < books[i].len)
|
||||
swap(books + i, books + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Book* bin_search(Book books[], int n, int key) {
|
||||
Book* res = NULL;
|
||||
int low = 0, high = n - 1;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (high + low) / 2;
|
||||
|
||||
if (books[mid].len == key) {
|
||||
res = &books[mid];
|
||||
break;
|
||||
}
|
||||
|
||||
low = (books[mid].len < key) ? mid + 1: low;
|
||||
high = (books[mid].len > key) ? mid - 1: high;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void pst(Book books[], int size, int n) {
|
||||
if (n >= size) {
|
||||
printf("index bigger then length of the array\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
printf("len: %3d; name: %s\n", books[n].len, books[n].name);
|
||||
}
|
||||
|
||||
int matches_number(Book b[], int n, int match) {
|
||||
int res = 0, i;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (b[i].len == match) res++;
|
||||
else if (b[i].len > match) break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int find_pairs_easy(Book b1[], int n1, Book b2[], int n2) {
|
||||
int res = 0, i;
|
||||
for (i = 0; i < n1; i++) {
|
||||
Book* b_res = bin_search(b2, n2, b1[i].len);
|
||||
if (b_res == NULL) continue;
|
||||
res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int find_pairs_advanced(Book b1[], int n1, Book b2[], int n2) {
|
||||
int res = 0, i;
|
||||
for (i = 0; i < n1; i++) {
|
||||
Book* b_res = bin_search(b2, n2, b1[i].len);
|
||||
if (b_res == NULL) continue;
|
||||
res += matches_number(b2, n2, b1[i].len);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void st_fprintf(Book books[], int n, char path[], char access[]) {
|
||||
int i;
|
||||
FILE* f = fopen(path, access);
|
||||
if (f == NULL) {
|
||||
printf("Could not open file %s\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
fprintf(f, "%d %s\n", books[i].len, books[i].name);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
int lines_numf(char path[]) {
|
||||
int res = 0;
|
||||
char c;
|
||||
FILE* f = fopen(path, "r");
|
||||
if (f == NULL) {
|
||||
printf("Could not open file %s\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
while ((c = fgetc(f)) != EOF) {
|
||||
if (c == '\n') res++;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return res;
|
||||
}
|
||||
|
||||
int lines_num(char path[]) {
|
||||
int res = 0, flag = 0;
|
||||
char c;
|
||||
FILE* f = fopen(path, "r");
|
||||
if (f == NULL) {
|
||||
printf("Could not open file %s\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
while ((c = fgetc(f)) != EOF) {
|
||||
if (c != '\n') flag = 1;
|
||||
if (c == '\n' && flag) {
|
||||
res++;
|
||||
flag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return res + flag;
|
||||
}
|
||||
|
||||
void st_fscanf(Book b[], int n, char path[]) {
|
||||
int i;
|
||||
FILE* f = fopen(path, "r");
|
||||
if (f == NULL) {
|
||||
printf("Could not open file %s\n", path);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
fscanf(f, "%d %s\n", &b[i].len, b[i].name);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "head.h"
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
srand(time(0));
|
||||
|
||||
Book* books1 = malloc(sizeof(Book) * N);
|
||||
Book* books2 = malloc(sizeof(Book) * N);
|
||||
|
||||
if (books1 == NULL || books2 == NULL) {
|
||||
printf("could not malloc\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
st_rand(books1, N);
|
||||
st_rand(books2, N);
|
||||
|
||||
printf("First array:\n");
|
||||
st_print(books1, N);
|
||||
printf("\nSecond array:\n");
|
||||
st_print(books2, N);
|
||||
|
||||
selection_sort(books1, N);
|
||||
selection_sort(books2, N);
|
||||
|
||||
printf("\nFirst array after sort:\n");
|
||||
st_print(books1, N);
|
||||
printf("\nSecond array after sort:\n");
|
||||
st_print(books2, N);
|
||||
|
||||
Book* res = bin_search(books1, N, 15);
|
||||
|
||||
if (res == NULL)
|
||||
printf("\nNo such book\n");
|
||||
else
|
||||
printf("\nLength: %2d; name: %s\n", res->len, res->name);
|
||||
|
||||
printf("\nElement in first array: ");
|
||||
pst(books1, N, (int)(N * 0.7));
|
||||
printf("Element in second array: ");
|
||||
pst(books2, N, (int)(N * 0.3));
|
||||
|
||||
printf("\nNumber of pairs (easy realization): %d\n", find_pairs_easy(books1, N, books2, N));
|
||||
printf("Number of pairs (advanced realization): %d\n", find_pairs_advanced(books1, N, books2, N));
|
||||
|
||||
st_fprintf(books1, N, "books.txt", "w");
|
||||
st_fprintf(books2, N, "books.txt", "a");
|
||||
|
||||
int line_number = lines_numf("books.txt");
|
||||
printf("\nNumber of lines in books.txt is %d\n", line_number);
|
||||
|
||||
Book* books3 = malloc(sizeof(Book) * line_number);
|
||||
|
||||
if (books3 == NULL) {
|
||||
printf("could not malloc\n");
|
||||
free(books1);
|
||||
free(books2);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
st_fscanf(books3, line_number, "books.txt");
|
||||
selection_sort(books3, line_number);
|
||||
|
||||
printf("\nThird array is:\n");
|
||||
st_print(books3, line_number);
|
||||
|
||||
free(books1);
|
||||
free(books2);
|
||||
free(books3);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 3
|
||||
#define STR_LEN 8
|
||||
|
||||
|
||||
typedef struct Book {
|
||||
int len;
|
||||
char name[STR_LEN];
|
||||
} Book;
|
||||
|
||||
void st_rand(Book books[], int n);
|
||||
void st_print(Book books[], int n);
|
||||
void swap(Book* b1, Book* b2);
|
||||
void selection_sort(Book books[], int n);
|
||||
Book* bin_search(Book books[], int n, int key);
|
||||
void pst(Book books[], int size, int n);
|
||||
int matches_number(Book b[], int n, int match);
|
||||
int find_pairs_easy(Book b1[], int n1, Book b2[], int n2);
|
||||
int find_pairs_advanced(Book b1[], int n1, Book b2[], int n2);
|
||||
void st_fprintf(Book books[], int n, char path[], char access[]);
|
||||
int lines_numf(char path[]);
|
||||
int lines_num(char path[]);
|
||||
void st_fscanf(Book b[], int n, char path[]);
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
double func(double x)
|
||||
{
|
||||
return exp(x*5.8) + sin(x+3.5);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
srand(time(0));
|
||||
|
||||
double start = -5.6, finish = -1.0, e = 1e-6, mid = (finish + start) / 2.0;
|
||||
|
||||
if (func(start) * func(finish) > 0)
|
||||
{
|
||||
printf("Couldn`t find\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
while (fabs(func(mid)) >= e)
|
||||
{
|
||||
mid = (finish + start) / 2.0;
|
||||
if (func(mid) > 0)
|
||||
{
|
||||
start = (func(start) > func(finish)) ? mid : start;
|
||||
finish = (func(start) < func(finish)) ? mid : finish;
|
||||
}
|
||||
else
|
||||
{
|
||||
start = (func(start) < func(finish)) ? mid : start;
|
||||
finish = (func(start) > func(finish)) ? mid : finish;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Zero: %lF\n", start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,103 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define N 20
|
||||
|
||||
void selectionSort(int arr[], int n)
|
||||
{
|
||||
int i, j, tmp;
|
||||
for (i = 0; i < n - 1; i++)
|
||||
{
|
||||
for (j = i + 1; j < n; j++)
|
||||
{
|
||||
if (arr[j] < arr[i])
|
||||
{
|
||||
tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bubbleSort(int arr[], int n)
|
||||
{
|
||||
int i, j, tmp;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
for (j = 0; j < n - 1; j++)
|
||||
{
|
||||
if (arr[j] > arr[j + 1])
|
||||
{
|
||||
tmp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void insertionSort(int arr[], int n)
|
||||
{
|
||||
int i, j, tmp;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
for (j = 0; j < i; j++)
|
||||
{
|
||||
if (arr[i] < arr[j])
|
||||
{
|
||||
tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
srand(time(0));
|
||||
|
||||
int *arr1 = malloc(sizeof(int) * N);
|
||||
int *arr2 = malloc(sizeof(int) * N);
|
||||
int *arr3 = malloc(sizeof(int) * N);
|
||||
|
||||
if (!arr1 || !arr2 || !arr3)
|
||||
{
|
||||
printf("could not malloc\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < N; i++)
|
||||
{
|
||||
int d = rand() % 100;
|
||||
arr1[i] = d;
|
||||
arr2[i] = d;
|
||||
arr3[i] = d;
|
||||
}
|
||||
|
||||
selectionSort(arr1, N);
|
||||
insertionSort(arr2, N);
|
||||
bubbleSort(arr3, N);
|
||||
|
||||
printf("Selection sort: ");
|
||||
for (i = 0; i < N; i++)
|
||||
printf("%d ", arr1[i]);
|
||||
|
||||
printf("\nInsertion sort: ");
|
||||
for (i = 0; i < N; i++)
|
||||
printf("%d ", arr2[i]);
|
||||
|
||||
printf("\nBubble sort: ");
|
||||
for (i = 0; i < N; i++)
|
||||
printf("%d ", arr3[i]);
|
||||
|
||||
printf("\n");
|
||||
|
||||
free(arr1);
|
||||
free(arr2);
|
||||
free(arr3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,206 @@
|
||||
#include "head.h"
|
||||
|
||||
void linked_print(LinkedList *l)
|
||||
{
|
||||
while (l != NULL)
|
||||
{
|
||||
printf("%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
LinkedList *add_tail(LinkedList *l, int x, char name[])
|
||||
{
|
||||
if (l->x == 0 && !strcmp(l->name, "\0"))
|
||||
{
|
||||
l->x = x;
|
||||
memcpy(l->name, name, STR_LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkedList *res = l;
|
||||
while (l->next != NULL)
|
||||
l = l->next;
|
||||
|
||||
LinkedList *tmp = malloc(sizeof(LinkedList));
|
||||
tmp->x = x;
|
||||
memcpy(tmp->name, name, STR_LEN);
|
||||
tmp->prev = l;
|
||||
l->next = tmp;
|
||||
return res;
|
||||
}
|
||||
|
||||
LinkedList *add_head(LinkedList *l, int x, char name[])
|
||||
{
|
||||
if (l->x == 0 && !strcmp(l->name, "\0"))
|
||||
{
|
||||
l->x = x;
|
||||
memcpy(l->name, name, STR_LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkedList *tmp = malloc(sizeof(LinkedList));
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
l->prev = tmp;
|
||||
tmp->prev = NULL;
|
||||
tmp->x = x;
|
||||
memcpy(tmp->name, name, STR_LEN);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
LinkedList *del_head(LinkedList *l)
|
||||
{
|
||||
if (l == NULL)
|
||||
return NULL;
|
||||
else if (l->next == NULL)
|
||||
{
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
}
|
||||
LinkedList *tmp = l->next;
|
||||
tmp->prev = NULL;
|
||||
free(l);
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void del_tail(LinkedList *l)
|
||||
{
|
||||
if (l == NULL)
|
||||
return;
|
||||
else if (l->next == NULL)
|
||||
{
|
||||
free(l);
|
||||
l = NULL;
|
||||
return;
|
||||
}
|
||||
while (l->next != NULL)
|
||||
l = l->next;
|
||||
LinkedList *tmp = l->prev;
|
||||
l->prev = NULL;
|
||||
free(l);
|
||||
tmp->next = NULL;
|
||||
}
|
||||
|
||||
int len(LinkedList *l)
|
||||
{
|
||||
int res = 0;
|
||||
while (l != NULL)
|
||||
{
|
||||
res++;
|
||||
l = l->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void swap(LinkedList *l, int first, int second)
|
||||
{
|
||||
if (first >= len(l) || second >= len(l))
|
||||
exit(3);
|
||||
int i, min, max;
|
||||
min = (first <= second) ? first : second;
|
||||
max = first + second - min;
|
||||
|
||||
for (i = 0; i < min; i++)
|
||||
{
|
||||
if (l != NULL)
|
||||
l = l->next;
|
||||
else
|
||||
exit(2);
|
||||
}
|
||||
LinkedList *tmp = l;
|
||||
|
||||
for (i = 0; i < max - min; i++)
|
||||
{
|
||||
if (l != NULL)
|
||||
l = l->next;
|
||||
else
|
||||
exit(2);
|
||||
}
|
||||
|
||||
LinkedList temp = *l;
|
||||
l->x = tmp->x;
|
||||
tmp->x = temp.x;
|
||||
memcpy(l->name, tmp->name, STR_LEN);
|
||||
memcpy(tmp->name, temp.name, STR_LEN);
|
||||
}
|
||||
|
||||
LinkedList get(LinkedList *l, int n)
|
||||
{
|
||||
if (n > len(l))
|
||||
exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
|
||||
void select_sort(LinkedList *l)
|
||||
{
|
||||
if (l == NULL || l->next == NULL)
|
||||
return;
|
||||
|
||||
int n = len(l), i, j;
|
||||
|
||||
for (i = 0; i < n - 1; i++)
|
||||
{
|
||||
for (j = i + 1; j < n; j++)
|
||||
{
|
||||
if (get(l, j).x < get(l, i).x)
|
||||
swap(l, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void list_fprintf(LinkedList *l, char path[])
|
||||
{
|
||||
FILE *f = fopen(path, "w");
|
||||
if (f == NULL)
|
||||
exit(4);
|
||||
int i, n = len(l);
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
fprintf(f, "%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
LinkedList *list_fscanf(char path[])
|
||||
{
|
||||
FILE *f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
exit(5);
|
||||
|
||||
LinkedList *tmp = malloc(sizeof(LinkedList));
|
||||
tmp->prev = NULL;
|
||||
tmp->next = NULL;
|
||||
int tmp_x, i;
|
||||
char s[STR_LEN] = {'\0'};
|
||||
|
||||
for (i = 0; fscanf(f, "%d %s\n", &tmp_x, s) != EOF; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
tmp->x = tmp_x;
|
||||
memcpy(tmp->name, s, STR_LEN);
|
||||
}
|
||||
else
|
||||
add_tail(tmp, tmp_x, s);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return tmp;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "head.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
srand(time(0));
|
||||
LinkedList *l = malloc(sizeof(LinkedList));
|
||||
if (l == NULL)
|
||||
exit(1);
|
||||
|
||||
int i, j;
|
||||
for (i = 0; i < N; i++)
|
||||
{
|
||||
char s[STR_LEN];
|
||||
for (j = 0; j < STR_LEN - 1; j++)
|
||||
s[j] = 'a' + rand() % 25;
|
||||
s[j + 1] = '\0';
|
||||
add_tail(l, rand() % 100, s);
|
||||
}
|
||||
|
||||
printf("\nAfter add_tail(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
l = add_head(l, 5, "head");
|
||||
printf("\nAfter add_head(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
l = del_head(l);
|
||||
printf("\nAfter del_head(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
swap(l, 0, 1);
|
||||
printf("\nAfter swap first and second el:\n");
|
||||
linked_print(l);
|
||||
|
||||
del_tail(l);
|
||||
printf("\nAfter del_tail(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
select_sort(l);
|
||||
printf("\nAfter sort:\n");
|
||||
linked_print(l);
|
||||
|
||||
printf("\nWriting to file test.txt\n");
|
||||
list_fprintf(l, "test.txt");
|
||||
|
||||
printf("\nRead from file test.txt:\n");
|
||||
LinkedList *l2 = list_fscanf("test.txt");
|
||||
linked_print(l2);
|
||||
|
||||
free(l);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 5
|
||||
#define STR_LEN 8
|
||||
|
||||
typedef struct LinkedList
|
||||
{
|
||||
int x;
|
||||
struct LinkedList *next;
|
||||
struct LinkedList *prev;
|
||||
char name[STR_LEN];
|
||||
} LinkedList;
|
||||
|
||||
void linked_print(LinkedList *l);
|
||||
LinkedList *add_tail(LinkedList *l, int x, char name[]);
|
||||
LinkedList *add_head(LinkedList *l, int x, char name[]);
|
||||
LinkedList *del_head(LinkedList *l);
|
||||
void del_tail(LinkedList *l);
|
||||
int len(LinkedList *l);
|
||||
LinkedList get(LinkedList *l, int n);
|
||||
|
||||
void swap(LinkedList *l1, int i, int j);
|
||||
void select_sort(LinkedList *l);
|
||||
void list_fprintf(LinkedList *l, char path[]);
|
||||
LinkedList *list_fscanf(char path[]);
|
||||
@@ -0,0 +1,171 @@
|
||||
#include "head.h"
|
||||
|
||||
void linked_print(Node* l) {
|
||||
while (l != NULL) {
|
||||
printf("%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
Node* add_tail(Node* l, int x, char name[]) {
|
||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||
l->x = x;
|
||||
memcpy(l->name, name, STR_LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
Node* res = l;
|
||||
while (l->next != NULL) l = l->next;
|
||||
|
||||
Node* tmp = malloc(sizeof(Node));
|
||||
tmp->x = x;
|
||||
memcpy(tmp->name, name, STR_LEN);
|
||||
tmp->prev = l;
|
||||
l->next = tmp;
|
||||
return res;
|
||||
}
|
||||
|
||||
Node* add_head(Node* l, int x, char name[]) {
|
||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||
l->x = x;
|
||||
memcpy(l->name, name, STR_LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
Node* tmp = malloc(sizeof(Node));
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
l->prev = tmp;
|
||||
tmp->prev = NULL;
|
||||
tmp->x = x;
|
||||
memcpy(tmp->name, name, STR_LEN);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
Node* del_head(Node* l) {
|
||||
if (l == NULL) return NULL;
|
||||
else if (l->next == NULL) {
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
}
|
||||
Node* tmp = l->next;
|
||||
tmp->prev = NULL;
|
||||
free(l);
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void del_tail(Node* l) {
|
||||
if (l == NULL) return;
|
||||
else if (l->next == NULL) {
|
||||
free(l);
|
||||
l = NULL;
|
||||
return;
|
||||
}
|
||||
while (l->next != NULL) l = l->next;
|
||||
Node* tmp = l->prev;
|
||||
l->prev = NULL;
|
||||
free(l);
|
||||
tmp->next = NULL;
|
||||
}
|
||||
|
||||
int len(Node* l) {
|
||||
int res = 0;
|
||||
while (l != NULL) {
|
||||
res++;
|
||||
l = l->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void swap(Node* l, int first, int second) {
|
||||
if (first >= len(l) || second >= len(l)) exit(3);
|
||||
int i, min, max;
|
||||
min = (first <= second) ? first : second;
|
||||
max = first + second - min;
|
||||
|
||||
for (i = 0; i < min; i++) {
|
||||
if (l != NULL) l = l->next;
|
||||
else exit(2);
|
||||
}
|
||||
Node* tmp = l;
|
||||
|
||||
for (i = 0; i < max - min; i++) {
|
||||
if (l != NULL) l = l->next;
|
||||
else exit(2);
|
||||
}
|
||||
|
||||
Node temp = *l;
|
||||
l->x = tmp->x;
|
||||
tmp->x = temp.x;
|
||||
memcpy(l->name, tmp->name, STR_LEN);
|
||||
memcpy(tmp->name, temp.name, STR_LEN);
|
||||
}
|
||||
|
||||
Node get(Node* l, int n) {
|
||||
if (n > len(l)) exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
|
||||
void select_sort(Node* l) {
|
||||
if (l == NULL || l->next == NULL) return;
|
||||
|
||||
int n = len(l), i, j;
|
||||
|
||||
for (i = 0; i < n - 1; i++) {
|
||||
for (j = i + 1; j < n; j++) {
|
||||
if (get(l, j).x < get(l, i).x)
|
||||
swap(l, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void list_fprintf(Node* l, char path[]) {
|
||||
FILE* f = fopen(path, "w");
|
||||
if (f == NULL)
|
||||
exit(4);
|
||||
int i, n = len(l);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
fprintf(f, "%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
Node* list_fscanf(char path[]) {
|
||||
int tmp_x, i;
|
||||
char s[STR_LEN] = {'\0'};
|
||||
FILE* f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
exit(5);
|
||||
|
||||
Node* tmp = malloc(sizeof(Node)); //Дописать проверку на NULL
|
||||
tmp->prev = NULL;
|
||||
tmp->next = NULL;
|
||||
|
||||
|
||||
|
||||
for (i = 0; fscanf(f, "%d %s\n", &tmp_x, s) != EOF; i++) {
|
||||
if (i == 0) {
|
||||
tmp->x = tmp_x;
|
||||
memcpy(tmp->name, s, STR_LEN);
|
||||
} // Перенести в a_t
|
||||
else add_tail(tmp, tmp_x, s);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return tmp;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef struct Node {
|
||||
int x;
|
||||
struct Node* next;
|
||||
struct Node* prev;
|
||||
char name[STR_LEN];
|
||||
} Node;
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "func.c"
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define E 1.0E-5
|
||||
#define A1 -2.0
|
||||
#define B1 -1.5
|
||||
#define A2 -1.0
|
||||
#define B2 0.0
|
||||
|
||||
char *sf1 = "[-3:1] sin(2*x)-exp(x)\"";
|
||||
char *sf2 = "[-3:1] sin(2*x)+exp(x)\"";
|
||||
char *gpl = "gnuplot -p -e \"plot ";
|
||||
|
||||
#define DBG
|
||||
//gcc filename.c -l m -o filename
|
||||
double f1(double x) {
|
||||
return sin(2*x)-exp(x);
|
||||
}
|
||||
double f2(double x) {
|
||||
return sin(2*x)+exp(x);
|
||||
}
|
||||
|
||||
double dih(double f(), double a, double b);
|
||||
|
||||
int main(){
|
||||
char cmd[128] = "\0";
|
||||
strcat(cmd,gpl);
|
||||
strcat(cmd,sf1);
|
||||
printf("%s\n", cmd);
|
||||
system(cmd);
|
||||
printf("X1 = %.10F\n",dih(f1,A1,B1));
|
||||
printf("X2 = %F\n",dih(f2,A2,B2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
double dih(double f(), double a, double b){
|
||||
double c;
|
||||
while( fabs(b-a) > E ) {
|
||||
c = (a+b)/2;
|
||||
#ifdef DBG
|
||||
printf ("%F \n", c );
|
||||
#endif
|
||||
if(f(a)*f(c) < 0 )
|
||||
b = c;
|
||||
else
|
||||
a = c;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,84 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "header.h"
|
||||
|
||||
void pr(node *tmp){
|
||||
int i;
|
||||
if(tmp == NULL){printf("[%p]\n", tmp); return;}
|
||||
while(tmp){
|
||||
printf("[%14p]<-[%14p]->[%14p] ", tmp->prev, tmp, tmp->next);
|
||||
printf("data: [%4ld] s[" , tmp->data);
|
||||
for(i=0;i<COUN; i++) printf("%c", tmp->s[i]);
|
||||
printf("]\n");
|
||||
tmp = tmp->next;
|
||||
}
|
||||
printf("---------------------\n");
|
||||
}
|
||||
|
||||
node *add_head(node *head, int hh){
|
||||
node *tmp=NULL;
|
||||
int i;
|
||||
if((tmp=malloc(sizeof(node)))==NULL){
|
||||
perror("malloc: NULL");
|
||||
exit(2);
|
||||
}
|
||||
tmp->prev=NULL;
|
||||
tmp->data=hh;
|
||||
for(i=0;i<COUN;i++) tmp->s[i]=((char)(65+rand()%25));
|
||||
if(head==NULL) tmp->next=NULL;
|
||||
else{
|
||||
tmp->next=head;
|
||||
head->prev=tmp;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
node *add_tail(node *head, int hh){
|
||||
node *tmp=NULL;
|
||||
node *t = NULL;
|
||||
int i;
|
||||
if((tmp=malloc(sizeof(node)))==NULL){
|
||||
perror("malloc: NULL");
|
||||
exit(3);
|
||||
}
|
||||
|
||||
tmp->data=hh;
|
||||
for(i=0;i<COUN;i++) tmp->s[i]=((char)(65+rand()%25));
|
||||
tmp->next=NULL;
|
||||
if(head==NULL) tmp->prev=NULL;
|
||||
else{
|
||||
t = head;
|
||||
while(t->next!=NULL) t = t->next;
|
||||
t->next=tmp;
|
||||
tmp->prev=head;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
node *del_head(node *head){
|
||||
node *tmp = head;
|
||||
if(head==NULL) return NULL;
|
||||
if(head -> next == NULL){
|
||||
free(head);
|
||||
return NULL;
|
||||
}
|
||||
head = head->next;
|
||||
head -> prev = NULL;
|
||||
free(tmp);
|
||||
return head;
|
||||
}
|
||||
|
||||
node *del_tail(node *list){
|
||||
node *tail = NULL;
|
||||
if(list==NULL) return NULL;
|
||||
if(list -> next == NULL){
|
||||
free(list);
|
||||
return NULL;
|
||||
}
|
||||
tail = list;
|
||||
while(tail->next->next) tail = tail->next;
|
||||
free(tail->next);
|
||||
tail -> next=NULL;
|
||||
return list;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#define COUN 40
|
||||
|
||||
typedef struct Node
|
||||
{
|
||||
long int data;
|
||||
struct Node *next;
|
||||
struct Node *prev;
|
||||
char s[COUN];
|
||||
} node;
|
||||
|
||||
void pr(node *tmp);
|
||||
node *add_head(node *head, int hh);
|
||||
node *add_tail(node *head, int hh);
|
||||
node *del_head(node *head);
|
||||
node *del_tail(node *list);
|
||||
// node *sort(node *head);
|
||||
// int lenghth(node *list);
|
||||
// void writefile(const char *path, node *head);
|
||||
// node *readfile(const char *path);
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "header.h"
|
||||
#include <windows.h>
|
||||
|
||||
int main(int args, char *argv[])
|
||||
{
|
||||
node *head = NULL;
|
||||
int r;
|
||||
srand(time(NULL));
|
||||
|
||||
while ((r = (rand() % 100)) < 90)
|
||||
head = add_head(head, r);
|
||||
pr(head);
|
||||
head = del_head(head);
|
||||
pr(head);
|
||||
|
||||
head = add_tail(head, 5);
|
||||
pr(head);
|
||||
|
||||
head = del_tail(head);
|
||||
pr(head);
|
||||
}
|
||||
// Домашка: сортировка списка и вычисление длины списка(по желанию запись в файл и чтение с файла)
|
||||
// Ханойские башни игра написать
|
||||
Binary file not shown.
@@ -0,0 +1,184 @@
|
||||
#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[])
|
||||
{
|
||||
Node *res = n;
|
||||
Node *tmp = malloc(sizeof(Node));
|
||||
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;
|
||||
}
|
||||
|
||||
while (n->next != NULL)
|
||||
n = n->next;
|
||||
|
||||
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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
printf("Deny malloc\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
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
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
+58
@@ -0,0 +1,58 @@
|
||||
#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");
|
||||
|
||||
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;
|
||||
}
|
||||
Executable
+106
@@ -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;
|
||||
}
|
||||
Executable
+20
@@ -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);
|
||||
Reference in New Issue
Block a user