82 lines
2.0 KiB
C
82 lines
2.0 KiB
C
#include "common.h"
|
|
|
|
char chrToLower(char chr) {
|
|
if (chr >= 'A' && chr <= 'Z')
|
|
return chr + 32;
|
|
return chr;
|
|
}
|
|
|
|
char chrToUpper(char chr) {
|
|
if (chr >= 'a' && chr <= 'z')
|
|
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");
|
|
}
|