From f7fa0bf67ecd30779f0f10a1fbcb5e5eb37de16c Mon Sep 17 00:00:00 2001 From: Albarjas Date: Thu, 21 May 2026 16:45:18 +0200 Subject: [PATCH] Implement stack functions --- stack.c | 43 +++++++++++++++++++++++++++++++++++-------- stack.h | 21 +++++++++++++++------ 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/stack.c b/stack.c index e3a90d4..0cc348c 100644 --- a/stack.c +++ b/stack.c @@ -1,33 +1,60 @@ #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.) +// 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 *nextNode = NULL; + if (stack != NULL) + { + nextNode = stack->next; + free(stack); + } + + return nextNode; } // Returns the data of the top element. void *top(StackNode *stack) { + void *data = NULL; + if (stack != NULL) + { + data = stack->data; + } + + return data; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { + StackNode *currentNode = stack; + StackNode *nextNode = NULL; + 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..5e8826d 100644 --- a/stack.h +++ b/stack.h @@ -1,19 +1,28 @@ #ifndef 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. +/* 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 +/* + * One node of the stack. + * data stores a generic pointer. + * next points to the next element below this one. + */ +typedef struct StackNode +{ + void *data; + struct StackNode *next; +} StackNode; // Pushes data as pointer onto the stack. 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.) +// 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); // Returns the data of the top element. @@ -22,4 +31,4 @@ void *top(StackNode *stack); // Clears stack and releases all memory. void clearStack(StackNode *stack); -#endif +#endif \ No newline at end of file