Compare commits

...

4 Commits

Author SHA1 Message Date
regis37
3abe1477ac function clearStack is done 2025-11-26 23:01:16 +01:00
regis37
5deaf21a61 function pop is done 2025-11-26 22:56:13 +01:00
regis37
69f44a5aa0 function top is done 2025-11-26 22:54:27 +01:00
regis37
bc6ec040b7 function push is done 2025-11-26 22:52:32 +01:00
2 changed files with 25 additions and 8 deletions

23
stack.c
View File

@ -10,24 +10,43 @@
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
if(newNode == NULL)
return NULL;
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.)
StackNode *pop(StackNode *stack)
{
if (stack == NULL)
return NULL;
StackNode *next = stack->next;
free(stack);
return next;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
if (stack == NULL)
return NULL;
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
StackNode *current = stack;
while (current != NULL)
{
StackNode *next = current->next;
free(current);
current = next;
}
}

View File

@ -10,13 +10,11 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen
typedef struct Node{
int *data;
struct Node *next;
typedef struct StackNode {
void *data;
struct StackNode *next;
} StackNode;
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);