Skip to content
Snippets Groups Projects
Commit f7904dff authored by thib's avatar thib
Browse files

almost working(?)

parent 8bd5929f
No related branches found
No related tags found
No related merge requests found
cc=gcc
CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak
LFLAGS= -lm
main: class.x
./class.x
class.x: class.o main.o
$(cc) -o $@ $^ $(CFLAGS) $(LFLAGS)
clean:
rm -f *.o *.x
#include "class.h"
#include <stdio.h>
#include <stdlib.h>
node init_node(double value) {
node n;
n.value = value;
n.child[0] = NULL;
n.child[1] = NULL;
return n;
}
element *createElement(double value) {
element *tmp = calloc(1, sizeof(element));
tmp->next = NULL;
tmp->nd = init_node(value);
return tmp;
}
void swap(double *a, double *b) {
double tmp = *a;
*a = *b;
*b = tmp;
}
// bubble sort
void sort(double *arr, int size) {
for (int j = size; j > 1; j--) {
for (int i = 0; i < j - 1; i++) {
if (arr[i] < arr[i + 1]) {
swap(&arr[i], &arr[i + 1]);
}
}
}
}
// add at first place
element *push(element *head, double value) {
element *tmp = createElement(value);
tmp->next = head;
return tmp;
}
void list_print(element *head) {
element *tmp = head;
while (tmp != NULL) {
printf("%.2f ", tmp->nd.value);
tmp = tmp->next;
}
printf("\n");
}
element *createSortedList(double *values, int len) {
sort(values, len);
element *head = NULL; // empty
for (int i = 0; i < len; i++) {
head = push(head, values[i]);
}
list_print(head);
return head;
}
int list_length(element *head) {
if (head == NULL) {
return -1;
}
int size = 0;
element *current = head;
while (current != NULL) {
size++;
current = current->next;
}
return size;
}
void print_tree(node* tree, int depth) { // affiche l'arbre ordre croissant
if (tree == NULL) {
return;
}
print_tree(tree->child[0], depth + 1);
for (int i = 0; i < depth; i++) {
printf("-------");
}
printf(" %.2f \n\n", tree->value);
print_tree(tree->child[1], depth + 1);
}
element *step(element *head, element *current) {
if (current->next != NULL) {
node nd1 = current->nd;
node nd2 = current->next->nd;
double ave = (nd1.value + nd2.value) / 2;
current->nd = init_node(ave);
current->nd.child[0] = &nd1;
current->nd.child[1] = &nd2;
// remove next
current->next = current->next->next;
return current->next;
} else {
element* tmp=head;
while(tmp->next!=current){
tmp=tmp->next;
}
node nd1 = tmp->nd;
node nd2 = current->nd;
double ave = (nd1.value + nd2.value) / 2;
tmp->nd = init_node(ave);
tmp->nd.child[0] = &nd1;
tmp->nd.child[1] = &nd2;
// remove next
tmp->next=NULL;
return tmp;
}
}
tree classify(element *head) {
element*current=head;
while (list_length(head) > 1) {
current=step(head,current);
list_print(head);
}
// print_tree(&head->nd, 0);
return &head->nd;
}
\ No newline at end of file
#ifndef _CLASS_H_
#define _CLASS_H_
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
double value;
struct _node *child[2];
} node;
typedef struct element {
node nd ;
struct element *next;
} element;
typedef node* tree;
tree classify(element *head);
element* createSortedList(double* values, int len);
#endif
#include "class.h"
#include <stdlib.h>
int main() {
double arr[]={1.8,3.0,7.5,8.1,9.8};
element* head=createSortedList(arr, 5);
classify(head);
return EXIT_SUCCESS;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment