calk added

This commit is contained in:
ada-dmitry
2023-04-24 22:40:49 +03:00
parent 62d99ba675
commit 7fe48f9bc4
8 changed files with 418 additions and 0 deletions
+219
View File
@@ -0,0 +1,219 @@
#include "lib.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]->[%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->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;
}
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;
}
int length(node *list)
{
node *tmp = list;
int res = 0;
if (list == NULL)
return 0;
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;
}
node *adv_add_head(node *head, int hh, char string[])
{
node *tmp = NULL;
if ((tmp = malloc(sizeof(node))) == NULL)
{
perror("malloc: NULL");
exit(2);
}
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);
}
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;
}
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){
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(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), maximum, i;
maximum = max(max(a,b), 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");
}
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 = adv_add_head(*head_to, (*head_from)->data, (*head_from)->s);
*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 = adv_add_head(*head_to, (*head_from)->data, (*head_from)->s);
*head_from = del_head(*head_from);
printftof(adresses);
hanoi_tower(n - 1, head_aux, head_to, head_from, adresses);
}
+27
View File
@@ -0,0 +1,27 @@
#define N 10
typedef struct Node {
long int data;
struct Node *next;
struct Node *prev;
char s[N];
} 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);
node *adv_add_head(node *head, int hh, char string[]);
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, pointer* adresses);
void printftof(pointer* adresses);
node *end(node *head);
node* printextra(node *head, int len, int i);
+29
View File
@@ -0,0 +1,29 @@
#include "lib.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()%100)) < 75) 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;
}