Fixed empty() to use the new initialize_stack(), and pop().
This commit is contained in:
parent
e72d21f1f0
commit
8173aaa7ff
19
lib/stack.c
19
lib/stack.c
@ -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;
|
||||||
|
if (length(*stack) == 1) {
|
||||||
|
initialize_stack(stack);
|
||||||
|
} else {
|
||||||
*stack = (*stack)->next;
|
*stack = (*stack)->next;
|
||||||
|
}
|
||||||
popped_entry->next = NULL;
|
popped_entry->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 *);
|
||||||
|
Loading…
Reference in New Issue
Block a user