From 8a91eb1fd0d492bdf7111d21f07dea1374ec9ee7 Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Wed, 27 Dec 2023 00:36:57 +0100 Subject: [PATCH] Generic Linked List --- src/board.h | 1 + src/common.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 13 ++++++++++ src/fen.h | 2 +- src/main.c | 24 ++++++++++++++----- 5 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/board.h b/src/board.h index 3b5a097..85ba9b0 100644 --- a/src/board.h +++ b/src/board.h @@ -1,4 +1,5 @@ #ifndef __BOARD_HEADER +#define __BOARD_HEADER #include "common.h" typedef uint Piece; diff --git a/src/common.c b/src/common.c index 4e1b0b4..d6e6ad7 100644 --- a/src/common.c +++ b/src/common.c @@ -11,3 +11,71 @@ char chrToUpper(char chr) { return chr - 32; return chr; } + +LinkedList *linkedList_init(void *value) { + LinkedList *node = (LinkedList *)malloc(sizeof(LinkedList)); + node->value = value; + node->next = NULL; + node->prev = NULL; + return node; +} + +LinkedList *linkedList_getAt(LinkedList *head, const uint index) { + uint current_index = 0; + LinkedList *current_node = head; + while (current_index++ != index) { + if (current_node->next == NULL) { + printf("[ERROR]: Linked List out of bounds.\n"); + return NULL; + } + current_node = current_node->next; + } + return current_node; +} + +bool linkedList_insertAt(LinkedList *head, void *value, const uint index) { + LinkedList *current_node = linkedList_getAt(head, index); + if (current_node == NULL) + return false; + LinkedList *node = linkedList_init(value); + // Set node's next neighbor to current node's next neighbor. + node->next = current_node->next; + // The previous neighbor of the newly created node is the current node. + node->prev = current_node; + // The new neightbor of current node is the newly created node. + current_node->next = node; + + // Set the previous neighbor of node's next neighbor to node. + if (node->next != NULL) { + node->next->prev = node; + } + + return true; +} + +bool linkedList_removeAt(LinkedList **head, const uint index) { + LinkedList *current_node = linkedList_getAt(*head, index); + if (current_node == NULL) + return false; + + LinkedList *prev = current_node->prev; + LinkedList *next = current_node->next; + if (prev != NULL) + prev->next = next; + if (next != NULL) + next->prev = prev; + if (index == 0) + *head = next; + free(current_node); + return true; +} + +void linkedList_print(LinkedList *head, void (*callback)(void *)) { + LinkedList *current_node = head; + while (current_node != NULL) { + callback(current_node->value); + printf("->"); + current_node = current_node->next; + } + printf("NULL\n"); +} diff --git a/src/common.h b/src/common.h index da5d67d..746496b 100644 --- a/src/common.h +++ b/src/common.h @@ -1,4 +1,5 @@ #ifndef __COMMON_HEADER +#define __COMMON_HEADER #include #include #include @@ -8,4 +9,16 @@ typedef unsigned int uint; char chrToLower(char chr); char chrToUpper(char chr); +typedef struct t_node_ll { + void *value; + struct t_node_ll *next; + struct t_node_ll *prev; +} LinkedList; + +LinkedList *linkedList_init(void *value); +LinkedList *linkedList_getAt(LinkedList *head, const uint index); +bool linkedList_insertAt(LinkedList *head, void *value, const uint index); +bool linkedList_removeAt(LinkedList **head, const uint index); +void linkedList_print(LinkedList *head, void (*callback)(void *)); + #endif // !__COMMON_HEADER diff --git a/src/fen.h b/src/fen.h index aa2c653..e37a640 100644 --- a/src/fen.h +++ b/src/fen.h @@ -1,6 +1,6 @@ #ifndef __FEN_HEADER +#define __FEN_HEADER #include "board.h" -#include "common.h" typedef char FEN[75]; void fen_fromBoard(const Board board, FEN fen); diff --git a/src/main.c b/src/main.c index 0422305..1d36bc5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,23 @@ #include "fen.h" +void printInt(void *value) { printf("%d", *(int *)value); } + int main() { - Board board; - FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; - fen_toBoard(fen, board); - Coordinates *coords = piece_getMoves(board, board[3].piece, board[3].coords); - printf("%d,%d", coords->row, coords->column); - free(coords); + // Board board; + // FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; + // fen_toBoard(fen, board); + // Coordinates *coords = piece_getMoves(board, board[3].piece, + // board[3].coords); printf("%d,%d", coords->row, coords->column); + // free(coords); + int a = 2; + LinkedList *head = linkedList_init(&a); + int b = 3; + linkedList_insertAt(head, &b, 0); + int c = 6; + linkedList_insertAt(head, &c, 1); + linkedList_print(head, printInt); + linkedList_removeAt(&head, 3); + linkedList_print(head, printInt); + // printf("%d", *(int *)first.value); return 0; }