mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 01:00:15 +00:00
meow
This commit is contained in:
+195
-119
@@ -1,140 +1,216 @@
|
||||
#include "head.h"
|
||||
|
||||
#include "prot.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
void linked_print(LinkList* l) {
|
||||
while (l != NULL) {
|
||||
printf("%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
LinkList* add_tail(LinkList* l, LinkList node) {
|
||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||
l->x = node.x;
|
||||
memcpy(l->name, node.name, LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkList* res = l;
|
||||
while (l->next != NULL) l = l->next;
|
||||
|
||||
LinkList* tmp = malloc(sizeof(LinkList));
|
||||
tmp->x = node.x;
|
||||
memcpy(tmp->name, node.name, LEN);
|
||||
tmp->prev = l;
|
||||
l->next = tmp;
|
||||
return res;
|
||||
}
|
||||
|
||||
LinkList* add_head(LinkList* l, LinkList node) {
|
||||
if (l->x == 0 && !strcmp(l->name, "\0")) {
|
||||
l->x = node.x;
|
||||
memcpy(l->name, node.name, LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkList* tmp = malloc(sizeof(LinkList));
|
||||
void pr(node *tmp)
|
||||
{
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
l->prev = tmp;
|
||||
{
|
||||
printf("[%p]\n", tmp);
|
||||
return;
|
||||
}
|
||||
while (tmp)
|
||||
{
|
||||
printf("[%14p]<-[%14p]->[%14p] ", tmp->prev, tmp, tmp->next);
|
||||
printf("data: [%4ld] s[%s]\n", tmp->data, tmp->s);
|
||||
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->x = node.x;
|
||||
memcpy(tmp->name, node.name, LEN);
|
||||
tmp->data = hh;
|
||||
for (i = 0; i < N - 1; i++)
|
||||
tmp->s[i] = ((char)(65 + rand() % 25));
|
||||
tmp->s[N - 1] = '\0';
|
||||
if (head == NULL)
|
||||
tmp->next = NULL;
|
||||
else
|
||||
{
|
||||
tmp->next = head;
|
||||
head->prev = tmp;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
LinkList* del_head(LinkList* l) {
|
||||
if (l == NULL) return NULL;
|
||||
if (l->next == NULL) {
|
||||
free(l);
|
||||
node *del_head(node *head)
|
||||
{
|
||||
node *tmp = head;
|
||||
if (head == NULL)
|
||||
return NULL;
|
||||
if (head->next == NULL)
|
||||
{
|
||||
free(head);
|
||||
return NULL;
|
||||
}
|
||||
LinkList* tmp = l->next;
|
||||
tmp->prev = NULL;
|
||||
free(l);
|
||||
return tmp;
|
||||
head = head->next;
|
||||
head->prev = NULL;
|
||||
free(tmp);
|
||||
return head;
|
||||
}
|
||||
|
||||
void del_tail(LinkList* l) {
|
||||
if (l == NULL) return;
|
||||
else if (l->next == NULL) {
|
||||
free(l);
|
||||
l = NULL;
|
||||
return;
|
||||
}
|
||||
while (l->next != NULL) l = l->next;
|
||||
LinkList* tmp = l->prev;
|
||||
l->prev = NULL;
|
||||
free(l);
|
||||
tmp->next = NULL;
|
||||
}
|
||||
|
||||
int len(LinkList* l) {
|
||||
int length(node *list)
|
||||
{
|
||||
node *tmp = list;
|
||||
int res = 0;
|
||||
if (l->x == 0 && !strcmp(l->name, "\0"))
|
||||
if (list == NULL)
|
||||
return 0;
|
||||
|
||||
while (l != NULL) {
|
||||
res++;
|
||||
l = l->next;
|
||||
if (list->data == 0 && !strcmp(list->s, "\0"))
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
while (tmp->prev != NULL)
|
||||
tmp = tmp->prev;
|
||||
while (tmp != NULL)
|
||||
{
|
||||
res++;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
LinkList get(LinkList* l, int n) {
|
||||
if (n > len(l)) exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
l = l->next;
|
||||
node *adv_add_head(node *head, int hh, char string[])
|
||||
{
|
||||
node *tmp = NULL;
|
||||
if ((tmp = malloc(sizeof(node))) == NULL)
|
||||
{
|
||||
perror("malloc: NULL");
|
||||
exit(2);
|
||||
}
|
||||
return *l;
|
||||
tmp->prev = NULL;
|
||||
tmp->data = hh;
|
||||
memcpy(tmp->s, string, N);
|
||||
if (head == NULL)
|
||||
tmp->next = NULL;
|
||||
else
|
||||
{
|
||||
tmp->next = head;
|
||||
head->prev = tmp;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
void swap(node *a, node *b)
|
||||
{
|
||||
node *tmp = NULL;
|
||||
if ((tmp = malloc(sizeof(node))) == NULL)
|
||||
exit(1);
|
||||
tmp->data = a->data;
|
||||
a->data = b->data;
|
||||
b->data = tmp->data;
|
||||
memcpy(tmp->s, a->s, N);
|
||||
memcpy(a->s, b->s, N);
|
||||
memcpy(b->s, tmp->s, N);
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp) {
|
||||
if (n == 1) {
|
||||
finish = add_head(finish, get(start, 0));
|
||||
start = del_head(start);
|
||||
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)->data < get(head, i)->data)
|
||||
swap(get(head, i), get(head, j));
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
node *printextra(node *head, int len, int i)
|
||||
{
|
||||
if (len < i || len == 0)
|
||||
{
|
||||
printf("*********************************|");
|
||||
return head;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[%14p] data: [%4ld] s[%s]|", head, head->data, head->s);
|
||||
return head->prev;
|
||||
}
|
||||
}
|
||||
node *end(node *head)
|
||||
{
|
||||
node *tmp = NULL;
|
||||
tmp = head;
|
||||
if (head == NULL)
|
||||
return head;
|
||||
while (tmp->next != NULL)
|
||||
tmp = tmp->next;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void printftof(node *head1, node *head2, node *head3)
|
||||
{
|
||||
int a = length(head1), b = length(head2), c = length(head3), maximum, i;
|
||||
maximum = max(a, b);
|
||||
maximum = max(maximum, c);
|
||||
head1 = end(head1);
|
||||
head2 = end(head2);
|
||||
head3 = end(head3);
|
||||
for (i = maximum; i > 0; i--)
|
||||
{
|
||||
head1 = printextra(head1, a, i);
|
||||
head2 = printextra(head2, b, i);
|
||||
head3 = printextra(head3, c, i);
|
||||
printf("\n");
|
||||
}
|
||||
printf("----------------------------------------------------------------------------------------------------------\n");
|
||||
printf("Tower 1 |Tower 2 |Tower 3 |\n");
|
||||
printf("----------------------------------------------------------------------------------------------------------\n\n");
|
||||
}
|
||||
int max(int a, int b)
|
||||
{
|
||||
if (a < b)
|
||||
return b;
|
||||
else
|
||||
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)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
printf("No linked list");
|
||||
exit(6);
|
||||
}
|
||||
else if (n == 1)
|
||||
{
|
||||
if (length(*head_to) == 0)
|
||||
printftof(*tower1, *tower2, *tower3);
|
||||
*head_to = adv_add_head(*head_to, (*head_from)->data, (*head_from)->s);
|
||||
*head_from = del_head(*head_from);
|
||||
if (length(*head_from) == 0)
|
||||
printftof(*tower1, *tower2, *tower3);
|
||||
return;
|
||||
}
|
||||
|
||||
tower_of_hanoi(n - 1, start, tmp, finish);
|
||||
|
||||
finish = add_head(finish, get(start, 0));
|
||||
start = del_head(start);
|
||||
|
||||
tower_of_hanoi(n - 1, tmp, finish, start);
|
||||
|
||||
printf("\nPosition:\nStart:\n");
|
||||
linked_print(start);
|
||||
printf("Finish:\n");
|
||||
linked_print(finish);
|
||||
printf("Tmp:\n");
|
||||
linked_print(tmp);
|
||||
}
|
||||
|
||||
void linked_free(LinkList* l) {
|
||||
while (l->next != NULL) l = l->next;
|
||||
|
||||
while (l->prev != NULL) {
|
||||
LinkList* tmp = l;
|
||||
l = l->prev;
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
hanoi_tower(n - 1, head_from, head_aux, head_to, tower1, tower2, tower3);
|
||||
printftof(*tower1, *tower2, *tower3);
|
||||
*head_to = adv_add_head(*head_to, (*head_from)->data, (*head_from)->s);
|
||||
*head_from = del_head(*head_from);
|
||||
printftof(*tower1, *tower2, *tower3);
|
||||
hanoi_tower(n - 1, head_aux, head_to, head_from, tower1, tower2, tower3);
|
||||
}
|
||||
Reference in New Issue
Block a user