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 "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;
if (length(*stack) == 1) {
initialize_stack(stack);
} else {
*stack = (*stack)->next;
}
popped_entry->next = NULL;
}

View File

@ -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 *);