This commit is contained in:
Max-R 2025-12-05 10:26:10 +01:00
parent 7051134256
commit e7423f0a20

33
stack.c
View File

@ -10,24 +10,49 @@
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
StackNode *newNode = createNode(data); // Speicher für neuen Knoten
// reservieren
if (newNode == NULL)
{ // wenn Speicher nicht reserviert werden konnte, wird
// stack unverändert zurückgegeben
return stack;
}
newNode->next = stack; // pointer verschieben
return newNode; // Zeiger auf neuen Speicherbereich zurückgeben
}
// 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;
StackNode *nextNode = stack->next;
free(stack);
stack = NULL;
return nextNode;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
}
void *top(StackNode *stack) { return stack != NULL ? stack->data : NULL; }
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
while (stack != NULL)
{
StackNode *next = stack->next;
free(stack);
stack = next;
}
return NULL;
}