start creating base objects for runtime
This commit is contained in:
100
src/runtime/internals/list/list.c
Normal file
100
src/runtime/internals/list/list.c
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "list.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../../../memory.h"
|
||||
|
||||
LinkedList *create_list(size_t data_size) {
|
||||
LinkedList *list = checked_malloc(sizeof(LinkedList));
|
||||
list->head = NULL;
|
||||
list->data_size = data_size;
|
||||
list->length = 0;
|
||||
return list;
|
||||
}
|
||||
|
||||
void append(LinkedList *list, void *element) {
|
||||
Node *new_node = checked_malloc(sizeof(Node));
|
||||
new_node->data = checked_malloc(list->data_size);
|
||||
memcpy(new_node->data, element, list->data_size);
|
||||
new_node->next = NULL;
|
||||
|
||||
if (!list->head) {
|
||||
list->head = new_node;
|
||||
} else {
|
||||
Node *temp = list->head;
|
||||
while (temp->next)
|
||||
temp = temp->next;
|
||||
temp->next = new_node;
|
||||
}
|
||||
list->length++;
|
||||
}
|
||||
|
||||
void *get_element_at(LinkedList *list, size_t index) {
|
||||
if (index >= list->length)
|
||||
return NULL;
|
||||
|
||||
Node *current = list->head;
|
||||
for (size_t i = 0; i < index; ++i)
|
||||
current = current->next;
|
||||
|
||||
return current->data;
|
||||
}
|
||||
|
||||
int set_element_at(LinkedList *list, size_t index, void *element) {
|
||||
if (index >= list->length)
|
||||
return 0;
|
||||
|
||||
Node *current = list->head;
|
||||
for (size_t i = 0; i < index; ++i)
|
||||
current = current->next;
|
||||
|
||||
memcpy(current->data, element, list->data_size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int remove_at(LinkedList *list, size_t index) {
|
||||
if (index >= list->length)
|
||||
return 0;
|
||||
|
||||
Node *temp = list->head;
|
||||
Node *prev = NULL;
|
||||
|
||||
for (size_t i = 0; i < index; ++i) {
|
||||
prev = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
if (prev)
|
||||
prev->next = temp->next;
|
||||
else
|
||||
list->head = temp->next;
|
||||
|
||||
free(temp->data);
|
||||
free(temp);
|
||||
list->length--;
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t list_length(LinkedList *list) { return list->length; }
|
||||
|
||||
void print_list(LinkedList *list, void (*print_func)(void *)) {
|
||||
Node *current = list->head;
|
||||
while (current) {
|
||||
print_func(current->data);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
void free_list(LinkedList *list, void (*free_data)(void *)) {
|
||||
Node *current = list->head;
|
||||
while (current) {
|
||||
Node *next = current->next;
|
||||
|
||||
if (free_data) // Safe to pass NULL if you don't need it
|
||||
free_data(current->data);
|
||||
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
free(list);
|
||||
}
|
||||
42
src/runtime/internals/list/list.h
Normal file
42
src/runtime/internals/list/list.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef LINKEDLIST_H
|
||||
#define LINKEDLIST_H
|
||||
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
// Node structure (opaque to user)
|
||||
typedef struct Node {
|
||||
void *data;
|
||||
struct Node *next;
|
||||
} Node;
|
||||
|
||||
typedef struct LinkedList {
|
||||
Node *head;
|
||||
size_t data_size;
|
||||
size_t length;
|
||||
} LinkedList;
|
||||
|
||||
// Create a new list for the given data type size
|
||||
LinkedList *create_list(size_t data_size);
|
||||
|
||||
// Append an element to the list
|
||||
void append(LinkedList *list, void *element);
|
||||
|
||||
// Get a pointer to the element at the given index
|
||||
void *get_element_at(LinkedList *list, size_t index);
|
||||
|
||||
// Set the element at the given index
|
||||
int set_element_at(LinkedList *list, size_t index, void *element);
|
||||
|
||||
// Remove the element at the given index
|
||||
int remove_at(LinkedList *list, size_t index);
|
||||
|
||||
// Get the number of elements in the list
|
||||
size_t list_length(LinkedList *list);
|
||||
|
||||
// Print the list using a provided print function
|
||||
void print_list(LinkedList *list, void (*print_func)(void *));
|
||||
|
||||
// Free all memory used by the list
|
||||
void free_list(LinkedList *list, void (*free_data)(void *));
|
||||
|
||||
#endif // LINKEDLIST_H
|
||||
Reference in New Issue
Block a user