Files
info2Praktikum-DobleSpiel/Startcode/stack.c
T

42 lines
893 B
C

#include "stack.h"
#include <stdlib.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. */
StackNode *push(StackNode *stack, void *data) {
StackNode *newnode = malloc(sizeof(StackNode));
if (newnode == NULL)
return stack;
newnode->data = data;
newnode->next = stack;
return newnode;
}
StackNode *pop(StackNode *stack) {
if (stack == NULL)
return stack;
StackNode *newtop = stack->next;
free(stack);
return newtop;
}
void *top(StackNode *stack) {
if (stack == NULL)
return NULL;
return stack->data;
}
void clearStack(StackNode *stack) {
while (stack != NULL) {
stack = pop(stack); // pop returnt newtop;
}
}