Re organisation
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "common.h"
|
||||
|
||||
Logger engine_logger = {.name = "Engine"};
|
||||
|
||||
char chrToLower(char chr) {
|
||||
if (chr >= 'A' && chr <= 'Z')
|
||||
return chr + 32;
|
||||
@@ -11,118 +13,3 @@ 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_append(LinkedList *head, void *value) {
|
||||
LinkedList *current_node = head;
|
||||
while (current_node != NULL && current_node->next != NULL)
|
||||
current_node = current_node->next;
|
||||
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;
|
||||
}
|
||||
|
||||
uint linkedList_length(const LinkedList *head) {
|
||||
uint current_index = 0;
|
||||
const LinkedList *current_node = head;
|
||||
while (current_node != NULL) {
|
||||
current_node = current_node->next;
|
||||
current_index++;
|
||||
}
|
||||
|
||||
return current_index;
|
||||
}
|
||||
|
||||
bool linkedList_removeAll(LinkedList *head) {
|
||||
uint length = linkedList_length(head);
|
||||
LinkedList *lastNode = linkedList_getAt(head, length - 1);
|
||||
if (lastNode == NULL)
|
||||
return false;
|
||||
while (lastNode->prev != NULL) {
|
||||
lastNode = lastNode->prev;
|
||||
free(lastNode->next);
|
||||
}
|
||||
free(lastNode);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void linkedList_print(const LinkedList *head, void (*callback)(void *)) {
|
||||
const LinkedList *current_node = head;
|
||||
while (current_node != NULL) {
|
||||
callback(current_node->value);
|
||||
printf("->");
|
||||
current_node = current_node->next;
|
||||
}
|
||||
printf("NULL\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user