generated from freudenreichan/info2Praktikum-DobleSpiel
62 lines
1.3 KiB
C
62 lines
1.3 KiB
C
#include <stdlib.h>
|
|
#include "stack.h"
|
|
|
|
//Testfunkionen zu push, pull, top & clearStack schreiben
|
|
|
|
StackNode *push(StackNode *stack, void *data)
|
|
{
|
|
if (!data)
|
|
{
|
|
return stack; //Nichts pushen
|
|
}
|
|
//if(stack && data){
|
|
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
|
if(!t)
|
|
{
|
|
return NULL; //Speicherfehler
|
|
}
|
|
t->next = stack;
|
|
t->data = data;
|
|
return t; //Gibt den ersten StackNode des Stacks zurueck
|
|
//}
|
|
return NULL;
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
StackNode *t = stack->next; //Naechstes Element im Stack wird erstes Element
|
|
free(stack);
|
|
return t;
|
|
}
|
|
else
|
|
{
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
// Returns the data of the top element.
|
|
void *top(StackNode *stack)
|
|
{
|
|
if(stack)
|
|
{
|
|
return stack->data;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
// Clears stack and releases all memory.
|
|
void clearStack(StackNode *stack)
|
|
{
|
|
while(stack)
|
|
{
|
|
StackNode *tmp = stack; //merkt sich den momentanen obersten Knoten
|
|
stack = stack->next; //setzt den obersten Knoten auf den Zweiten im Stack
|
|
free(tmp->data);
|
|
free(tmp);
|
|
}
|
|
}
|