1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2024-12-22 15:51:11 +00:00

Voilà, normalement, les listes fonctionnent

This commit is contained in:
Antoine Bartuccio 2018-06-05 15:52:48 +02:00
parent a72587d430
commit 6ab2711c9c
Signed by: klmp200
GPG Key ID: E7245548C53F904B
4 changed files with 149 additions and 13 deletions

View File

@ -35,16 +35,23 @@ void $_free__Element(THIS(Element)){
free(this);
}
Element *_init_element(void *data, size_t size, List *list){
Element *$_init_Element(void *data, size_t size, List *list){
Element *el = (Element*) malloc_or_die(sizeof(Element));
void *newData = malloc_or_die(sizeof(size));
void *new_data = NULL;
if (data != NULL) {
new_data = malloc_or_die(size);
memcpy(new_data, data, size);
}
memcpy(newData, data, size);
el->list = list;
el->data = newData;
el->data = new_data;
el->previous = NULL;
el->next = NULL;
if (el->list != NULL)
el->list->size ++;
LINK_ALL(Element, el,
get_next,
get_previous,

View File

@ -7,23 +7,120 @@ GETTER(List, Element*, head)
GETTER(List, Element*, tail)
GETTER(List, int, size)
Element * get_element_List(THIS(List), int index){
Element * current = NULL;
int i;
if (index < this->size){
if (index == this->size - 1)
current = this->tail;
else if (index == 0)
current = this->head;
else if (index <= (this->size - 1) / 2){
i = 0;
current = this->head;
while (i < index){
current = current->next;
i++;
}
} else {
i = this->size - 1;
current = this->tail;
while (i > index){
current = current->previous;
i--;
}
}
} else OUTSIDE_BOUNDS;
return current;
}
void * get_element_data_List(THIS(List), int index){
Element * el = this->get_element(this, index);
if (el != NULL)
return el->data;
return NULL;
}
void * get_head_data_List(THIS(List)){
if (this->head != NULL)
return this->head->data;
return NULL;
}
void * get_tail_data_List(THIS(List)){
if (this->tail != NULL)
return this->tail->data;
return NULL;
}
void $_free__List(THIS(List)){
this->clear(this);
free(this);
}
void clear_List(THIS(List)){
Element * el = this->head;
Element * el_tmp;
while (el != NULL){
el->list = NULL;
el_tmp = el;
el = el_tmp->next;
DELETE(el_tmp);
Element *current = this->head;
Element *to_delete = NULL;
while (current != NULL){
to_delete = current;
current = current->next;
to_delete->list = NULL;
DELETE(to_delete);
}
this->head = NULL;
this->tail = NULL;
this->size = 0;
}
void insert_tail_List(THIS(List), void * data, size_t data_size){
/* Create a new element */
Element *new_element = NEW(Element, data, data_size, this);
if (this->tail == NULL){
this->head = new_element;
this->tail = new_element;
} else {
new_element->previous = this->tail;
this->tail->next = new_element;
this->tail = new_element;
}
}
void insert_head_List(THIS(List), void * data, size_t data_size){
/* Create a new element */
Element *new_element = NEW(Element, data, data_size, this);
if (this->head != NULL)
this->head->previous = new_element;
else
this->tail = new_element;
new_element->next = this->head;
this->head = new_element;
}
void insert_inside_List(THIS(List), void * data, size_t data_size, int index){
Element *new_element = NULL;
Element *old_element = NULL;
if (index == 0)
this->insert_head(this, data, data_size);
else if (index == this->size)
this->insert_tail(this, data, data_size);
else if (index < this->size){
new_element = NEW(Element, data, data_size, this);
old_element = this->get_element(this, index);
new_element->previous = old_element->previous;
new_element->next = old_element;
old_element->previous->next = new_element;
old_element->previous = new_element;
} else OUTSIDE_BOUNDS;
}
void remove_head_List(THIS(List)){
DELETE(this->head);
}
@ -42,6 +139,13 @@ List *$_init_List(){
get_head,
get_tail,
get_size,
get_head_data,
get_tail_data,
get_element,
get_element_data,
insert_inside,
insert_tail,
insert_head,
clear,
remove_head,
remove_tail

View File

@ -11,7 +11,7 @@ typedef struct o_Element Element;
#include "Element.h"
#define WRONG_ELEMENT CRASH("This element is not within the list\n")
#define OUTSIDE_BOUNDS CRASH("You are outside of the list\n")
struct o_List {
PRIVATE Element *head;
@ -20,12 +20,22 @@ struct o_List {
PUBLIC Element *(*get_head)(_THIS(List));
PUBLIC Element *(*get_tail)(_THIS(List));
PUBLIC Element *(*get_element)(_THIS(List), int index);
PUBLIC void* (*get_head_data)(_THIS(List));
PUBLIC void* (*get_tail_data)(_THIS(List));
PUBLIC void* (*get_element_data)(_THIS(List), int index);
PUBLIC int (*get_size)(_THIS(List));
PUBLIC void (*insert_inside)(_THIS(List), void * data, size_t data_size, int index);
PUBLIC void (*insert_tail)(_THIS(List), void * data, size_t data_size);
PUBLIC void (*insert_head)(_THIS(List), void * data, size_t data_size);
PUBLIC void (*clear)(_THIS(List));
PUBLIC void (*remove_head)(_THIS(List));
PUBLIC void (*remove_tail)(_THIS(List));
DESTRUCTOR(List);
};

15
main.c
View File

@ -4,6 +4,21 @@
int main() {
List *l = NEW(List);
char text1[30] = "Patate";
char text2[30] = "Patator";
printf("La taille est de %d\n", l->get_size(l));
l->insert_tail(l, text1, sizeof(char) * 30);
l->insert_head(l, text2, sizeof(char) * 30);
l->insert_head(l, "Bite", sizeof(char) * 30);
printf("La taille est de %d\n", l->get_size(l));
printf("%s\n", (char *) l->get_head_data(l));
printf("%s\n", (char *) l->get_tail_data(l));
printf("%s\n", (char *) l->get_element_data(l, 0));
printf("%s\n", (char *) l->get_element_data(l, 1));
l->insert_inside(l, "Rigolo", sizeof(char) * 30, 2);
printf("%s\n", (char *) l->get_element_data(l, 1));
printf("%s\n", (char *) l->get_element_data(l, 3));
l->remove_tail(l);
printf("La taille est de %d\n", l->get_size(l));
DELETE(l);