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 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 19 deletions(-) (limited to 'src/list.c') 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; +} -- cgit v1.2.3