Skip to content

DilemaFixer/Stryktyrki

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Stryktyrki ๐Ÿš€

Build Status Version License Language

A lightweight and efficient C library for structured data processing and manipulation.

๐Ÿ“ฅ Installation

Clone Complete Repository

git clone https://github.com/DilemaFixer/Stryktyrki.git
cd Stryktyrki

Download Individual Components

Linked List

curl -O https://raw.githubusercontent.com/DilemaFixer/Arr/main/arr.h
curl -O https://raw.githubusercontent.com/DilemaFixer/Arr/main/arr.c

curl -O https://raw.githubusercontent.com/DilemaFixer/Stryktyrki/main/linked_list.h
curl -O https://raw.githubusercontent.com/DilemaFixer/Stryktyrki/main/linked_list.c

Memory Stack

curl -O https://raw.githubusercontent.com/DilemaFixer/Stryktyrki/main/mem_stack.h
curl -O https://raw.githubusercontent.com/DilemaFixer/Stryktyrki/main/mem_stack.c

Queue

curl -O https://raw.githubusercontent.com/DilemaFixer/Stryktyrki/main/queue.h
curl -O https://raw.githubusercontent.com/DilemaFixer/Stryktyrki/main/queue.c

Stack

curl -O https://raw.githubusercontent.com/DilemaFixer/Stryktyrki/main/stack.h
curl -O https://raw.githubusercontent.com/DilemaFixer/Stryktyrki/main/stack.c

๐Ÿ”ง API and Usage Examples

๐Ÿ“Š Data Structures

Linked List (l_list)

A singly linked list implementation for efficient node-based sequential data storage.

Core Structures
typedef struct node_t {
    struct node_t* next; // Pointer to the next node in the list
} node_t;

typedef struct l_list {
    node_t* head;    // First node in the list
    size_t count;    // Number of nodes in the list
    void (*free_custom_node)(node_t *node); // Custom node freeing function
} l_list;
Functions
l_list *new_empty_linked_list();
// Creates a new empty linked list

l_list *new_linked_list_with_node(node_t *node);
// Creates a linked list with an initial node

void free_linked_list(l_list *list);
// Frees all resources associated with the linked list

void linked_list_add_at(l_list *list, node_t *node, size_t index);
// Adds a node at the specified index

void linked_list_add_first(l_list *list, node_t *node);
// Adds a node at the beginning of the list

void linked_list_add_last(l_list *list, node_t *node);
// Adds a node at the end of the list

void linked_list_remove_at(l_list *list, size_t index);
// Removes the node at the specified index

void linked_list_remove_first(l_list *list);
// Removes the first node in the list

void linked_list_remove_last(l_list *list);
// Removes the last node in the list

void* find_at_linked_list(l_list *list, void* (*selector)(size_t *i, node_t *node, void *context), void *context);
// Finds a node using a selector function

arr_t* find_all_at_linked_list(l_list *list, void* (*selector)(size_t *i, node_t *node, void *context), void *context);
// Finds all nodes matching a criteria using a selector function
Usage Example
#include "linked_list.h"
#include "logger.h"
#include <stdio.h>

// Example of a custom node type
typedef struct my_node {
    node_t base;      // Must be first to allow casting to node_t
    int value;
} my_node;

// Custom node freeing function
void free_my_node(node_t *node) {
    if (node) {
        free(node);
    }
}

// Selector function to find a node with a specific value
void* find_node_by_value(size_t *i, node_t *node, void *context) {
    my_node *custom_node = (my_node*)node;
    int target_value = *((int*)context);
    
    if (custom_node->value == target_value) {
        return custom_node;
    }
    return NULL;
}

void example() {
    // Create a linked list
    l_list *list = new_empty_linked_list();
    list->free_custom_node = free_my_node;
    
    // Create and add some nodes
    for (int i = 0; i < 5; i++) {
        my_node *node = malloc(sizeof(my_node));
        node->base.next = NULL;
        node->value = i * 10;
        linked_list_add_last(list, (node_t*)node);
    }
    
    // Find a node with value 20
    int search_value = 20;
    my_node *found = find_at_linked_list(list, find_node_by_value, &search_value);
    if (found) {
        printf("Found node with value: %d\n", found->value);
    }
    
    // Clean up
    free_linked_list(list);
}

Memory Stack (mem_stack)

A stack-based memory management structure for efficient fixed-size allocations.

Core Structures
typedef struct mem_stack {
    void *mem;       // Base memory pointer
    size_t capacity; // Total capacity in bytes
    void* sp;        // Stack pointer (current position)
} mem_stack;
Functions
mem_stack *new_mem_stack(size_t capacity);
// Creates a new memory stack with specified capacity in bytes

void free_mem_stack(mem_stack *stack);
// Frees all resources associated with the memory stack

void mem_stack_resize(mem_stack *stack, size_t plus_size);
// Resizes the memory stack by adding the specified additional size

void mem_stack_push(mem_stack *stack, void *item, size_t size);
// Pushes data of specified size onto the stack

void *mem_stack_pop(mem_stack *stack, size_t size);
// Pops data of specified size from the stack

void *mem_stack_peek(mem_stack *stack, size_t size);
// Peeks at data of specified size from the stack without removing it

bool is_mem_stack_empty(mem_stack *stack);
// Checks if the memory stack is empty
Usage Example
#include "mem_stack.h"
#include <stdio.h>

void example() {
    // Create a memory stack with 1024 bytes capacity
    mem_stack *stack = new_mem_stack(1024);
    
    // Push integers onto the stack
    int values[] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
        mem_stack_push(stack, &values[i], sizeof(int));
    }
    
    // Peek at the top integer without removing it
    int *peeked = mem_stack_peek(stack, sizeof(int));
    printf("Top value (peek): %d\n", *peeked);
    
    // Pop integers from the stack in reverse order
    while (!is_mem_stack_empty(stack)) {
        int *popped = mem_stack_pop(stack, sizeof(int));
        printf("Popped value: %d\n", *popped);
    }
    
    // Clean up
    free_mem_stack(stack);
}

Queue (queue_t)

A circular buffer-based queue implementation for FIFO (First-In-First-Out) data operations.

Core Structures
typedef struct queue_t {
   void **q;        // Array of void pointers for storing elements
   size_t count;    // Number of elements currently in the queue
   size_t capacity; // Total capacity of the queue
   size_t front;    // Index of the front element
   size_t rear;     // Index where the next element will be inserted
} queue_t;
Functions
queue_t *new_queue(size_t capacity);
// Creates a new queue with specified capacity

void free_queue(queue_t *queue);
// Frees all resources associated with the queue

void resize_queue(queue_t *queue);
// Resizes the queue to double its current capacity

void enqueue(queue_t *queue, void *item);
// Adds an item to the back of the queue

void *dequeue(queue_t *queue);
// Removes and returns the item from the front of the queue

void *peek(queue_t *queue);
// Returns the item at the front of the queue without removing it

bool is_queue_empty(queue_t *queue);
// Checks if the queue is empty

size_t queue_size(queue_t *queue);
// Returns the number of elements in the queue
Usage Example
#include "queue.h"
#include <stdio.h>

void example() {
    // Create a queue with capacity for 10 elements
    queue_t *queue = new_queue(10);
    
    // Create and enqueue some integer elements (must be heap-allocated for persistence)
    for (int i = 0; i < 5; i++) {
        int *value = malloc(sizeof(int));
        *value = i * 10;
        enqueue(queue, value);
    }
    
    // Check the front of the queue without removing it
    int *front = peek(queue);
    if (front) {
        printf("Front of queue: %d\n", *front);
    }
    
    // Dequeue and process elements in FIFO order
    while (!is_queue_empty(queue)) {
        int *value = dequeue(queue);
        printf("Dequeued: %d\n", *value);
        free(value);
    }
    
    // Clean up
    free_queue(queue);
}

Array Stack (arr_stack)

A stack implementation backed by a dynamic array for LIFO (Last-In-First-Out) operations.

Core Structures
typedef struct arr_stack {
    void **items;   // Array of void pointers for storing elements
    size_t capacity; // Total capacity of the stack
    size_t count;    // Current number of elements in the stack
} arr_stack;
Functions
arr_stack *new_arr_stack(size_t capacity);
// Creates a new array stack with specified capacity

void free_arr_stack(arr_stack *stack);
// Frees all resources associated with the stack

void arr_stack_resize(arr_stack *stack);
// Resizes the stack to double its current capacity

void arr_stack_push(arr_stack *stack, void *item);
// Pushes an item onto the top of the stack

void *arr_stack_pop(arr_stack *stack);
// Removes and returns the item from the top of the stack

void *arr_stack_peek(arr_stack *stack);
// Returns the item at the top of the stack without removing it

bool is_arr_stack_empty(arr_stack *stack);
// Checks if the stack is empty
Usage Example
#include "stack.h"
#include <stdio.h>

void example() {
    // Create a stack with capacity for 8 elements
    arr_stack *stack = new_arr_stack(8);
    
    // Create and push some integer elements (must be heap-allocated for persistence)
    for (int i = 0; i < 5; i++) {
        int *value = malloc(sizeof(int));
        *value = i * 10;
        arr_stack_push(stack, value);
    }
    
    // Peek at the top element without removing it
    int *top = arr_stack_peek(stack);
    if (top) {
        printf("Top of stack: %d\n", *top);
    }
    
    // Pop and process elements in LIFO order
    while (!is_arr_stack_empty(stack)) {
        int *value = arr_stack_pop(stack);
        printf("Popped: %d\n", *value);
        free(value);
    }
    
    // Clean up
    free_arr_stack(stack);
}

๐Ÿ“š Dependencies

  • Arr - Dynamic array implementation
  • CSL - C string list utilities

About

A lightweight and efficient C library for structured data processing and manipulation.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages