Commit b46d39d0 authored by Chris Müller's avatar Chris Müller
Browse files

Add files for implementing Double-linked Lists.

parent 1c19f77f
......@@ -6,6 +6,7 @@ set(SOURCES
matrix.c
unittest.c
structures/single_linked_list.c
structures/double_linked_list.c
)
set(HEADER
......@@ -15,6 +16,7 @@ set(HEADER
unittest.h
structures/stack.h
structures/queue.h
structures/list.h
)
add_library(${TARGET_NAME} SHARED ${SOURCES})
......
#include "list.h"
#include "standard.h"
#include <assert.h>
struct Node {
struct Node* next;
struct Node* prev;
pointer data;
};
struct CryList {
struct Node* head;
struct Node* tail;
};
struct CryList*
cry_list_new(void)
{
struct CryList* list = cry_malloc(struct CryList);
list->head = 0;
list->tail = 0;
return list;
}
void
cry_list_free(struct CryList* list, cry_free_handler handler)
{
assert(list != 0);
struct Node* node = list->head;
struct Node* tmp = 0;
while(node != 0) {
tmp = node;
node = node->next;
if(handler != 0)
handler(tmp->data);
cry_free(tmp);
}
}
size_t
cry_list_size(struct CryList* list)
{
assert(list != 0);
size_t length = 0;
struct Node* node = list->head;
while(node != 0) {
node = node->next;
++length;
}
return length;
}
pointer
cry_list_head(struct CryList* list)
{
assert(list != 0);
return list->head;
}
pointer
cry_list_tail(struct CryList* list)
{
assert(list != 0);
return list->tail;
}
#ifndef CRYSTAL_STRUCTURES_LIST_H
#define CRYSTAL_STRUCTURES_LIST_H
#include "structures.h"
struct CryList;
struct CryList* cry_list_new(void);
void cry_list_free(struct CryList* list, cry_free_handler handler);
size_t cry_list_size(struct CryList* list);
pointer cry_list_head(struct CryList* list);
pointer cry_list_tail(struct CryList* list);
#endif // CRYSTAL_STRUCTURES_LIST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment