Renamed Node to ListNode

* source/server/lists.c (appendToList): String replaced Node with ListNode.
(deleteNodeFromList): String replaced Node with ListNode.
(deleteListNodeFromList): String replaced Node with ListNode.
(indexOfFromList): String replaced Node with ListNode.
(getFirstFromList): String replaced Node with ListNode.
(isInList): String replaced Node with ListNode.
(isPointerInList): String replaced Node with ListNode.
* source/server/lists.h: String replaced Node with ListNode.
This commit is contained in:
Barra Ó Catháin 2024-04-14 23:47:03 +01:00
parent ef3cfb69c2
commit 80dce61058
2 changed files with 44 additions and 44 deletions

View File

@ -27,27 +27,27 @@ size_t appendToList(enum DataType type, struct List * list, void * data)
// First check that you're adding the correct type: // First check that you're adding the correct type:
assert(type == list->type); assert(type == list->type);
struct Node * newNode = calloc(1, sizeof(struct Node)); struct ListNode * newListNode = calloc(1, sizeof(struct ListNode));
newNode->next = NULL; newListNode->next = NULL;
newNode->previous = list->tail; newListNode->previous = list->tail;
newNode->data = data; newListNode->data = data;
if (list->itemCount == 0) if (list->itemCount == 0)
{ {
list->head = newNode; list->head = newListNode;
} }
else else
{ {
list->tail->next = newNode; list->tail->next = newListNode;
} }
list->tail = newNode; list->tail = newListNode;
list->itemCount++; list->itemCount++;
return list->itemCount; return list->itemCount;
} }
void * deleteNodeFromList(size_t index, struct List * list) void * deleteListNodeFromList(size_t index, struct List * list)
{ {
void * toReturn; void * toReturn;
@ -56,31 +56,31 @@ void * deleteNodeFromList(size_t index, struct List * list)
return NULL; return NULL;
} }
struct Node * currentNode = NULL; struct ListNode * currentListNode = NULL;
if (index < (list->itemCount / 2)) if (index < (list->itemCount / 2))
{ {
currentNode = list->head; currentListNode = list->head;
// Get to the correct point in the linked list: // Get to the correct point in the linked list:
for (int currentIndex = 0; currentIndex < index; currentIndex++) for (int currentIndex = 0; currentIndex < index; currentIndex++)
{ {
currentNode = currentNode->next; currentListNode = currentListNode->next;
} }
} }
else else
{ {
currentNode = list->tail; currentListNode = list->tail;
// Get to the correct point in the linked list: // Get to the correct point in the linked list:
for (int currentIndex = list->itemCount - 1; currentIndex > index; currentIndex--) for (int currentIndex = list->itemCount - 1; currentIndex > index; currentIndex--)
{ {
currentNode = currentNode->previous; currentListNode = currentListNode->previous;
} }
} }
if (currentNode == list->head) if (currentListNode == list->head)
{ {
list->head = list->head->next; list->head = list->head->next;
if (list->head) if (list->head)
@ -88,7 +88,7 @@ void * deleteNodeFromList(size_t index, struct List * list)
list->head->previous = NULL; list->head->previous = NULL;
} }
} }
if (currentNode == list->tail) if (currentListNode == list->tail)
{ {
list->tail = list->tail->previous; list->tail = list->tail->previous;
if (list->tail) if (list->tail)
@ -97,18 +97,18 @@ void * deleteNodeFromList(size_t index, struct List * list)
} }
} }
if (currentNode->next != NULL) if (currentListNode->next != NULL)
{ {
currentNode->next->previous = currentNode->previous; currentListNode->next->previous = currentListNode->previous;
} }
if (currentNode->previous != NULL) if (currentListNode->previous != NULL)
{ {
currentNode->previous->next = currentNode->next; currentListNode->previous->next = currentListNode->next;
} }
toReturn = currentNode->data; toReturn = currentListNode->data;
free(currentNode); free(currentListNode);
list->itemCount--; list->itemCount--;
@ -124,17 +124,17 @@ ssize_t indexOfFromList(bool (*comparisonFunction)(void *, void *), void * data,
} }
else else
{ {
struct Node * currentNode = list->head; struct ListNode * currentListNode = list->head;
do do
{ {
if (comparisonFunction(currentNode->data, data) == true) if (comparisonFunction(currentListNode->data, data) == true)
{ {
return index; return index;
} }
index++; index++;
currentNode = currentNode->next; currentListNode = currentListNode->next;
} }
while (currentNode != NULL); while (currentListNode != NULL);
return -1; return -1;
} }
@ -149,17 +149,17 @@ void * getFirstFromList(bool (*comparisonFunction)(void *, void *), void * data,
} }
else else
{ {
struct Node * currentNode = list->head; struct ListNode * currentListNode = list->head;
do do
{ {
if (comparisonFunction(currentNode->data, data) == true) if (comparisonFunction(currentListNode->data, data) == true)
{ {
return currentNode; return currentListNode;
} }
index++; index++;
currentNode = currentNode->next; currentListNode = currentListNode->next;
} }
while (currentNode != NULL); while (currentListNode != NULL);
return NULL; return NULL;
} }
@ -173,16 +173,16 @@ bool isInList(bool (*comparisonFunction)(void *, void *), void * data, struct Li
} }
else else
{ {
struct Node * currentNode = list->head; struct ListNode * currentListNode = list->head;
do do
{ {
if (comparisonFunction(currentNode->data, data) == 0) if (comparisonFunction(currentListNode->data, data) == 0)
{ {
return true; return true;
} }
currentNode = currentNode->next; currentListNode = currentListNode->next;
} }
while (currentNode != NULL); while (currentListNode != NULL);
return false; return false;
} }
@ -196,16 +196,16 @@ bool isPointerInList(void * data, struct List * list)
} }
else else
{ {
struct Node * currentNode = list->head; struct ListNode * currentListNode = list->head;
do do
{ {
if (currentNode->data == data) if (currentListNode->data == data)
{ {
return true; return true;
} }
currentNode = currentNode->next; currentListNode = currentListNode->next;
} }
while (currentNode != NULL); while (currentListNode != NULL);
return false; return false;
} }

View File

@ -12,14 +12,14 @@ struct List
{ {
size_t itemCount; size_t itemCount;
enum DataType type; enum DataType type;
struct Node * head; struct ListNode * head;
struct Node * tail; struct ListNode * tail;
}; };
struct Node struct ListNode
{ {
struct Node * next; struct ListNode * next;
struct Node * previous; struct ListNode * previous;
void * data; void * data;
}; };
@ -29,7 +29,7 @@ struct Node
struct List * createList(enum DataType type); struct List * createList(enum DataType type);
size_t appendToList(enum DataType type, struct List * list, void * data); size_t appendToList(enum DataType type, struct List * list, void * data);
void * deleteNodeFromList(size_t index, struct List * list); void * deleteListNodeFromList(size_t index, struct List * list);
ssize_t indexOfFromList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list); ssize_t indexOfFromList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list);
void * getFirstFromList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list); void * getFirstFromList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list);
bool isInList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list); bool isInList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list);