Initial Implementation Of Lists
* lists.c (createList): Initial implementation. (appendToList): Initial implementation. (deleteNodeFromList): Initial implementation. (indexOfFromList): Initial implementation. (getFirstFromList): Initial implementation. (isInList): Initial implementation. (isPointerInList): Initial implementation. * lists.h (LISTS_H): Defines the enum "DataType", and the structs "Node" and "List".
This commit is contained in:
parent
ce9f197a83
commit
9562cbfd21
|
@ -0,0 +1,230 @@
|
|||
// =========================================
|
||||
// | SilverMUD Server - lists.c |
|
||||
// | Copyright (C) 2023, Barra Ó Catháin |
|
||||
// | See end of file for copyright notice. |
|
||||
// =========================================
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "lists.h"
|
||||
|
||||
// Functions:
|
||||
// ==========
|
||||
struct List * createList(enum DataType type)
|
||||
{
|
||||
struct List * newList = calloc(1, sizeof(struct List));
|
||||
newList->itemCount = 0;
|
||||
newList->head = NULL;
|
||||
newList->tail = NULL;
|
||||
newList->type = type;
|
||||
return newList;
|
||||
}
|
||||
|
||||
size_t appendToList(enum DataType type, struct List * list, void * data)
|
||||
{
|
||||
// First check that you're adding the correct type:
|
||||
assert(type == list->type);
|
||||
|
||||
struct Node * newNode = calloc(1, sizeof(struct Node));
|
||||
newNode->next = NULL;
|
||||
newNode->previous = list->tail;
|
||||
newNode->data = data;
|
||||
|
||||
if (list->itemCount == 0)
|
||||
{
|
||||
list->head = newNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
list->tail->next = newNode;
|
||||
}
|
||||
|
||||
list->tail = newNode;
|
||||
list->itemCount++;
|
||||
|
||||
return list->itemCount;
|
||||
}
|
||||
|
||||
void * deleteNodeFromList(size_t index, struct List * list)
|
||||
{
|
||||
void * toReturn;
|
||||
|
||||
if ((list->itemCount - 1) < index)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Node * currentNode = NULL;
|
||||
|
||||
if (index < (list->itemCount / 2))
|
||||
{
|
||||
currentNode = list->head;
|
||||
|
||||
// Get to the correct point in the linked list:
|
||||
for (int currentIndex = 0; currentIndex < index; currentIndex++)
|
||||
{
|
||||
currentNode = currentNode->next;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
currentNode = list->tail;
|
||||
|
||||
// Get to the correct point in the linked list:
|
||||
for (int currentIndex = list->itemCount - 1; currentIndex > index; currentIndex--)
|
||||
{
|
||||
currentNode = currentNode->previous;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentNode == list->head)
|
||||
{
|
||||
list->head = list->head->next;
|
||||
if (list->head)
|
||||
{
|
||||
list->head->previous = NULL;
|
||||
}
|
||||
}
|
||||
if (currentNode == list->tail)
|
||||
{
|
||||
list->tail = list->tail->previous;
|
||||
if (list->tail)
|
||||
{
|
||||
list->tail->next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentNode->next != NULL)
|
||||
{
|
||||
currentNode->next->previous = currentNode->previous;
|
||||
}
|
||||
|
||||
if (currentNode->previous != NULL)
|
||||
{
|
||||
currentNode->previous->next = currentNode->next;
|
||||
}
|
||||
|
||||
toReturn = currentNode->data;
|
||||
free(currentNode);
|
||||
|
||||
list->itemCount--;
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
ssize_t indexOfFromList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list)
|
||||
{
|
||||
size_t index = 0;
|
||||
if (list->head == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct Node * currentNode = list->head;
|
||||
do
|
||||
{
|
||||
if (comparisonFunction(currentNode->data, data) == true)
|
||||
{
|
||||
return index;
|
||||
}
|
||||
index++;
|
||||
currentNode = currentNode->next;
|
||||
}
|
||||
while (currentNode != NULL);
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void * getFirstFromList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list)
|
||||
{
|
||||
size_t index = 0;
|
||||
if (list->head == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct Node * currentNode = list->head;
|
||||
do
|
||||
{
|
||||
if (comparisonFunction(currentNode->data, data) == true)
|
||||
{
|
||||
return currentNode;
|
||||
}
|
||||
index++;
|
||||
currentNode = currentNode->next;
|
||||
}
|
||||
while (currentNode != NULL);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool isInList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list)
|
||||
{
|
||||
if (list->head == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct Node * currentNode = list->head;
|
||||
do
|
||||
{
|
||||
if (comparisonFunction(currentNode->data, data) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode->next;
|
||||
}
|
||||
while (currentNode != NULL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isPointerInList(void * data, struct List * list)
|
||||
{
|
||||
if (list->head == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct Node * currentNode = list->head;
|
||||
do
|
||||
{
|
||||
if (currentNode->data == data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode->next;
|
||||
}
|
||||
while (currentNode != NULL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// | End of lists.h, copyright notice follows. |
|
||||
// =============================================
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
// =========================================
|
||||
// | SilverMUD Server - lists.h |
|
||||
// | Copyright (C) 2023, Barra Ó Catháin |
|
||||
// | See end of file for copyright notice. |
|
||||
// =========================================
|
||||
#ifndef LISTS_H
|
||||
#define LISTS_H
|
||||
#include <stdbool.h>
|
||||
|
||||
enum DataType
|
||||
{
|
||||
AREA,
|
||||
PLAYER,
|
||||
CONNECTION
|
||||
};
|
||||
|
||||
struct List
|
||||
{
|
||||
size_t itemCount;
|
||||
enum DataType type;
|
||||
struct Node * head;
|
||||
struct Node * tail;
|
||||
};
|
||||
|
||||
struct Node
|
||||
{
|
||||
struct Node * next;
|
||||
struct Node * previous;
|
||||
void * data;
|
||||
};
|
||||
|
||||
|
||||
// Functions:
|
||||
// ==========
|
||||
|
||||
struct List * createList(enum DataType type);
|
||||
size_t appendToList(enum DataType type, struct List * list, void * data);
|
||||
void * deleteNodeFromList(size_t index, 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);
|
||||
bool isInList(bool (*comparisonFunction)(void *, void *), void * data, struct List * list);
|
||||
bool isPointerInList(void * data, struct List * list);
|
||||
|
||||
#endif
|
||||
// =============================================
|
||||
// | End of lists.h, copyright notice follows. |
|
||||
// =============================================
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
Loading…
Reference in New Issue