Fixed empty() to use the new initialize_stack(), and pop().

This commit is contained in:
Murilo Soares Pereira 2010-04-04 20:52:02 -03:00
parent e72d21f1f0
commit 8173aaa7ff
2 changed files with 13 additions and 10 deletions

View File

@ -3,15 +3,13 @@
#include "card.h" #include "card.h"
#include "stack.h" #include "stack.h"
struct stack *initialize_stack() { void initialize_stack(struct stack **stack) {
struct stack *stack = NULL; *stack = malloc(sizeof(**stack));
stack = malloc(sizeof(*stack)); (*stack)->card = NULL;
(*stack)->next = NULL;
stack->card = NULL; return;
stack->next = NULL;
return(stack);
} }
bool empty(struct stack *stack) { bool empty(struct stack *stack) {
@ -39,19 +37,24 @@ void push(struct stack **stack, struct card *card) {
if (empty(*stack)) { if (empty(*stack)) {
(*stack)->card = card; (*stack)->card = card;
} else { } else {
new_stack = initialize_stack(); initialize_stack(&new_stack);
new_stack->card = card; new_stack->card = card;
new_stack->next = (*stack); new_stack->next = (*stack);
*stack = new_stack; *stack = new_stack;
} }
} }
/* FIXME: uglyness inside if statement */
struct stack *pop(struct stack **stack) { struct stack *pop(struct stack **stack) {
struct stack *popped_entry = NULL; struct stack *popped_entry = NULL;
if(!empty(*stack)) { if(!empty(*stack)) {
popped_entry = *stack; popped_entry = *stack;
*stack = (*stack)->next; if (length(*stack) == 1) {
initialize_stack(stack);
} else {
*stack = (*stack)->next;
}
popped_entry->next = NULL; popped_entry->next = NULL;
} }

View File

@ -6,7 +6,7 @@ struct stack {
struct stack *next; struct stack *next;
}; };
struct stack *initialize_stack(); void initialize_stack(struct stack **);
bool empty(struct stack *); bool empty(struct stack *);
int length(struct stack *); int length(struct stack *);
void push(struct stack **, struct card *); void push(struct stack **, struct card *);