From 89067a7ba6064281dfe7fe5693bceb81069121f7 Mon Sep 17 00:00:00 2001 From: "Denis.Schrempf" Date: Mon, 23 Nov 2026 21:41:30 +0100 Subject: [PATCH] Mein Teil (Sollte Fertig sein) --- Startcode/makefile | 2 ++ Startcode/stack.c | 31 +++++++++++++------ Startcode/stack.h | 13 ++++---- Startcode/test_stack.c | 68 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 Startcode/test_stack.c diff --git a/Startcode/makefile b/Startcode/makefile index 97c5e30..ada6852 100644 --- a/Startcode/makefile +++ b/Startcode/makefile @@ -34,6 +34,8 @@ $(program_obj_files): %.o: %.c # -------------------------- # Unit Tests +test_stack: test_stack.c stack.o stack.h + $(CC) $(FLAGS) -I$(unityfolder) test_stack.c stack.o $(unityfolder)/unity.c -o test_stack.exe # -------------------------- unitTests: echo "needs to be implemented" diff --git a/Startcode/stack.c b/Startcode/stack.c index e3a90d4..86a8170 100644 --- a/Startcode/stack.c +++ b/Startcode/stack.c @@ -1,33 +1,46 @@ #include #include "stack.h" -//TODO: grundlegende Stackfunktionen implementieren: -/* * `push`: legt ein Element oben auf den Stack, - * `pop`: entfernt das oberste Element, - * `top`: liefert das oberste Element zurück, - * `clearStack`: gibt den gesamten Speicher frei. */ - // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { + StackNode *newNode = (StackNode *)malloc(sizeof(StackNode)); + if (newNode == NULL) { + return stack; + } + newNode->data = data; + newNode->next = stack; + return newNode; } // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be // freed by caller.) StackNode *pop(StackNode *stack) { - + if (stack == NULL) { + return NULL; + } + StackNode *nextNode = stack->next; + free(stack); + return nextNode; } // Returns the data of the top element. void *top(StackNode *stack) { - + if (stack == NULL) { + return NULL; + } + return stack->data; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { - + while (stack != NULL) { + StackNode *nextNode = stack->next; + free(stack); + stack = nextNode; + } } \ No newline at end of file diff --git a/Startcode/stack.h b/Startcode/stack.h index f7d542d..afe1ef4 100644 --- a/Startcode/stack.h +++ b/Startcode/stack.h @@ -1,13 +1,16 @@ #ifndef STACK_H -#define STACK_H +#define STACK_H -/* A stack is a special type of queue which uses the LIFO (last in, first out) principle. -This means that with each new element all other elements are pushed deeper into the stack. -The latest element is taken from the stack. */ +/* A stack is a special type of queue which uses the LIFO (Last In, First Out) principle. + This means that with each new element all other elements are pushed deeper into the stack. + The latest element is taken from the stack. */ #include -//TODO: passenden Datentyp als struct anlegen +typedef struct StackNode { + void *data; + struct StackNode *next; +} StackNode; // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data); diff --git a/Startcode/test_stack.c b/Startcode/test_stack.c new file mode 100644 index 0000000..5e51789 --- /dev/null +++ b/Startcode/test_stack.c @@ -0,0 +1,68 @@ +#include +#include +#include "stack.h" + +void setUp(void) +{ +} +void tearDown(void) +{ +} + +static void test_empty_stack(void) +{ + StackNode *stack = NULL; + assert(top(stack) == NULL); + assert(pop(stack) == NULL); + clearStack(stack); +} + +static void test_push_top_pop_int(void) +{ + StackNode *stack = NULL; + int a = 10; + int b = 20; + int c = 30; + + stack = push(stack, &a); + assert(top(stack) == &a); + assert(*(int *)top(stack) == 10); + + stack = push(stack, &b); + stack = push(stack, &c); + assert(*(int *)top(stack) == 30); + + stack = pop(stack); + assert(*(int *)top(stack) == 20); + + stack = pop(stack); + assert(*(int *)top(stack) == 10); + + stack = pop(stack); + assert(stack == NULL); + assert(top(stack) == NULL); +} + +static void test_clear_stack(void) +{ + StackNode *stack = NULL; + int values[] = {1, 2, 3, 4, 5}; + + for (int i = 0; i < 5; i++) { + stack = push(stack, &values[i]); + } + + assert(*(int *)top(stack) == 5); + + clearStack(stack); +} + +int main(void) +{ + test_empty_stack(); + test_push_top_pop_int(); + test_clear_stack(); + + printf("Alle Stack-Tests erfolgreich bestanden!\n"); + return 0; +} \ No newline at end of file