From d99200f020dcfb675f8caae4ccbfadf33e986cf7 Mon Sep 17 00:00:00 2001 From: paulusja Date: Thu, 4 Dec 2025 12:00:59 +0100 Subject: [PATCH] Add initial version with basic queue implementation. --- 09_bst/main.c | 40 ++++++++++++++ 09_bst/namesarchive.c | 123 ++++++++++++++++++++++++++++++++++++++++++ 09_bst/namesarchive.h | 9 ++++ 3 files changed, 172 insertions(+) create mode 100644 09_bst/main.c create mode 100644 09_bst/namesarchive.c create mode 100644 09_bst/namesarchive.h diff --git a/09_bst/main.c b/09_bst/main.c new file mode 100644 index 0000000..b4d5fbf --- /dev/null +++ b/09_bst/main.c @@ -0,0 +1,40 @@ +#include +#include +#include "namesarchive.h" + +int main() +{ + const char *namesToAdd[] = {"Sara", "Tim", "Karl", "Gerd", "Bruno", "Adam", "Hanna", "Yvonne", "Werner", "Walter", "Zorro"}; + const char *namesToRemove[] = {"Yvonne", "Zorro", "Sara", "Horatio"}; + const unsigned int lenNamesToAdd = sizeof(namesToAdd) / sizeof(namesToAdd[0]); + const unsigned int lenNamesToRemove = sizeof(namesToRemove) / sizeof(namesToRemove[0]); + + for(int i = 0; i < lenNamesToAdd; i++) + { + printf("Adding %s ...\n", namesToAdd[i]); + addName(namesToAdd[i]); + } + + printf("\nNames in archive:\n"); + printNames(); + printf("\n"); + + for(int i = 0; i < lenNamesToRemove; i++) + { + printf("Removing %s ...\n", namesToRemove[i]); + removeName(namesToRemove[i]); + } + + printf("\nNames in archive:\n"); + printNames(); + printf("\n"); + + printf("Clearing archive ...\n"); + clearArchive(); + + printf("\nNames in archive:\n"); + printNames(); + printf("\n"); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/09_bst/namesarchive.c b/09_bst/namesarchive.c new file mode 100644 index 0000000..564b9ab --- /dev/null +++ b/09_bst/namesarchive.c @@ -0,0 +1,123 @@ +#include "namesarchive.h" +#include +#include +#include + +typedef struct queueElem +{ + char *name; + struct queueElem *next; +} QueueElem; + +static QueueElem *head = NULL; + +static QueueElem *createEmptyElem() +{ + return calloc(1, sizeof(QueueElem)); +} + +static QueueElem *createNameElem(const char *name) +{ + QueueElem *newElem = NULL; + + if(name != NULL) + { + newElem = createEmptyElem(); + + if(newElem != NULL) + { + newElem->name = malloc(sizeof(char) * (strlen(name)+1)); + + if(newElem->name != NULL) + strcpy(newElem->name, name); + else + { + free(newElem); + newElem = NULL; + } + } + } + + return newElem; +} + +void addName(const char *name) +{ + if(head == NULL) + head = createEmptyElem(); + + if(head != NULL) + { + QueueElem *newElem = createNameElem(name); + + if(newElem != NULL) + { + QueueElem *currentElem = head; + + while(currentElem->next != NULL) + { + currentElem = currentElem->next; + } + + currentElem->next = newElem; + } + } +} + +void printNames() +{ + if(head != NULL) + { + QueueElem *elem = head->next; + + while(elem != NULL) + { + printf("%s\n", elem->name); + elem = elem->next; + } + } +} + +static QueueElem *getPreviousNode(const char *name) +{ + QueueElem *currentElem = head; + + while(currentElem != NULL && currentElem->next != NULL && strcmp(currentElem->next->name, name) != 0) + { + currentElem = currentElem->next; + } + + if(currentElem == NULL || currentElem->next == NULL) + return NULL; + else + return currentElem; +} + +static void disposeElem(QueueElem *elem) +{ + if(elem != NULL) + free(elem->name); + free(elem); +} + +void removeName(const char *name) +{ + QueueElem *prevElem = getPreviousNode(name); + + if(prevElem != NULL) + { + QueueElem *elemToRemove = prevElem->next; + prevElem->next = prevElem->next->next; + disposeElem(elemToRemove); + } +} + +void clearArchive() +{ + while(head != NULL) + { + QueueElem *oldHead = head; + head = head->next; + disposeElem(oldHead); + } +} diff --git a/09_bst/namesarchive.h b/09_bst/namesarchive.h new file mode 100644 index 0000000..fcb6dc1 --- /dev/null +++ b/09_bst/namesarchive.h @@ -0,0 +1,9 @@ +#ifndef NAMEARCHIVE_H +#define NAMEARCHIVE_H + +void addName(const char *name); +void removeName(const char *name); +void printNames(); +void clearArchive(); + +#endif \ No newline at end of file