From d62f358393ba80825bb340a96be789ba314e3960 Mon Sep 17 00:00:00 2001 From: aindros Date: Wed, 8 Jan 2020 22:11:47 +0100 Subject: src --- src/list.c | 34 ++++++++++++++++++++++++++++++++++ src/list.h | 16 ++++++++++++++++ src/main.c | 19 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/list.c create mode 100644 src/list.h create mode 100644 src/main.c diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..561b308 --- /dev/null +++ b/src/list.c @@ -0,0 +1,34 @@ +#include +#include "list.h" + +/* + * Creates a new list. + */ +List* List_Create(void) +{ + List* list = malloc(sizeof(List)); + + list->next = NULL; /* nwxt element is empty */ + list->current = list; /* points to itself */ + list->first = list; /* first element is itself */ + list->data = NULL; /* data is empty */ + list->size = 0; + + return list; +} + +/* + * Adds a new element to the list. + */ +void List_Add(List* list, /* list where to add new element */ + void* data, /* data to add */ + int size_of) /* size of the data */ +{ + void* dest = malloc(size_of); /* allocates memory like data parameter */ + + memcpy(dest, data, size_of); /* copies data inside dest */ + list->data = dest; + + list->next = malloc(sizeof(List)); + list->size ++; +} diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..083e239 --- /dev/null +++ b/src/list.h @@ -0,0 +1,16 @@ +#ifndef _LIST_H_ +#define _LIST_H_ + +typedef struct list_t { + void* data; + struct List* next; + struct List* first; + struct List* current; + int size; +} List; + +List* List_Create(void); +void List_Add (List*, void*, int); +void List_Remove(); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..9361ae6 --- /dev/null +++ b/src/main.c @@ -0,0 +1,19 @@ +#include +#include "list.h" + +int main(int argc, char** argv) +{ + int test1 = 5; + int test2 = 32; + + List* list = List_Create(); + + List_Add(list, &test1, sizeof(int)); + List_Add(list, &test2, sizeof(int)); + + int* t = list->data; + + printf("Valore: %i, size: %i\n", *t, list->size); + + return 0; +} -- cgit v1.2.3