From 5492194eee5955738a747184ea98cc3db3d74d40 Mon Sep 17 00:00:00 2001 From: aindros Date: Thu, 9 Jan 2020 01:09:15 +0100 Subject: first version, missing some methods --- src/list.c | 60 +++++++++++++++++++++++++++++++++++++++++------------------- src/list.h | 23 ++++++++++++++++------- src/main.c | 29 ++++++++++++++++++++++------- 3 files changed, 79 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/list.c b/src/list.c index 561b308..3edc82b 100644 --- a/src/list.c +++ b/src/list.c @@ -1,34 +1,56 @@ #include +#include /* memcpy */ #include "list.h" -/* - * Creates a new list. - */ -List* List_Create(void) +List List_Create() { - List* list = malloc(sizeof(List)); + List 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; + list.first = NULL; + list.last = NULL; + 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 List_Add(List* list, + void* data, + int size_of) { - void* dest = malloc(size_of); /* allocates memory like data parameter */ + struct list_item_t *item = malloc(sizeof(struct list_item_t)); + void *dest = malloc(size_of); /* allocates memory like data parameter */ memcpy(dest, data, size_of); /* copies data inside dest */ - list->data = dest; + item->data = dest; + item->next = NULL; - list->next = malloc(sizeof(List)); + if (list->first == NULL) + list->first = item; + + if (list->last != NULL) + list->last->next = item; + + list->last = item; list->size ++; } + +Iterator List_Iterator(List* list) +{ + struct iterator_t *iterator = malloc(sizeof(struct iterator_t)); + iterator->current = list->first; + + return *iterator; +} + +int Iterator_HasNext(Iterator* iterator) +{ + return iterator->current != NULL; +} + +List_Item *Iterator_Next(Iterator *iterator) +{ + struct list_item_t *current = iterator->current; + iterator->current = iterator->current->next; + + return current; +} diff --git a/src/list.h b/src/list.h index 083e239..b621f06 100644 --- a/src/list.h +++ b/src/list.h @@ -1,16 +1,25 @@ #ifndef _LIST_H_ #define _LIST_H_ +typedef struct list_item_t { + void *data; + struct list_item_t *next; +} List_Item; + typedef struct list_t { - void* data; - struct List* next; - struct List* first; - struct List* current; + struct list_item_t *first; + struct list_item_t *last; int size; } List; -List* List_Create(void); -void List_Add (List*, void*, int); -void List_Remove(); +typedef struct iterator_t { + struct list_item_t *current; +} Iterator; + +List List_Create (void); +void List_Add (List *, void *, int); +Iterator List_Iterator (List *); +int Iterator_HasNext(Iterator *); +List_Item *Iterator_Next (Iterator *); #endif diff --git a/src/main.c b/src/main.c index 9361ae6..b7d4671 100644 --- a/src/main.c +++ b/src/main.c @@ -1,19 +1,34 @@ #include +#include +#include #include "list.h" int main(int argc, char** argv) { - int test1 = 5; - int test2 = 32; + int i; + int *t; + List_Item *current; + char buffer[4]; + char *dest = malloc(sizeof(char) * 4); - List* list = List_Create(); + Iterator it; - List_Add(list, &test1, sizeof(int)); - List_Add(list, &test2, sizeof(int)); + List list = List_Create(); - int* t = list->data; + for (i = 0; i < 23; i++) { + itoa(i, buffer, 10); + strcpy(dest, "i"); + strcat(dest, buffer); + List_Add(&list, dest, sizeof(int)); + } - printf("Valore: %i, size: %i\n", *t, list->size); + printf("read from list with iterator:\n"); + + it = List_Iterator(&list); + while(Iterator_HasNext(&it)) { + current = Iterator_Next(&it); + printf("%s\n", current->data); + } return 0; } -- cgit v1.2.3