diff --git a/Startcode/stack.c b/Startcode/stack.c index 06ea6e5..b0c3bcc 100644 --- a/Startcode/stack.c +++ b/Startcode/stack.c @@ -10,7 +10,25 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { + int tempnext = 0; + if(stack == NULL) + return NULL; + do{ + if(stack->prev != NULL){ + stack = stack->prev; + } + } while(stack->prev != NULL) + + stack->prev = malloc(sizeof(StackNode)); + tempnext = *stack; + stack = stack->prev; + stack->value = &data; + stack->prev = NULL; + stack->next = tempnext; + + return stack; + } // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be @@ -18,7 +36,7 @@ StackNode *push(StackNode *stack, void *data) StackNode *pop(StackNode *stack) { if(stack == NULL) - return NULL; + return &stack; do{ if(stack->prev == NULL){ @@ -29,7 +47,7 @@ StackNode *pop(StackNode *stack) stack = stack->prev; } } while(stack->prev != NULL) - return stack; + return &stack; } @@ -40,8 +58,8 @@ void *top(StackNode *stack) return &stack; do{ - if(stack->prev == NULL){ - stack = stack->next; + if(stack->prev != NULL){ + stack = stack->prev; } } while(stack->prev != NULL) return stack->value; @@ -53,7 +71,14 @@ void clearStack(StackNode *stack) if(stack == NULL) return; - do{ - if(stack->) - } + do{ + if(stack->prev == NULL){ + stack = stack->next; + stack->prev = NULL; + free(stack->prev); + } else { + stack = stack->prev; + } + } while(stack->next != NULL) + return; } \ No newline at end of file diff --git a/Startcode/stack.h b/Startcode/stack.h index 79ec753..b03f8ab 100644 --- a/Startcode/stack.h +++ b/Startcode/stack.h @@ -12,7 +12,7 @@ The latest element is taken from the stack. */ //typedef int Node; typedef struct { - int value; + void* value; StackNode* next; StackNode* prev;