aboutsummaryrefslogtreecommitdiff
path: root/src/list.c
diff options
context:
space:
mode:
authoraindros <aindros@hotmail.com>2020-01-09 01:09:15 +0100
committeraindros <aindros@hotmail.com>2020-01-09 01:09:15 +0100
commit5492194eee5955738a747184ea98cc3db3d74d40 (patch)
tree854d2ba2bec03e74bffe1bac49e67a85e3a75d23 /src/list.c
parent4fff4e6f37f781f7fdb9e6b433524bc61d1ce600 (diff)
downloadutils-5492194eee5955738a747184ea98cc3db3d74d40.tar.gz
utils-5492194eee5955738a747184ea98cc3db3d74d40.zip
first version, missing some methods
Diffstat (limited to 'src/list.c')
-rw-r--r--src/list.c60
1 files changed, 41 insertions, 19 deletions
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 <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;
+}