Grundlegende Stack-funktionen implementiert und Struct nochmal ueberarbeitet

This commit is contained in:
silvana884 2025-12-03 14:15:45 +01:00
parent 09db456716
commit 7a20acd8f6
2 changed files with 21 additions and 6 deletions

25
stack.c
View File

@ -16,9 +16,9 @@ StackNode *push(StackNode *stack, void *data)
{ {
return NULL; //Speicherfehler return NULL; //Speicherfehler
} }
t->prev = stack; t->next = stack;
t->data = data; t->data = data;
return t; return t; //Gibt den ersten StackNode des Stacks zurueck
} }
return NULL; return NULL;
} }
@ -27,17 +27,32 @@ StackNode *push(StackNode *stack, void *data)
// freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) StackNode *pop(StackNode *stack)
{ {
if(stack)
{
StackNode *t = stack->next; //Naechstes Element im Stack wird erstes Element
free(stack);
return t;
}
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) void *top(StackNode *stack)
{ {
if(stack)
{
return stack->data;
}
return NULL;
} }
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
while(stack)
{
StackNode *tmp = stack;
stack = stack->prev;
free(tmp->data);
free(tmp);
}
} }

View File

@ -10,7 +10,7 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen //TODO: passenden Datentyp als struct anlegen
typedef{ typedef{
int* data; int* data;
struct StackNode *prev; struct StackNode *next;
}StackNode; }StackNode;
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.