Generic Linked List
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#ifndef __BOARD_HEADER
|
||||
#define __BOARD_HEADER
|
||||
#include "common.h"
|
||||
|
||||
typedef uint Piece;
|
||||
|
||||
68
src/common.c
68
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");
|
||||
}
|
||||
|
||||
13
src/common.h
13
src/common.h
@@ -1,4 +1,5 @@
|
||||
#ifndef __COMMON_HEADER
|
||||
#define __COMMON_HEADER
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
24
src/main.c
24
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user