Commit c4811ff5 authored by Chris Müller's avatar Chris Müller

Add datastructures Stack and Queue. Add <stdlib.h> for malloc, calloc, realloc for src/standard.h.

parent 61fa4734
......@@ -5,6 +5,7 @@ set(VERSION "0.1")
set(SOURCES
matrix.c
unittest.c
structures/single_linked_list.c
)
set(HEADER
......@@ -12,6 +13,8 @@ set(HEADER
unix_colors.h
matrix.h
unittest.h
structures/stack.h
structures/queue.h
)
add_library(${TARGET_NAME} SHARED ${SOURCES})
......
#ifndef CRYSTAL_STANDARD_H
#define CRYSTAL_STANDARD_H
#include <stdlib.h>
#define crydefine__tuple(type, T) \
typedef struct T { \
return_code code; \
......@@ -26,4 +28,6 @@ crydefine__tuple(float, tuple_float);
#define cry_realloc(Type, Pointer, num) realloc(Pointer, sizeof(Pointer) * num)
#define cry_free(Pointer) free(Pointer)
#define cry_cast(Type, Var) ((Type)Var)
#endif // CRYSTAL_STANDARD_H
#ifndef CRYSTAL_STRUCTURES_QUEUE_H
#define CRYSTAL_STRUCTURES_QUEUE_H
#include "structures.h"
struct CryQueue;
struct CryQueue* cry_queue_new(void);
void cry_queue_free(struct CryQueue* queue, cry_free_handler handler);
int cry_queue_size(struct CryQueue* queue);
pointer cry_queue_front(struct CryQueue* queue);
pointer cry_queue_dequeue(struct CryQueue* queue);
void cry_queue_enqueue(struct CryQueue* queue, pointer data);
#endif // CRYSTAL_STRUCTURES_QUEUE_H
#include "queue.h"
#include "stack.h"
#include <assert.h>
struct Node {
struct Node* next;
pointer data;
};
struct Stack {
struct Node* begin;
};
struct Queue {
struct Node* front;
struct Node* tail;
};
struct CryQueue*
cry_queue_new(void)
{
struct Queue* list = cry_malloc(struct Queue*);
list->front = 0;
list->tail = 0;
return cry_cast(struct CryQueue*, list);
}
void
cry_queue_free(struct CryQueue* queue, cry_free_handler handler)
{
assert(queue != 0);
struct Node* node = cry_cast(struct Queue*, queue)->front;
struct Node* tmp = 0;
while(node != 0) {
tmp = node;
node = node->next;
if(handler != 0) {
handler(tmp->data);
}
cry_free(node);
}
cry_free(queue);
}
int
cry_queue_size(struct CryQueue* queue)
{
assert(queue != 0);
size_t length = 0;
struct Node* node = cry_cast(struct Queue*, queue)->front;
while(node != 0) {
node = node->next;
++length;
}
return length;
}
pointer
cry_queue_front(struct CryQueue* queue)
{
assert(queue != 0);
if(cry_cast(struct Queue*, queue)->front == 0)
return 0;
else
return cry_cast(struct Queue*, queue)->front->data;
}
pointer
cry_queue_dequeue(struct CryQueue* queue)
{
assert(queue != 0);
struct Queue* list = cry_cast(struct Queue*, queue);
struct Node* front = list->front;
pointer data = 0;
if(front == 0)
return data;
data = front->data;
if(list->front == list->tail)
list->front = list->tail = 0;
else
list->front = front->next;
cry_free(front);
return data;
}
void
cry_queue_enqueue(struct CryQueue* queue, pointer data)
{
assert(queue != 0);
struct Queue* list = cry_cast(struct Queue*, queue);
struct Node* node = cry_malloc(struct Node);
node->next = 0;
node->data = data;
if(list->front == 0) {
list->front = node;
list->tail = node;
} else {
list->tail->next = node;
list->tail = node;
}
}
struct CryStack*
cry_stack_new(void)
{
struct Stack* stack = cry_malloc(struct Stack);
stack->begin = 0;
return cry_cast(struct CryStack*, stack);
}
void
cry_stack_free(struct CryStack* stack, cry_free_handler handler)
{
assert(stack != 0);
struct Node* node = cry_cast(struct Stack*, stack)->begin;
struct Node* tmp = 0;
while(node != 0) {
tmp = node;
node = node->next;
if(handler != 0) {
handler(tmp->data);
}
cry_free(node);
}
cry_free(stack);
}
size_t
cry_stack_size(struct CryStack* stack)
{
assert(stack != 0);
size_t length = 0;
struct Node* node = cry_cast(struct Stack*, stack)->begin;
while(node != 0) {
node = node->next;
++length;
}
return length;
}
pointer
cry_stack_top(struct CryStack* stack)
{
assert(stack != 0);
if(cry_cast(struct Stack*, stack)->begin == 0)
return 0;
else
return cry_cast(struct Stack*, stack)->begin->data;
}
pointer
cry_stack_pop(struct CryStack* stack)
{
assert(stack != 0);
struct Stack* list = cry_cast(struct Stack*, stack);
struct Node* node = list->begin;
if(node == 0)
return 0;
pointer data = node->data;
list->begin = node->next;
cry_free(node);
return data;
}
void
cry_stack_push(struct CryStack* stack, pointer data)
{
assert(stack != 0);
struct Stack* list = cry_cast(struct Stack*, stack);
struct Node* node = cry_malloc(struct Node);
node->next = list->begin;
node->data = data;
list->begin = node;
}
#ifndef CRYSTAL_STRUCTURES_STACK_H
#define CRYSTAL_STRUCTURES_STACK_H
#include "structures.h"
struct CryStack;
struct CryStack* cry_stack_new(void);
void cry_stack_free(struct CryStack* stack, cry_free_handler handler);
size_t cry_stack_size(struct CryStack* stack);
pointer cry_stack_top(struct CryStack* stack);
pointer cry_stack_pop(struct CryStack* stack);
void cry_stack_push(struct CryStack* stack, pointer data);
#endif // CRYSTAL_STRUCTURES_STACK_H
#ifndef CRYSTAL_STRUCTURES_H
#define CRYSTAL_STRUCTURES_H
#include <standard.h>
typedef void (*cry_free_handler)(pointer data);
#endif // CRYSTAL_STRUCTURES_H
......@@ -2,6 +2,7 @@
#define CRYSTAL_UNITTEST_H
#include "standard.h"
#include <assert.h>
typedef void (*cry_modulechecker)(const_pointer data);
......
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