Add initial version with basic queue implementation.

This commit is contained in:
paulusja 2025-12-04 12:00:59 +01:00
parent 01a153cea4
commit d99200f020
3 changed files with 172 additions and 0 deletions

40
09_bst/main.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}

123
09_bst/namesarchive.c Normal file
View File

@ -0,0 +1,123 @@
#include "namesarchive.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
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);
}
}

9
09_bst/namesarchive.h Normal file
View File

@ -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