diff --git a/stack.c b/stack.c index 916ac51..d44de95 100644 --- a/stack.c +++ b/stack.c @@ -16,9 +16,9 @@ StackNode *push(StackNode *stack, void *data) { return NULL; //Speicherfehler } - t->prev = stack; + t->next = stack; t->data = data; - return t; + return t; //Gibt den ersten StackNode des Stacks zurueck } return NULL; } @@ -27,17 +27,32 @@ StackNode *push(StackNode *stack, void *data) // freed by caller.) 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. 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; + stack = stack->prev; + free(tmp->data); + free(tmp); + } } \ No newline at end of file diff --git a/stack.h b/stack.h index e962b2b..0f6807e 100644 --- a/stack.h +++ b/stack.h @@ -10,7 +10,7 @@ The latest element is taken from the stack. */ //TODO: passenden Datentyp als struct anlegen typedef{ int* data; - struct StackNode *prev; + struct StackNode *next; }StackNode; // Pushes data as pointer onto the stack.