#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 = malloc(sizeof(StackNode)); newNode->data = data; newNode->next = stack; // Set the new node's next pointer to the current top of the stack. return newNode; // Return the new node as the top of the 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) { return NULL; // Nothing to pop if stack is empty. } StackNode *tempNode = stack; stack = stack->next; // Move the stack pointer to the next node. free(tempNode); // Free the old top node. return stack; } // Returns the data of the top element. void *top(StackNode *stack) { if (stack == NULL) { return NULL; // Return NULL if stack is empty. } return stack->data; // Return the value of the top node. } // Clears stack and releases all memory. void clearStack(StackNode *stack) { while (stack != NULL) { stack = pop(stack); // Pop each element and free memory. } }