Hanoi added

This commit is contained in:
ada-dmitry
2023-04-18 08:28:26 +03:00
parent ea4d0db0f1
commit 62d99ba675
7 changed files with 203 additions and 1 deletions
Binary file not shown.
+140
View File
@@ -0,0 +1,140 @@
#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, LinkedList 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;
}
LinkedList* res = l;
while (l->next != NULL) l = l->next;
LinkedList* tmp = malloc(sizeof(LinkedList));
tmp->x = node.x;
memcpy(tmp->name, node.name, LEN);
tmp->prev = l;
l->next = tmp;
return res;
}
LinkedList* add_head(LinkedList* l, LinkedList 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;
}
LinkedList* tmp = malloc(sizeof(LinkedList));
if (tmp == NULL)
exit(1);
tmp->next = l;
l->prev = tmp;
tmp->prev = NULL;
tmp->x = node.x;
memcpy(tmp->name, node.name, LEN);
return tmp;
}
LinkedList* del_head(LinkedList* l) {
if (l == NULL) return NULL;
if (l->next == NULL) {
free(l);
return NULL;
}
LinkedList* tmp = l->next;
tmp->prev = NULL;
free(l);
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;
if (l->x == 0 && !strcmp(l->name, "\0"))
return 0;
while (l != NULL) {
res++;
l = l->next;
}
return res;
}
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 tower_of_hanoi(int n, LinkedList* start, LinkedList* finish, LinkedList* tmp) {
if (n == 1) {
finish = add_head(finish, get(start, 0));
start = del_head(start);
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(LinkedList* l) {
while (l->next != NULL) l = l->next;
while (l->prev != NULL) {
LinkedList* tmp = l;
l = l->prev;
free(tmp);
}
}
+38
View File
@@ -0,0 +1,38 @@
#include "head.h"
int main(int argc, char* argv[]) {
srand(time(0));
LinkedList* l1 = malloc(sizeof(LinkedList));
LinkedList* l2 = malloc(sizeof(LinkedList));
LinkedList* l3 = malloc(sizeof(LinkedList));
if (l1 == NULL || l2 == NULL || l3 == NULL)
exit(1);
int i, j;
char s[LEN];
for (i = 0; i < N; i++) {
for (j = 0; j < LEN - 1; j++)
s[j] = 'a' + rand() % 25;
s[j + 1] = '\0';
LinkedList l;
l.x = i;
memcpy(l.name, s, LEN);
l.next = NULL;
l.prev = NULL;
l1 = add_tail(l1, l);
}
tower_of_hanoi(N, l1, l3, l2);
// printf("Done:\n");
linked_free(l1);
linked_free(l2);
linked_free(l3);
return 0;
}
+24
View File
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 3
#define LEN 8
typedef struct LinkedList {
int x;
char name[LEN];
struct LinkedList* next;
struct LinkedList* prev;
} LinkedList;
void linked_print(LinkedList* l);
LinkedList* add_tail(LinkedList* l, LinkedList node);
LinkedList* add_head(LinkedList* l, LinkedList node);
LinkedList* del_head(LinkedList* l);
void del_tail(LinkedList* l);
int len(LinkedList* l);
LinkedList get(LinkedList* l, int n);
void tower_of_hanoi(int n, LinkedList* start, LinkedList* finish, LinkedList* tmp);
void linked_free(LinkedList* l);
View File
+1 -1
View File
@@ -10,7 +10,7 @@ int main(int argc, char* argv[]) {
printf("could not malloc\n"); printf("could not malloc\n");
exit(2); exit(2);
} }
printf("Error here 1");
int i; int i;
for (i = 0; i < COUNT; i++) { for (i = 0; i < COUNT; i++) {
char s[LEN]; char s[LEN];
Binary file not shown.