mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
ht2.0
This commit is contained in:
Binary file not shown.
@@ -5,7 +5,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void pr(node *tmp)
|
void pr(tower *tmp)
|
||||||
{
|
{
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
{
|
{
|
||||||
@@ -20,11 +20,11 @@ void pr(node *tmp)
|
|||||||
}
|
}
|
||||||
printf("-------------\n");
|
printf("-------------\n");
|
||||||
}
|
}
|
||||||
node *add_head(node *head, int hh)
|
tower *add_head(tower *crown, int hh)
|
||||||
{
|
{
|
||||||
node *tmp = NULL;
|
tower *tmp = NULL;
|
||||||
int i;
|
int i;
|
||||||
if ((tmp = malloc(sizeof(node))) == NULL)
|
if ((tmp = malloc(sizeof(tower))) == NULL)
|
||||||
{
|
{
|
||||||
perror("malloc: NULL");
|
perror("malloc: NULL");
|
||||||
exit(2);
|
exit(2);
|
||||||
@@ -34,34 +34,34 @@ node *add_head(node *head, int hh)
|
|||||||
for (i = 0; i < N - 1; i++)
|
for (i = 0; i < N - 1; i++)
|
||||||
tmp->s[i] = ((char)(65 + rand() % 25));
|
tmp->s[i] = ((char)(65 + rand() % 25));
|
||||||
tmp->s[N - 1] = '\0';
|
tmp->s[N - 1] = '\0';
|
||||||
if (head == NULL)
|
if (crown == NULL)
|
||||||
tmp->next = NULL;
|
tmp->next = NULL;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tmp->next = head;
|
tmp->next = crown;
|
||||||
head->prev = tmp;
|
crown->prev = tmp;
|
||||||
}
|
}
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
node *del_head(node *head)
|
tower *del_head(tower *crown)
|
||||||
{
|
{
|
||||||
node *tmp = head;
|
tower *tmp = crown;
|
||||||
if (head == NULL)
|
if (crown == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (head->next == NULL)
|
if (crown->next == NULL)
|
||||||
{
|
{
|
||||||
free(head);
|
free(crown);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
head = head->next;
|
crown = crown->next;
|
||||||
head->prev = NULL;
|
crown->prev = NULL;
|
||||||
free(tmp);
|
free(tmp);
|
||||||
return head;
|
return crown;
|
||||||
}
|
}
|
||||||
|
|
||||||
int length(node *list)
|
int length(tower *list)
|
||||||
{
|
{
|
||||||
node *tmp = list;
|
tower *tmp = list;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -80,10 +80,10 @@ int length(node *list)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
node *adv_add_head(node *head, int hh, char string[])
|
tower *adv_add_head(tower *crown, int hh, char string[])
|
||||||
{
|
{
|
||||||
node *tmp = NULL;
|
tower *tmp = NULL;
|
||||||
if ((tmp = malloc(sizeof(node))) == NULL)
|
if ((tmp = malloc(sizeof(tower))) == NULL)
|
||||||
{
|
{
|
||||||
perror("malloc: NULL");
|
perror("malloc: NULL");
|
||||||
exit(2);
|
exit(2);
|
||||||
@@ -91,19 +91,19 @@ node *adv_add_head(node *head, int hh, char string[])
|
|||||||
tmp->prev = NULL;
|
tmp->prev = NULL;
|
||||||
tmp->data = hh;
|
tmp->data = hh;
|
||||||
memcpy(tmp->s, string, N);
|
memcpy(tmp->s, string, N);
|
||||||
if (head == NULL)
|
if (crown == NULL)
|
||||||
tmp->next = NULL;
|
tmp->next = NULL;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
tmp->next = head;
|
tmp->next = crown;
|
||||||
head->prev = tmp;
|
crown->prev = tmp;
|
||||||
}
|
}
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
void swap(node *a, node *b)
|
void swap(tower *a, tower *b)
|
||||||
{
|
{
|
||||||
node *tmp = NULL;
|
tower *tmp = NULL;
|
||||||
if ((tmp = malloc(sizeof(node))) == NULL)
|
if ((tmp = malloc(sizeof(tower))) == NULL)
|
||||||
exit(1);
|
exit(1);
|
||||||
tmp->data = a->data;
|
tmp->data = a->data;
|
||||||
a->data = b->data;
|
a->data = b->data;
|
||||||
@@ -114,7 +114,7 @@ void swap(node *a, node *b)
|
|||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
node *get(node *list, int n)
|
tower *get(tower *list, int n)
|
||||||
{
|
{
|
||||||
if (n >= length(list))
|
if (n >= length(list))
|
||||||
exit(3);
|
exit(3);
|
||||||
@@ -124,46 +124,46 @@ node *get(node *list, int n)
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
node *sort(node *head)
|
tower *sort(tower *crown)
|
||||||
{
|
{
|
||||||
if (head == NULL || head->next == NULL)
|
if (crown == NULL || crown->next == NULL)
|
||||||
return head;
|
return crown;
|
||||||
int i, j, n = length(head);
|
int i, j, n = length(crown);
|
||||||
for (i = 0; i < n - 1; i++)
|
for (i = 0; i < n - 1; i++)
|
||||||
{
|
{
|
||||||
for (j = i + 1; j < n; j++)
|
for (j = i + 1; j < n; j++)
|
||||||
{
|
{
|
||||||
if (get(head, j)->data < get(head, i)->data)
|
if (get(crown, j)->data < get(crown, i)->data)
|
||||||
swap(get(head, i), get(head, j));
|
swap(get(crown, i), get(crown, j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return head;
|
return crown;
|
||||||
}
|
}
|
||||||
node *printextra(node *head, int len, int i)
|
tower *printextra(tower *crown, int len, int i)
|
||||||
{
|
{
|
||||||
if (len < i || len == 0)
|
if (len < i || len == 0)
|
||||||
{
|
{
|
||||||
printf("*********************************|");
|
printf("*********************************|");
|
||||||
return head;
|
return crown;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("[%14p] data: [%4ld] s[%s]|", head, head->data, head->s);
|
printf("[%14p] data: [%4ld] s[%s]|", crown, crown->data, crown->s);
|
||||||
return head->prev;
|
return crown->prev;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
node *end(node *head)
|
tower *end(tower *crown)
|
||||||
{
|
{
|
||||||
node *tmp = NULL;
|
tower *tmp = NULL;
|
||||||
tmp = head;
|
tmp = crown;
|
||||||
if (head == NULL)
|
if (crown == NULL)
|
||||||
return head;
|
return crown;
|
||||||
while (tmp->next != NULL)
|
while (tmp->next != NULL)
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printftof(node *head1, node *head2, node *head3)
|
void printftof(tower *head1, tower *head2, tower *head3)
|
||||||
{
|
{
|
||||||
int a = length(head1), b = length(head2), c = length(head3), maximum, i;
|
int a = length(head1), b = length(head2), c = length(head3), maximum, i;
|
||||||
maximum = max(a, b);
|
maximum = max(a, b);
|
||||||
@@ -190,7 +190,7 @@ int max(int a, int b)
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, node **const tower1, node **const tower2, node **const tower3)
|
void hanoi_tower(int n, tower **head_from, tower **head_to, tower **head_aux, tower **const tower1, tower **const tower2, tower **const tower3)
|
||||||
{
|
{
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,22 +7,22 @@
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
node *head = NULL, *head2 = NULL, *head3 = NULL;
|
tower *crown1 = NULL, *crown2 = NULL, *crown3 = NULL;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
printf("Generated linked list (method of head)\n");
|
printf("Generated linked list (method of crown1)\n");
|
||||||
while ((r = (rand() % 100)) < 75)
|
while ((r = (rand() % 100)) < 75)
|
||||||
head = add_head(head, r);
|
crown1 = add_head(crown1, r);
|
||||||
pr(head);
|
pr(crown1);
|
||||||
|
|
||||||
printf("Length of linked list is: %d\n", length(head));
|
printf("Length of linked list is: %d\n", length(crown1));
|
||||||
|
|
||||||
printf("Sorted linked list\n");
|
printf("Sorted linked list\n");
|
||||||
sort(head);
|
sort(crown1);
|
||||||
pr(head);
|
pr(crown1);
|
||||||
|
|
||||||
hanoi_tower(length(head), &head, &head3, &head2, &head, &head2, &head3);
|
hanoi_tower(length(crown1), &crown1, &crown3, &crown2, &crown1, &crown2, &crown3);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -6,18 +6,18 @@ typedef struct Node
|
|||||||
struct Node *next;
|
struct Node *next;
|
||||||
struct Node *prev;
|
struct Node *prev;
|
||||||
char s[N];
|
char s[N];
|
||||||
} node;
|
} tower;
|
||||||
|
|
||||||
void pr(node *tmp);
|
void pr(tower *tmp);
|
||||||
node *add_head(node *head, int hh);
|
tower *add_head(tower *crown, int hh);
|
||||||
node *del_head(node *head);
|
tower *del_head(tower *crown);
|
||||||
int length(node *list);
|
int length(tower *list);
|
||||||
node *sort(node *head);
|
tower *sort(tower *crown);
|
||||||
node *get(node *list, int n);
|
tower *get(tower *list, int n);
|
||||||
void swap(node *a, node *b);
|
void swap(tower *a, tower *b);
|
||||||
node *adv_add_head(node *head, int hh, char string[]);
|
tower *adv_add_head(tower *crown, int hh, char string[]);
|
||||||
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, node **const tower1, node **const tower2, node **const tower3);
|
void hanoi_tower(int n, tower **head_from, tower **head_to, tower **head_aux, tower **const tower1, tower **const tower2, tower **const tower3);
|
||||||
void printftof(node *head1, node *head2, node *head3);
|
void printftof(tower *head1, tower *head2, tower *head3);
|
||||||
node *end(node *head);
|
tower *end(tower *crown);
|
||||||
node *printextra(node *head, int len, int i);
|
tower *printextra(tower *crown, int len, int i);
|
||||||
int max(int a, int b);
|
int max(int a, int b);
|
||||||
Reference in New Issue
Block a user