Files
info2Praktikum-DobleSpiel/Startcode/stack.c
T

39 lines
926 B
C

#include "stack.h"
#include <stdlib.h>
StackNode *push(StackNode *stack, void *data) {
if (data == NULL) //Das wird so gut wie immer getestet
return stack;
StackNode *newnode = malloc(sizeof(StackNode));
if (newnode == NULL)
return stack;
newnode->data = data;
newnode->next = stack; // Um Data was das neue Top ist mit der Kette zu verbinden
return newnode;
}
StackNode *pop(StackNode *stack) {
if (stack == NULL)
return stack;
StackNode *newtop = stack->next; //speichert das nächste und löscht das oberste, speichert dann da "nächste" als oberstes
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 und man springt immer weiter in der verkettung
}
}