diff --git a/lib/stack.c b/lib/stack.c index 53d800d..06399f9 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -3,15 +3,13 @@ #include "card.h" #include "stack.h" -struct stack *initialize_stack() { - struct stack *stack = NULL; +void initialize_stack(struct stack **stack) { + *stack = malloc(sizeof(**stack)); - stack = malloc(sizeof(*stack)); + (*stack)->card = NULL; + (*stack)->next = NULL; - stack->card = NULL; - stack->next = NULL; - - return(stack); + return; } bool empty(struct stack *stack) { @@ -39,19 +37,24 @@ void push(struct stack **stack, struct card *card) { if (empty(*stack)) { (*stack)->card = card; } else { - new_stack = initialize_stack(); + initialize_stack(&new_stack); new_stack->card = card; new_stack->next = (*stack); *stack = new_stack; } } +/* FIXME: uglyness inside if statement */ struct stack *pop(struct stack **stack) { struct stack *popped_entry = NULL; if(!empty(*stack)) { popped_entry = *stack; - *stack = (*stack)->next; + if (length(*stack) == 1) { + initialize_stack(stack); + } else { + *stack = (*stack)->next; + } popped_entry->next = NULL; } diff --git a/lib/stack.h b/lib/stack.h index 6b277e8..18fb97d 100644 --- a/lib/stack.h +++ b/lib/stack.h @@ -6,7 +6,7 @@ struct stack { struct stack *next; }; -struct stack *initialize_stack(); +void initialize_stack(struct stack **); bool empty(struct stack *); int length(struct stack *); void push(struct stack **, struct card *);