From 2c10463852c9dcef060a8ab1f5d47ce5e3373c4e Mon Sep 17 00:00:00 2001 From: Timo Hertel Date: Sun, 23 Nov 2025 16:07:25 +0100 Subject: [PATCH] Implement stack, basic test program & makefile entry for the test --- I2_Dobble/makefile | 10 ++++-- I2_Dobble/stack.c | 33 ++++++++++++++--- I2_Dobble/stack.h | 7 +++- I2_Dobble/test_stack.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 I2_Dobble/test_stack.c diff --git a/I2_Dobble/makefile b/I2_Dobble/makefile index 1f15f75..e855199 100644 --- a/I2_Dobble/makefile +++ b/I2_Dobble/makefile @@ -30,8 +30,14 @@ doble : main.o $(program_obj_files) $(CC) $(FLAGS) $^ -o doble $(program_obj_filesobj_files): %.o: %.c - $(CC) -c $(FLAGS) $^ -o $@ + $(CC) -c $(FLAGS) -g3 -fPIC $^ -o $@ +# -------------------------- +# Stack Tests +# -------------------------- +stackTests : stack.o test_stack.c + $(CC) $(FLAGS) -g3 $^ -o stackTest + # -------------------------- # Unit Tests # -------------------------- @@ -46,4 +52,4 @@ ifeq ($(OS),Windows_NT) del /f *.o doble else rm -f *.o doble -endif \ No newline at end of file +endif diff --git a/I2_Dobble/stack.c b/I2_Dobble/stack.c index e3a90d4..6c3c54b 100644 --- a/I2_Dobble/stack.c +++ b/I2_Dobble/stack.c @@ -10,24 +10,47 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { - + // ,-→ Wenn keine Daten angegeben wird die Liste unverändert zurückgegeben; Ist das sinnvoll? kein Anzeichen, kein Rückgabewert... + if (data != NULL) + { + StackNode *newNode; + newNode = (StackNode *)malloc(sizeof(StackNode)); + newNode->value = data; + + // ,-→ bedeutet Liste war leer - das neue Element hat keinen Nachfolger (next = NULL) + if (stack == NULL) { + newNode->next = NULL; + } else { + newNode->next = stack; + } + stack = newNode; + } + return stack; } // 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) + { + StackNode *nextNode = stack->next; + free(stack); + stack = nextNode; + } + return stack; } // Returns the data of the top element. void *top(StackNode *stack) { - + return stack->value; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { - -} \ No newline at end of file + while (stack != NULL) { + stack = pop(stack); + } +} diff --git a/I2_Dobble/stack.h b/I2_Dobble/stack.h index f7d542d..d1ca13c 100644 --- a/I2_Dobble/stack.h +++ b/I2_Dobble/stack.h @@ -7,7 +7,12 @@ The latest element is taken from the stack. */ #include -//TODO: passenden Datentyp als struct anlegen +//DONE: passenden Datentyp als struct anlegen +typedef struct Node +{ + void *value; + struct Node* next; +} StackNode; // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data); diff --git a/I2_Dobble/test_stack.c b/I2_Dobble/test_stack.c new file mode 100644 index 0000000..60fabd8 --- /dev/null +++ b/I2_Dobble/test_stack.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include "stack.h" + +void inspectStack(StackNode *stack) +{ + if (stack != NULL) + { + printf("Der Stack enthält die folgenden Elemente: %d", *(int*)stack->value); + while (stack->next != NULL) + { + printf(" %d", *(int*)stack->next->value); + stack = stack->next; + } + putchar('\n'); + } + else + { + printf("Der Stack ist leer\n"); + } +} + +int main() +{ + StackNode *stack; + stack = NULL; // initialisierung mit NULL -> leere Liste + + printf("...ein Element wird eingefügt...\n"); + int toBeRemoved = 42; + stack = push(stack, &toBeRemoved); + + inspectStack(stack); + + printf("...das Element wird wieder entfernt...\n"); + stack = pop(stack); + + inspectStack(stack); + + printf("...pop auf leeren Stack...\n"); + stack = pop(stack); + + inspectStack(stack); + putchar('\n'); + + int data[5] = {1, 2, 3, 4, 5}; + + // alle 5 werte der reihe nach auf den Satck legen - 1 unten ... 5 oben + for (int i = 0; i < 5; i++) + { + stack = push(stack, &data[i]); + } + + + //alle Elemente mit Test-Funktion ausgeben + inspectStack(stack); + + // Elemente stück für Stück ausgeben und entfernen + printf("1. Element war: %d\n", *(int*)top(stack)); + stack = pop(stack); + printf("2. Element war: %d\n", *(int*)top(stack)); + stack = pop(stack); + printf("3. Element war: %d\n", *(int*)top(stack)); + stack = pop(stack); + printf("4. Element war: %d\n", *(int*)top(stack)); + stack = pop(stack); + printf("5. Element war: %d\n", *(int*)top(stack)); +} + +/* +int main() +{ + UNITY_BEGIN(); + + printf("============================\n Stack tests\n============================\n"); + RUN_TEST(test_createNodeFailsOnZeroData); + + return UNITY_END(); +} +*/