From 2c8974b6c6cc0b9d9658e9e93c416c9abe288f8c Mon Sep 17 00:00:00 2001 From: fonkou Date: Tue, 25 Nov 2025 16:57:33 +0100 Subject: [PATCH 1/2] add typdef StackNode --- stack.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/stack.c b/stack.c index e3a90d4..845f1a6 100644 --- a/stack.c +++ b/stack.c @@ -8,26 +8,23 @@ * `clearStack`: gibt den gesamten Speicher frei. */ // Pushes data as pointer onto the stack. -StackNode *push(StackNode *stack, void *data) -{ +typedef struct Stack_node{ + int *stackNode; +}StackNode; + +StackNode *push(StackNode *stack, void *data) { } // 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) -{ - +StackNode *pop(StackNode *stack) { } // Returns the data of the top element. -void *top(StackNode *stack) -{ - +void *top(StackNode *stack) { } // Clears stack and releases all memory. -void clearStack(StackNode *stack) -{ - -} \ No newline at end of file +void clearStack(StackNode *stack) { +} From c2955be947df47d47f537abbb02bc7cbd5dbc571 Mon Sep 17 00:00:00 2001 From: fonkou Date: Tue, 2 Dec 2025 01:42:34 +0100 Subject: [PATCH 2/2] fix stac.c --- stack.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/stack.c b/stack.c index 845f1a6..c13df34 100644 --- a/stack.c +++ b/stack.c @@ -1,30 +1,43 @@ #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. -typedef struct Stack_node{ - int *stackNode; -}StackNode; +typedef struct Stack_node { + void *data; + struct Stack_node *next; +} StackNode; +// Pushes data onto the stack. +// Returns the new top of the stack. StackNode *push(StackNode *stack, void *data) { + StackNode *node = malloc(sizeof(StackNode)); + if (!node) return stack; // allocation failed → leave unchanged + node->data = data; + node->next = stack; + return node; // new top element } -// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be -// freed by caller.) +// Deletes the top element and returns the new stack top. +// Caller must free the *data* itself. StackNode *pop(StackNode *stack) { + if (!stack) return NULL; + + StackNode *next = stack->next; + free(stack); // free only the node, NOT the data + return next; } -// Returns the data of the top element. +// Returns the data at the top of the stack. void *top(StackNode *stack) { + if (!stack) return NULL; + return stack->data; } -// Clears stack and releases all memory. +// Clears entire stack (but does NOT free the data). void clearStack(StackNode *stack) { + while (stack) { + StackNode *next = stack->next; + free(stack); + stack = next; + } }