From cc927ad72337dfee53fa93dc27b7e4e4efbacc20 Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Fri, 5 Dec 2025 12:19:11 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Stack=20Funktionen=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- highscores.txt | 1 + stack.c | 40 ++++++++++++++++++++++++++++++++++++++++ stack.h | 5 +++++ 3 files changed, 46 insertions(+) diff --git a/highscores.txt b/highscores.txt index 4edd5a7..2326b02 100644 --- a/highscores.txt +++ b/highscores.txt @@ -1 +1,2 @@ +player_name;9943 player1;3999 diff --git a/stack.c b/stack.c index e3a90d4..dd516f9 100644 --- a/stack.c +++ b/stack.c @@ -10,24 +10,64 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { + StackNode *newNode = malloc(sizeof(StackNode)); + if(*newNode == NULL) + { + //printf("Fehler Bei Speicherallozierung!"); + return NULL; + } + 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) + { + //printf("Fehlerhafte Adresse uebergeben"); + return NULL; + } + StackNode *next = stack->next; + + free(stack); + + return next; } // Returns the data of the top element. void *top(StackNode *stack) { + if(stack == NULL) + { + //printf("Fehlerhafte Adresse uebergeben"); + return NULL; + } + return stack->data; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { + if(stack == NULL) + { + //printf("Fehlerhafte Adresse uebergeben"); + return NULL; + } + StackNode *currentNode = stack; + StackNode *nextNode; + + while(currentNode != NULL) + { + nextNode = currentNode->next; + free(currentNode); + currentNode = nextNode; + } } \ No newline at end of file diff --git a/stack.h b/stack.h index f7d542d..f438504 100644 --- a/stack.h +++ b/stack.h @@ -8,6 +8,11 @@ The latest element is taken from the stack. */ #include //TODO: passenden Datentyp als struct anlegen +typedef struct StackNode +{ + void *data; + struct SatckNode *next; +} StackNode; // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data); From 00fafec3dc18a54bef8abee5e481802e5f9f3238 Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Fri, 5 Dec 2025 14:33:22 +0100 Subject: [PATCH 2/2] test_stack.c erstellt --- stack.c | 4 +- stack.h | 2 +- test_stack.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 test_stack.c diff --git a/stack.c b/stack.c index dd516f9..6ce7eff 100644 --- a/stack.c +++ b/stack.c @@ -11,7 +11,7 @@ StackNode *push(StackNode *stack, void *data) { StackNode *newNode = malloc(sizeof(StackNode)); - if(*newNode == NULL) + if(newNode == NULL) { //printf("Fehler Bei Speicherallozierung!"); return NULL; @@ -58,7 +58,7 @@ void clearStack(StackNode *stack) if(stack == NULL) { //printf("Fehlerhafte Adresse uebergeben"); - return NULL; + return; } StackNode *currentNode = stack; diff --git a/stack.h b/stack.h index f438504..d80bda0 100644 --- a/stack.h +++ b/stack.h @@ -11,7 +11,7 @@ The latest element is taken from the stack. */ typedef struct StackNode { void *data; - struct SatckNode *next; + struct StackNode *next; } StackNode; // Pushes data as pointer onto the stack. diff --git a/test_stack.c b/test_stack.c new file mode 100644 index 0000000..26a219f --- /dev/null +++ b/test_stack.c @@ -0,0 +1,121 @@ +#include +#include +#include "stack.h" + +void testeStackBeschreiben() +{ + printf("=== Test: push() ===\n"); + + StackNode *stack = NULL; + int wert1 = 42; + stack = push(stack, &wert1); + + int *topValue = (int *)(stack->data); + if(topValue != NULL && *topValue == 42) + { + printf("Test 1: Erstes Element erfolgreich gepusht!\n"); + } + else + { + printf("Test 1: FEHLGESCHLAGEN!\n"); + } + + + int wert2 = 12; + stack = push(stack, &wert2); + + topValue = (int *)(stack->data); + int *secondValue = (int *)((stack->next)->data); + + if(topValue != NULL && *topValue == 12.25 && secondValue != NULL && *secondValue == 42) + { + printf("Test 2: Zweites Element erfolgreich gepusht!\n"); + } + else + { + printf("Test 2: FEHLGESCHLAGEN!\n"); + } + + + printf("=== Ende Test: push() ===\n\n"); + return; +} + +void testepop() +{ + printf("=== Test: pop() ===\n"); + + StackNode *stack = NULL; + int wert1 = 20; + int wert2 = 74; + + stack = push(stack, &wert1); + stack = push(stack, &wert2); + + stack = pop(stack); + + int *topValue = (int *)(stack->data); + + if(topValue != NULL && *topValue == 20) + { + printf("Test: Erstes Element erfolgreich gelöscht!\n"); + } + else + { + printf("Test 1: FEHLGESCHLAGEN!\n"); + } + + printf("=== Ende Test: pop() ===\n\n"); +} + +void testetop() +{ + printf("=== Test: top() ===\n"); + + StackNode *stack = NULL; + int wert1 = 20; + int wert2 = 74; + + stack = push(stack, &wert1); + stack = push(stack, &wert2); + + int *topValue = top(stack); + + if(topValue != NULL && *topValue == 74) + { + printf("Test: top() gibt korrektes Element zurück!\n"); + } + else + { + printf("Test: FEHLGESCHLAGEN!\n"); + } + + printf("=== Ende Test: top() ===\n\n"); +} + +void testeclearStack() +{ + printf("=== Test: clearStack() ===\n"); + + StackNode *stack = NULL; + int wert1 = 20; + int wert2 = 74; + + stack = push(stack, &wert1); + stack = push(stack, &wert2); + + clearStack(stack); + + printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n"); + printf("=== Ende Test: clearStack() ===\n\n"); +} + +int main() +{ + testeStackBeschreiben(); + testepop(); + testetop(); + testeclearStack(); + + return 0; +} \ No newline at end of file