Let's get started

This commit is contained in:
parys 2024-01-22 12:46:01 +01:00
commit b2d67cca8d
5 changed files with 299 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
main
valgrind-out.txt

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023 Maciej Samborski
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.

4
README.md Normal file
View File

@ -0,0 +1,4 @@
# ll
A generic singly linked list in C.

54
example.c Normal file
View File

@ -0,0 +1,54 @@
#include <stdio.h>
#define LL_IMPLEMENTATION
#include "ll.h"
#define llPrint(type, format, list) do { \
for (size_t i = 0; i < llLen(list); ++i) \
{ \
printf(format"\n", llGet(type, list, i)); \
} \
} while (0)
int main(void)
{
llList list = llCreateList();
llAppend(list, 75);
llAppend(list, 120);
llAppend(list, 2377);
llPrepend(list, 665);
int x = llGet(int, list, 0);
printf("x: %d\n", x);
int * y = llGetRef(int, list, 1);
printf("*y: %d\n", *y);
printf("%zu\n", llLen(list));
llPrint(int, "%d", list);
llList list2 = llCreateList();
llAppend(list2, 'H');
llAppend(list2, 'i');
llAppend(list2, ' ');
llAppend(list2, 'o');
llAppend(list2, 'm');
llAppend(list2, '!');
llInsert(list2, 3, 'm');
for (size_t i = 0; i < llLen(list2); ++i)
{
printf("%c", llGet(char, list2, i));
}
printf("\n");
llFree(&list);
llFree(&list2);
return 0;
}

230
ll.h Normal file
View File

@ -0,0 +1,230 @@
#ifndef LL_H_
#define LL_H_
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
typedef struct llNode llNode;
typedef struct llList llList;
struct llNode {
void * data;
llNode * next;
};
struct llList {
llNode * nodes;
};
llList llCreateList();
llNode * _llCreateNode(void * value);
void llFree(llList * list);
void _llAppend(llList * list, void * value);
llList * _llPrepend(llList * list, void * value);
llList * _llInsert(llList * list, size_t id, void * value);
llList * _llDelete(llList * list, size_t id);
size_t _llGet(llList * list, size_t id);
void * _llGetRef(llList * list, size_t id);
size_t llLen(llList list);
#define llAppend(list, value) _llAppend(&list, (void *) value)
#define llPrepend(list, value) list = *_llPrepend(&list, (void *) value)
#define llInsert(list, id, value) list = *_llInsert(&list, id, (void *) value)
#define llDelete(list, id) list = *_llDelete(&list, id)
#define llGet(type, list, id) (type) _llGet(&list, id)
#define llGetRef(type, list, id) (type *) _llGetRef(&list, id)
#define llCreateNode(value) _llCreateNode((void *) value)
#ifdef LL_IMPLEMENTATION
llList llCreateList()
{
return (llList) {
.nodes = NULL,
};
}
llNode * _llCreateNode(void * value)
{
llNode * node = malloc(sizeof(llNode));
node->data = value;
node->next = NULL;
return node;
}
void llFreeNodes(llNode * node)
{
if (node->next != NULL)
llFreeNodes(node->next);
free(node);
}
void llFree(llList * list)
{
if (list->nodes == NULL) return;
llNode * node = list->nodes;
if (node->next != NULL)
llFreeNodes(node);
else
free(node);
}
void _llAppend(llList * list, void * value)
{
if (list->nodes == NULL)
{
list->nodes = llCreateNode(value);
return;
}
llNode * node = list->nodes;
while (node->next != NULL)
node = node->next;
node->next = llCreateNode(value);
}
llList * _llPrepend(llList * list, void * value)
{
llNode * node = llCreateNode(value);
node->next = list->nodes;
list->nodes = node;
return list;
}
llList * _llInsert(llList * list, size_t id, void * value)
{
llNode * prev = NULL;
llNode * node = list->nodes;
if (id == 0)
{
list->nodes = llCreateNode(value);
list->nodes->next = node;
return list;
}
while (id > 0)
{
if (node->next == NULL && id > 1)
{
fprintf(stderr, "RUNTIME ERROR: Index out of bounds\n");
llFree(list);
exit(1);
}
prev = node;
node = node->next;
--id;
}
prev->next = _llCreateNode(value);
prev->next->next = node;
return list;
}
llList * _llDelete(llList * list, size_t id)
{
llNode * prev = NULL;
llNode * node = list->nodes;
if (list->nodes == NULL) return list;
if (id == 0)
{
list->nodes = node->next;
free(node);
return list;
}
while (id > 0)
{
if (node->next == NULL)
{
fprintf(stderr, "RUNTIME ERROR: Index out of bounds\n");
llFree(list);
exit(1);
}
prev = node;
node = node->next;
--id;
}
prev->next = node->next;
free(node);
return list;
}
size_t _llGet(llList * list, size_t id)
{
llNode * node = list->nodes;
while (id > 0)
{
if (node->next == NULL)
{
fprintf(stderr, "RUNTIME ERROR: Index out of bounds\n");
llFree(list);
exit(1);
}
node = node->next;
--id;
}
return (size_t) node->data;
}
void * _llGetRef(llList * list, size_t id)
{
llNode * node = list->nodes;
while (id > 0)
{
if (node->next == NULL)
{
fprintf(stderr, "RUNTIME ERROR: Index out of bounds\n");
llFree(list);
exit(1);
}
node = node->next;
--id;
}
return (void *) &node->data;
}
size_t llLen(llList list)
{
llNode * node = list.nodes;
size_t len = 0;
while (node != NULL)
{
++len;
node = node->next;
}
return len;
}
#endif // LL_IMPLEMENTATION
#endif // LL_H_