mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 01:00:15 +00:00
resort
This commit is contained in:
Binary file not shown.
@@ -1,76 +0,0 @@
|
|||||||
#include "header.h"
|
|
||||||
|
|
||||||
void linked_print(LinkedList *l)
|
|
||||||
{
|
|
||||||
while (l != NULL)
|
|
||||||
{
|
|
||||||
printf("%lF\n", l->x);
|
|
||||||
l = l->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkedList *add_head(LinkedList *l, double x)
|
|
||||||
{
|
|
||||||
if (l == NULL)
|
|
||||||
{
|
|
||||||
l = malloc(sizeof(LinkedList));
|
|
||||||
if (l == NULL)
|
|
||||||
exit(1);
|
|
||||||
l->x = x;
|
|
||||||
l->next = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (l->x == 0.0)
|
|
||||||
{
|
|
||||||
l->x = x;
|
|
||||||
l->next = NULL;
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkedList *tmp = malloc(sizeof(LinkedList));
|
|
||||||
if (tmp == NULL)
|
|
||||||
exit(1);
|
|
||||||
|
|
||||||
tmp->next = l;
|
|
||||||
tmp->x = x;
|
|
||||||
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;
|
|
||||||
free(l);
|
|
||||||
l = NULL;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
int len(LinkedList *l)
|
|
||||||
{
|
|
||||||
int res = 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;
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
#include "header.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
srand(time(0));
|
|
||||||
LinkedList *numbers = malloc(sizeof(LinkedList));
|
|
||||||
if (numbers == NULL)
|
|
||||||
exit(1);
|
|
||||||
|
|
||||||
char c;
|
|
||||||
char flag = 'n';
|
|
||||||
|
|
||||||
while ((c = getchar()) != EOF)
|
|
||||||
{
|
|
||||||
if (c == '\n')
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (c == ' ')
|
|
||||||
{
|
|
||||||
flag = 's';
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (c)
|
|
||||||
{
|
|
||||||
case '0' ... '9':
|
|
||||||
if (flag == 's')
|
|
||||||
{
|
|
||||||
numbers = add_head(numbers, (double)(c - '0'));
|
|
||||||
flag = 'n';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
double a = get(numbers, 0).x;
|
|
||||||
numbers = del_head(numbers);
|
|
||||||
a = a * 10 + c - '0';
|
|
||||||
numbers = add_head(numbers, a);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '+':
|
|
||||||
case '-':
|
|
||||||
case '*':
|
|
||||||
case '/':
|
|
||||||
if (len(numbers) < 2)
|
|
||||||
{
|
|
||||||
printf("Invalid expression\n");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
double a = get(numbers, 0).x;
|
|
||||||
numbers = del_head(numbers);
|
|
||||||
double b = get(numbers, 0).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", get(numbers, 0).x);
|
|
||||||
|
|
||||||
free(numbers);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#define N 5
|
|
||||||
|
|
||||||
typedef struct LinkedList
|
|
||||||
{
|
|
||||||
double x;
|
|
||||||
struct LinkedList *next;
|
|
||||||
} LinkedList;
|
|
||||||
|
|
||||||
void linked_print(LinkedList *l);
|
|
||||||
LinkedList *add_head(LinkedList *l, double x);
|
|
||||||
LinkedList *del_head(LinkedList *l);
|
|
||||||
int len(LinkedList *l);
|
|
||||||
LinkedList get(LinkedList *l, int n);
|
|
||||||
Binary file not shown.
@@ -1,17 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
typedef struct node {
|
|
||||||
int x;
|
|
||||||
struct node* next;
|
|
||||||
}Plate;
|
|
||||||
|
|
||||||
void print_plates(Plate* plates);
|
|
||||||
Plate* push(Plate* plates, int x);
|
|
||||||
Plate* remove_plate(Plate* plates);
|
|
||||||
int pop(Plate** plates);
|
|
||||||
int len(Plate* plates);
|
|
||||||
int isnm(char *str);
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
#include "head.h"
|
|
||||||
|
|
||||||
void print_plates(Plate *plates)
|
|
||||||
{
|
|
||||||
if (plates == NULL)
|
|
||||||
{
|
|
||||||
printf("[%p]\n", plates);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
while(plates){
|
|
||||||
printf("[%14p]->[%14p] ", plates, plates->next);
|
|
||||||
printf("data: [%4d]\n", plates->x);
|
|
||||||
plates = plates->next;
|
|
||||||
}
|
|
||||||
printf("-------------\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
Plate* push(Plate* plates, int d){
|
|
||||||
Plate *temp = NULL;
|
|
||||||
if((temp=malloc(sizeof(Plate)))==NULL){
|
|
||||||
perror("malloc:NULL");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
temp->x = d;
|
|
||||||
if(plates==NULL) temp->next=NULL;
|
|
||||||
else{
|
|
||||||
temp->next=plates;
|
|
||||||
}
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
Plate* del_head(Plate* plates){
|
|
||||||
Plate *temp=plates;
|
|
||||||
if(plates==NULL) return NULL;
|
|
||||||
if(plates->next==NULL){
|
|
||||||
free(plates);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
plates = plates->next;
|
|
||||||
free(temp);
|
|
||||||
return plates;
|
|
||||||
}
|
|
||||||
|
|
||||||
int len(Plate* plates){
|
|
||||||
int ans = 0;
|
|
||||||
Plate *temp = plates;
|
|
||||||
if(plates==NULL) return 0;
|
|
||||||
while(temp){
|
|
||||||
ans++;
|
|
||||||
temp=temp->next;
|
|
||||||
}
|
|
||||||
return ans;
|
|
||||||
}
|
|
||||||
|
|
||||||
int pop(Plate** plates){
|
|
||||||
int i;
|
|
||||||
if(*plates==NULL){
|
|
||||||
printf("Error");
|
|
||||||
exit(4);
|
|
||||||
}
|
|
||||||
i = (*plates)->x;
|
|
||||||
*plates = del_head(*plates);
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
int isnm(char *str){
|
|
||||||
int i = 0, n = strlen(str);
|
|
||||||
for(i=0; i<n; i++){
|
|
||||||
if(!isdigit(str[i])){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
#include "head.h"
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
|
||||||
Plate *plates = NULL;
|
|
||||||
char c;
|
|
||||||
int i,j,k, ans = 0;
|
|
||||||
for (k=1; k < argc; k++){
|
|
||||||
if (isnm(argv[k])){
|
|
||||||
plates = push(plates, atoi(argv[k]));
|
|
||||||
}
|
|
||||||
else if (strlen(argv[k]) == 1 || !strcmp(argv[k], "*m")){
|
|
||||||
if (len(plates)<2){
|
|
||||||
printf("Not enough numbers");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
if (!strcmp(argv[k], "*m")) c = '*';
|
|
||||||
else c = *argv[k];
|
|
||||||
i = pop(&plates);
|
|
||||||
j = pop(&plates);
|
|
||||||
switch(c){
|
|
||||||
case '+':
|
|
||||||
plates = push(plates, j + i);
|
|
||||||
break;
|
|
||||||
case '-':
|
|
||||||
plates = push(plates, j - i);
|
|
||||||
break;
|
|
||||||
case '*':
|
|
||||||
plates = push(plates, j * i);
|
|
||||||
break;
|
|
||||||
case '/':
|
|
||||||
plates = push(plates, j / i);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("You wrote something wrong");
|
|
||||||
exit(3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
printf("You wrote something wrong");
|
|
||||||
exit(7);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (len(plates)!=1){
|
|
||||||
printf("Not enough operations");
|
|
||||||
exit(4);
|
|
||||||
}
|
|
||||||
ans = pop(&plates);
|
|
||||||
printf("Result of line is %d\n", ans);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Binary file not shown.
@@ -1,105 +0,0 @@
|
|||||||
#include "header.h"
|
|
||||||
|
|
||||||
void node_print(Node *n)
|
|
||||||
{
|
|
||||||
while (n != NULL)
|
|
||||||
{
|
|
||||||
printf("%s\n", n->name);
|
|
||||||
n = n->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Node *add_tail(Node *n, char s[])
|
|
||||||
{
|
|
||||||
if (n == NULL)
|
|
||||||
{
|
|
||||||
if ((n = malloc(sizeof(Node))) == 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;
|
|
||||||
}
|
|
||||||
Node *res = n;
|
|
||||||
while (n->next != NULL){
|
|
||||||
printf("%p\n", n);
|
|
||||||
n = n->next;
|
|
||||||
}
|
|
||||||
Node *tmp = malloc(sizeof(Node));
|
|
||||||
memcpy(tmp->name, s, LEN);
|
|
||||||
tmp->prev = 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 *xor_find(Node *n1, Node *n2)
|
|
||||||
{
|
|
||||||
int i, j, f = 0;
|
|
||||||
Node *res = NULL;
|
|
||||||
if ((res = malloc(sizeof(Node))) == NULL)
|
|
||||||
{
|
|
||||||
printf("could not malloc\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;
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
#include "header.h"
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
printf("Cto-Nibud\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("could not malloc\n");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
n1 = add_tail(n1, "bebebe");
|
|
||||||
|
|
||||||
printf("Error here 1\n");
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < COUNT; i++)
|
|
||||||
{
|
|
||||||
char s[LEN];
|
|
||||||
int j;
|
|
||||||
printf("sss\n");
|
|
||||||
for (j = 0; j < LEN - 1; j++)
|
|
||||||
s[j] = 'a' + rand() % 2;
|
|
||||||
s[LEN - 1] = '\0';
|
|
||||||
|
|
||||||
n1 = add_tail(n1, s);
|
|
||||||
printf("11");
|
|
||||||
for (j = 0; j < LEN - 1; j++){
|
|
||||||
s[j] = 'a' + rand() % 2;
|
|
||||||
printf("22");
|
|
||||||
}
|
|
||||||
s[LEN - 1] = '\0';
|
|
||||||
n2 = add_tail(n2, s);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Source arrays:\n");
|
|
||||||
node_print(n1);
|
|
||||||
printf("\n");
|
|
||||||
node_print(n2);
|
|
||||||
|
|
||||||
n3 = xor_find(n1, n2);
|
|
||||||
|
|
||||||
printf("\nFinal array:\n");
|
|
||||||
node_print(n3);
|
|
||||||
|
|
||||||
free(n1);
|
|
||||||
free(n2);
|
|
||||||
free(n3);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#define COUNT 3
|
|
||||||
#define LEN 3
|
|
||||||
|
|
||||||
typedef struct Node
|
|
||||||
{
|
|
||||||
char name[LEN];
|
|
||||||
struct Node *next;
|
|
||||||
struct Node *prev;
|
|
||||||
} Node;
|
|
||||||
|
|
||||||
void node_print(Node *n);
|
|
||||||
Node *add_tail(Node *n, char s[]);
|
|
||||||
Node get(Node *node, int n);
|
|
||||||
Node *xor_find(Node *n1, Node *n2);
|
|
||||||
int len(Node *n);
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#include <math.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
double f(double x) { return exp(x) + sin(2.0 * x); }
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
srand(time(0));
|
|
||||||
|
|
||||||
double start = -4.0, finish = -3.0, e = 1e-6, mid = (finish + start) / 2.0;
|
|
||||||
|
|
||||||
if (f(start) * f(finish) > 0) {
|
|
||||||
printf("Could not find\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (fabs(f(mid)) >= e) {
|
|
||||||
mid = (finish + start) / 2.0;
|
|
||||||
if (f(mid) > 0) {
|
|
||||||
start = (f(start) > f(finish)) ? mid : start;
|
|
||||||
finish = (f(start) < f(finish)) ? mid : finish;
|
|
||||||
} else {
|
|
||||||
start = (f(start) < f(finish)) ? mid : start;
|
|
||||||
finish = (f(start) > f(finish)) ? mid : finish;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Found: %lF\n", start);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,219 +0,0 @@
|
|||||||
|
|
||||||
#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);
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
#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);
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user