diff options
author | 2020-01-09 01:09:15 +0100 | |
---|---|---|
committer | 2020-01-09 01:09:15 +0100 | |
commit | 5492194eee5955738a747184ea98cc3db3d74d40 (patch) | |
tree | 854d2ba2bec03e74bffe1bac49e67a85e3a75d23 /src | |
parent | 4fff4e6f37f781f7fdb9e6b433524bc61d1ce600 (diff) | |
download | utils-5492194eee5955738a747184ea98cc3db3d74d40.tar.gz utils-5492194eee5955738a747184ea98cc3db3d74d40.zip |
first version, missing some methods
Diffstat (limited to 'src')
-rw-r--r-- | src/list.c | 60 | ||||
-rw-r--r-- | src/list.h | 23 | ||||
-rw-r--r-- | src/main.c | 29 |
3 files changed, 79 insertions, 33 deletions
@@ -1,34 +1,56 @@ #include <stdlib.h> +#include <string.h> /* 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; +} @@ -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 @@ -1,19 +1,34 @@ #include <stdio.h> +#include <stdlib.h> +#include <string.h> #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; } |