generated from freudenreichan/info2Praktikum-DobleSpiel
Grundlegende Stack-funktionen implementiert und Struct nochmal ueberarbeitet
This commit is contained in:
parent
09db456716
commit
7a20acd8f6
25
stack.c
25
stack.c
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
2
stack.h
2
stack.h
@ -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.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user