first commit

This commit is contained in:
2026-03-10 19:01:36 -05:00
commit 81e262d1df
11 changed files with 800 additions and 0 deletions

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

85
Makefile Normal file
View File

@@ -0,0 +1,85 @@
# Compiler Flags
CC := gcc
CFLAGS := -g -Wall -Wextra -Werror -pedantic -fno-omit-frame-pointer
export ASAN_OPTIONS = allocator_may_return_null=1
# Directory variables
LIBDIR := lib
OBJ := obj
INC := include
SRC := src
TEST := tests
# Filepath Pattern Matching
LIB := $(LIBDIR)/lib.a
SRCS := $(wildcard $(SRC)/*.c)
OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS))
TESTS := $(wildcard $(TEST)/*.c)
TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS))
# Commands must be labeled PHONY
.PHONY: all release clean test
# Compiler Release Flags
release: CFLAGS := -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer -O2 -DNDEBUG
release: clean $(LIB)
# Target for compilation.
all: $(LIB)
# Target / Dependencies
$(LIB): $(OBJS) | $(LIBDIR)
$(RM) $(LIB)
ar -cvrs $@ $^
$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJ)/%.o: $(SRC)/%.c | $(OBJ)
$(CC) $(CFLAGS) -c $< -o $@
$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin
$(CC) $(CFLAGS) $< $(LIB) -o $@
# Make directories if none.
$(LIBDIR):
mkdir $@
$(INC):
mkdir $@
$(OBJ):
mkdir $@
$(TEST)/bin:
mkdir $@
# Run the tests in the bin folder and track results
test: $(LIB) $(TESTBINS)
@SUCCESS=0; FAILURE=0; \
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'; \
for t in $(TESTBINS); do \
NAME=$$(basename $$t); \
START=$$(date +%s%N); \
if $$t; then \
RET=0; \
else \
RET=$$?; \
fi; \
END=$$(date +%s%N); \
ELAPSED_NS=$$((END - START)); \
ELAPSED_MS=$$((ELAPSED_NS / 1000000)); \
if [ $$RET -eq 0 ]; then \
printf "%-20s %bPASS%b (%b%4d ms%b)\n" "$$NAME" "$$GREEN" "$$NC" "$$YELLOW" "$$ELAPSED_MS" "$$NC"; \
SUCCESS=$$((SUCCESS + 1)); \
else \
printf "%-20s %bFAIL%b (%b%4d ms%b)\n" "$$NAME" "$$RED" "$$NC" "$$YELLOW" "$$ELAPSED_MS" "$$NC"; \
FAILURE=$$((FAILURE + 1)); \
fi; \
done; \
printf "\nTests completed\n"; \
printf "SUCCESS: %b%d%b\n" "$$GREEN" "$$SUCCESS" "$$NC"; \
printf "FAILURE: %b%d%b\n" "$$RED" "$$FAILURE" "$$NC"; \
test $$FAILURE -eq 0
clean:
$(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/

40
README.md Normal file
View File

@@ -0,0 +1,40 @@
# link
## Description
Linked List Implementation written in C.
## Table of Contents
- [Description](#description)
- [Features](#features)
- [Usage](#usage)
- [Credits / Resources](#credits--resources)
- [License](#license)
## Features / TODOS
- [x] Linked List
-[x] Insertion
-[x] Deletion
-[x] Search
- [x] Stack
- [x] Push
- [x] Pop
- [x] Queue
- [x] Enqueue
- [x] Dequeue
- [ ] Priority Queue
- [ ] Better Testing
## Usage
## Credits / Resources
[Tom Preston-Werner README Driven Development](https://tom.preston-werner.com/2010/08/23/readme-driven-development)<br>
[Make a README](https://www.makeareadme.com/)<br>
[Choose a LICENSE](https://choosealicense.com/)<br>
[Engineer Man - Singly Linked Lists and Doubly Linked lists implementation in C](https://www.youtube.com/watch?v=RCHGco2NvMk)<br>
[Jacob Sorber](https://www.youtube.com/watch?v=VOpjAHCee7c)<br>
## License
This project is licensed under MIT - see the [LICENSE](LICENSE) file for details.

105
include/except.h Normal file
View File

@@ -0,0 +1,105 @@
#ifndef EXCEPT_INCLUDED
#define EXCEPT_INCLUDED
#include <setjmp.h>
struct Exception {
const char *reason;
};
typedef struct Exception Exception;
struct Except_Frame {
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const struct Exception *exception;
};
typedef struct Except_Frame Except_Frame;
enum { EXCEPT_STATE_ENTERED=0,
EXCEPT_STATE_RAISED,
EXCEPT_STATE_HANDLED,
EXCEPT_STATE_FINALIZED,
EXCEPT_STATE_COUNT};
extern struct Except_Frame *except_stack;
extern const struct Exception assertion_failed;
void except_raise(const Exception *e, const char *file, int line);
#undef assert
#ifdef NDEBUG
#define assert(e) ((void)0)
#else
extern void asserted(int e);
#define assert(e) ((void)((e)||(RAISE(assertion_failed),0)))
#endif
#ifdef WIN32
#include <windows.h>
extern DWORD except_index;
extern void except_init(void);
extern void except_push(Except_Frame *fp);
extern void except_pop(void);
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
#define RERAISE except_raise(except_frame.exception, \
except_frame.file, except_frame.line)
#define RETURN switch (except_pop(),0) default: return
#define TRY do { \
volatile int except_flag; \
Except_Frame except_frame; \
if (except_index == TLS_OUT_OF_INDEXES) \
except_init(); \
except_push(&except_frame); \
except_flag = setjmp(except_frame.env); \
if (except_flag == EXCEPT_STATE_ENTERED) {
#define EXCEPT(e) \
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} else if (except_frame.exception == &(e)) { \
except_flag = EXCEPT_STATE_HANDLED;
#define ELSE \
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} else { \
except_flag = EXCEPT_STATE_HANDLED;
#define FINALLY \
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} { \
if (except_flag == EXCEPT_STATE_ENTERED) \
except_flag = EXCEPT_STATE_FINALIZED;
#define END_TRY \
if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
} while (0)
#else
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
#define RERAISE except_raise(except_frame.exception, \
except_frame.file, except_frame.line)
#define RETURN switch (except_stack = except_stack->prev,0) default: return
#define TRY do { \
volatile int except_flag; \
Except_Frame except_frame; \
except_frame.prev = except_stack; \
except_stack = &except_frame; \
except_flag = setjmp(except_frame.env); \
if (except_flag == EXCEPT_STATE_ENTERED) {
#define EXCEPT(e) \
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} else if (except_frame.exception == &(e)) { \
except_flag = EXCEPT_STATE_HANDLED;
#define ELSE \
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} else { \
except_flag = EXCEPT_STATE_HANDLED;
#define FINALLY \
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} { \
if (except_flag == EXCEPT_STATE_ENTERED) \
except_flag = EXCEPT_STATE_FINALIZED;
#define END_TRY \
if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \
} if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \
} while (0)
#endif
#endif // except.h

44
include/link.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef LINK_INCLUDED
#define LINK_INCLUDED
#include <stdbool.h>
#include <stdint.h>
struct Node {
uint64_t id; // For Example
struct Node *next; // Link
};
typedef struct Node Node;
// Syntactic Sugar
typedef struct Node * List;
typedef struct Node * Stack;
typedef struct Node * Queue;
// Alloc a new node, can RAISE oom Exception.
extern struct Node *node_alloc( uint64_t id );
// Callback function prototype
typedef void (*Callback)(Node *);
extern void list_for_each(List *l, Callback callback);
extern void list_free(List *l);
extern void list_print(List *l);
// CRUD by Index. 0 is the beginning, -1 will be treated as the end.
extern bool list_insert_at(List *l, struct Node *n, int index);
extern uint64_t list_delete_at(List *l, int index);
extern uint64_t list_search_at(List *l, int index);
// Read and Delete by ID(or whatever).
extern bool list_search_id(List *l, uint64_t id);
extern bool list_delete_id(List *l, uint64_t id);
// Stacks
extern bool stack_push(Stack *s, struct Node *n);
extern uint64_t stack_pop(Stack *s);
// Queues
extern bool q_enqueue(Queue *q, struct Node *n);
extern uint64_t q_dequeue(Queue *q);
#endif

31
include/mem.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef MEM_INCLUDED
#define MEM_INCLUDED
#include "except.h" // Exceptions
#include <stddef.h> // size_t
/* General Macros*/
#define MEM_KB(x) ((size_t)(x) * 1024ULL)
#define MEM_MB(x) ((size_t)(x) * 1024ULL * 1024ULL)
#define MEM_GB(x) ((size_t)(x) * 1024ULL * 1024ULL * 1024ULL)
#define MEM_SIZE(x) (ptrdiff_t)sizeof(x)
#define MEM_COUNT(a) (MEM_SIZE(a) / MEM_SIZE(*(a)))
#define MEM_LEN(s) (MEM_COUNT(s) - 1)
extern const Exception oom; // Out of memory
extern void *mem_alloc (size_t nbytes,const char *file, int line);
extern void *mem_calloc(size_t count, size_t nbytes, const char *file, int line);
extern void mem_free(void *ptr, const char *file, int line);
extern void *mem_resize(void *ptr, size_t nbytes, const char *file, int line);
#define ALLOC(nbytes) mem_alloc((nbytes), __FILE__, __LINE__)
#define CALLOC(count, nbytes) mem_calloc((count), (nbytes), __FILE__, __LINE__)
#define NEW(p) ((p) = ALLOC((size_t)sizeof *(p)))
#define NEW0(p) ((p) = CALLOC(1, (size_t)sizeof *(p)))
#define FREE(ptr) ((void)(mem_free((ptr), __FILE__, __LINE__), (ptr) = 0))
#define RESIZE(ptr, nbytes) ((ptr) = mem_resize((ptr), (nbytes), __FILE__, __LINE__))
#endif

82
src/except.c Normal file
View File

@@ -0,0 +1,82 @@
#include <stdlib.h>
#include <stdio.h>
#include "../include/except.h"
struct Except_Frame *except_stack = NULL;
const struct Exception assertion_failed = { "Assertion failed" };
void asserted(int e){
if(!e){
RAISE(assertion_failed);
}
}
void except_raise(const struct Exception *e, const char *file,int line)
{
#ifdef WIN32
Except_Frame *p;
if (except_index == TLS_OUT_OF_INDEXES)
except_init();
p = TlsGetValue(except_index);
#else
struct Except_Frame *p = except_stack;
#endif
asserted(e != NULL);
if (p == NULL) {
fprintf(stderr, "Uncaught exception");
if (e->reason)
fprintf(stderr, " %s", e->reason);
else
fprintf(stderr, " at 0x%p", (void*)e);
if (file && line > 0)
fprintf(stderr, " raised at %s:%d\n", file, line);
fprintf(stderr, "aborting...\n");
fflush(stderr);
abort();
}
p->exception = e;
p->file = file;
p->line = line;
#ifdef WIN32
except_pop();
#else
except_stack = except_stack->prev;
#endif
longjmp(p->env, EXCEPT_STATE_RAISED);
}
#ifdef WIN32
_CRTIMP void __cdecl _assert(void *, void *, unsigned);
#undef assert
#define assert(e) ((e) || (_assert(#e, __FILE__, __LINE__), 0))
DWORD except_index = -1;
void except_init(void) {
BOOL cond;
except_index = TlsAlloc();
assert(except_index != TLS_OUT_OF_INDEXES);
cond = TlsSetValue(except_index, NULL);
assert(cond == TRUE);
}
void except_push(Except_Frame *fp) {
BOOL cond;
fp->prev = TlsGetValue(except_index);
cond = TlsSetValue(except_index, fp);
assert(cond == TRUE);
}
void except_pop(void) {
BOOL cond;
Except_Frame *tos = TlsGetValue(except_index);
cond = TlsSetValue(except_index, tos->prev);
assert(cond == TRUE);
}
#endif

252
src/link.c Normal file
View File

@@ -0,0 +1,252 @@
#include "../include/mem.h"
#include "../include/link.h"
#include <stdio.h>
// Alloc a new node, can RAISE oom Exception.
struct Node *node_alloc(uint64_t id){
// Calls mem_alloc, which can RAISE oom Exception.
struct Node *result = NEW(result);
// Populate ID and set link to NULL
result->id = id;
result->next = NULL;
return result;
}
static void node_free(struct Node *n){
FREE(n);
n = NULL;
}
static void node_print(struct Node *n) {
printf("Node:%p\tID:%ld\n", (void *)n, n->id);
}
void list_for_each(List *l, Callback callback){
// Iterate over the list and apply callback on each node
struct Node *cur = *l;
while (cur != NULL) {
callback(cur); // Call the callback function with the current node
cur = cur->next;
}
}
void list_free(List *l){
struct Node *cur = *l;
struct Node *temp = NULL;
// Traverse the list and free each node
while (cur != NULL) {
temp = cur;
cur = cur->next;
node_free(temp); // Free the current node
}
// Finally, set the head pointer to NULL
*l = NULL;
}
void list_print(List *l){
list_for_each(l, node_print);
}
// CRUD by Index. 0 is the beginning, -1 will be treated as the end.
bool list_insert_at(List *l, struct Node *n, int index){
// Handle empty list
if (*l == NULL) {
*l = n;
return true;
}
// If index is negative, treat it as "insert at the end"
if (index < 0) {
struct Node *cur = *l;
// Traverse to the end of the list
while (cur->next != NULL) {
cur = cur->next;
}
// Insert at the end
cur->next = n;
return true;
}
// Walk through list until index or end is reached
int i = 0;
struct Node *prev = NULL;
struct Node *cur = *l;
while (cur != NULL && i != index) {
++i;
prev = cur;
cur = cur->next;
}
// If index is 0, insert at the head
if (index == 0) {
*l = n;
n->next = cur;
return true;
}
// If we've reached the end of the list, insert at the end
if (cur == NULL) {
prev->next = n;
return true;
}
// Otherwise, insert in the middle
prev->next = n;
n->next = cur;
return true;
}
uint64_t list_delete_at(List *l, int index){
if (*l == NULL) {
return 0; // List is empty, cannot remove.
}
struct Node *prev = NULL;
struct Node *cur = *l;
int i = 0;
// Handle special case for removing last node
if (index < 0) {
// Traverse to the second-to-last node (if exists)
while (cur != NULL && cur->next != NULL) {
prev = cur;
cur = cur->next;
}
} else {
// Traverse to the specified index
while (cur != NULL && i < index) {
prev = cur;
cur = cur->next;
++i;
}
}
// If cur is NULL, the index is out of range
if (cur == NULL) {
return 0; // Index out of range
}
// Handle removal of the first node (index 0)
if (prev == NULL) {
*l = cur->next; // Update the head to the next node
} else {
prev->next = cur->next; // Skip the current node
}
uint64_t result = cur->id; // Get the id of deleted node.
node_free(cur); // Free the memory for the removed node
return result;
}
uint64_t list_search_at(List *l, int index){
if (*l == NULL) {
return 0; // List is empty, cannot remove.
}
struct Node *cur = *l;
int i = 0;
// Handle special case for removing last node
if (index >= -1) {
// Traverse to the second-to-last node (if exists)
while (cur != NULL) {
cur = cur->next;
}
} else {
// Traverse to the specified index
while (cur != NULL && i < index) {
cur = cur->next;
++i;
}
}
// If cur is NULL, the index is out of range
if (cur == NULL) {
return 0; // Index out of range
}
uint64_t result = cur->id; // Get the id of deleted node.
return result;
}
// Read and Delete by ID(or whatever).
bool list_search_id(List *l, uint64_t id){
struct Node *cur = *l;
// Traverse the list
while (cur != NULL) {
if (cur->id == id) {
return true; // Found the node with the matching id
}
cur = cur->next;
}
return false; // No node with the given id was found
}
bool list_delete_id(List *l, uint64_t id){
struct Node *prev = NULL;
struct Node *cur = *l;
// Seach the whole list.
while (cur != NULL && cur->id != id) {
prev = cur;
cur = cur->next;
}
// No match to delete.
if (cur == NULL) {
return false;
}
// Handle a first node
if (prev == NULL) {
if (cur->next == NULL) {
// only item?
*l = NULL;
} else {
// more items?
*l = cur->next;
}
node_free(cur);
return true;
}
// Handle a last node
if (cur->next == NULL) {
prev->next = NULL;
node_free(cur);
return true;
}
// Handle a middle node
if (prev != NULL && cur->next != NULL) {
prev->next = cur->next;
node_free(cur);
return true;
}
return false; // Default
}
// Stacks
bool stack_push(Stack *s, struct Node *n){
return list_insert_at((List *)s, n, 0);
}
uint64_t stack_pop(Stack *s){
return list_delete_at((List *)s, 0);
}
// Queues
bool q_enqueue(Queue *q, struct Node *n){
return list_insert_at((List *)q, n, -1);
}
uint64_t q_dequeue(Queue *q){
return list_delete_at((List *)q,-1);
}

62
src/mem.c Normal file
View File

@@ -0,0 +1,62 @@
#include <stdlib.h>
#include <stddef.h>
#include "../include/except.h"
#include "../include/mem.h"
const struct Exception oom = { "Out of memory!" };
void *mem_alloc(size_t nbytes, const char *file, int line)
{
void *ptr;
assert(nbytes > 0);
ptr = malloc(nbytes);
if (ptr == NULL)
{
if (file == NULL)
RAISE(oom);
else
except_raise(&oom, file, line);
}
return ptr;
}
void *mem_calloc(size_t count, size_t nbytes, const char *file, int line)
{
void *ptr;
assert(count > 0);
assert(nbytes > 0);
ptr = calloc(count, nbytes);
if (ptr == NULL)
{
if (file == NULL)
RAISE(oom);
else
except_raise(&oom, file, line);
}
return ptr;
}
void mem_free(void *ptr, const char *file, int line)
{
(void) file;
(void) line;
if (ptr)
free(ptr);
}
void *mem_resize(void *ptr, size_t nbytes, const char *file, int line)
{
assert(ptr); // ?
assert(nbytes > 0);// ?
void *tmp = realloc(ptr, nbytes);
if (tmp == NULL)
{
if (file == NULL)
RAISE(oom);
else
except_raise(&oom, file, line);
}
ptr = tmp;
return ptr;
}

39
tests/01_stack.c Normal file
View File

@@ -0,0 +1,39 @@
#define DEBUG
#include "../include/link.h"
#include <stdlib.h>
#include <stdio.h>
int main(void){
List s1 = NULL;
struct Node *n1 = node_alloc(1);
struct Node *n2 = node_alloc(2);
struct Node *n3 = node_alloc(3);
struct Node *n4 = node_alloc(4);
stack_push(&s1, n4);
stack_push(&s1, n3);
stack_push(&s1, n2);
stack_push(&s1, n1);
list_print(&s1);
uint64_t test1 = stack_pop(&s1);
printf("Removed %ld from end of list.\n", test1);
list_print(&s1);
uint64_t test2 = stack_pop(&s1);
printf("Removed %ld from end of list.\n", test2);
list_print(&s1);
uint64_t test3 = stack_pop(&s1);
printf("Removed %ld from end of list.\n", test3);
list_print(&s1);
uint64_t test4 = stack_pop(&s1);
printf("Removed %ld from end of list.\n", test4);
list_print(&s1);
list_free(&s1);
return EXIT_SUCCESS;
}

39
tests/02_queue.c Normal file
View File

@@ -0,0 +1,39 @@
#define DEBUG
#include "../include/link.h"
#include <stdlib.h>
#include <stdio.h>
int main(void){
List s1 = NULL;
struct Node *n1 = node_alloc(1);
struct Node *n2 = node_alloc(2);
struct Node *n3 = node_alloc(3);
struct Node *n4 = node_alloc(4);
list_insert_at(&s1, n4,-1);
list_insert_at(&s1, n3,-1);
list_insert_at(&s1, n2,-1);
list_insert_at(&s1, n1,-1);
list_print(&s1);
uint64_t test1 = list_delete_at(&s1,-1);
printf("Removed %ld from end of list.\n", test1);
list_print(&s1);
uint64_t test2 = list_delete_at(&s1,-1);
printf("Removed %ld from end of list.\n", test2);
list_print(&s1);
uint64_t test3 = list_delete_at(&s1,-1);
printf("Removed %ld from end of list.\n", test3);
list_print(&s1);
uint64_t test4 = list_delete_at(&s1,-1);
printf("Removed %ld from end of list.\n", test4);
list_print(&s1);
list_free(&s1);
return EXIT_SUCCESS;
}