Added stack#reverse.

This commit is contained in:
Murilo Pereira
2011-04-30 22:13:41 -03:00
parent 9ed28d9d51
commit 5a62ecb955
3 changed files with 80 additions and 0 deletions

View File

@@ -94,3 +94,18 @@ struct stack *pop(struct stack **stack) {
return(popped_entry);
}
struct stack *reverse(struct stack **stack) {
if (length(*stack) > 1) {
struct stack *tmp_stack;
allocate_stack(&tmp_stack);
initialize_stack(tmp_stack);
while (!empty(*stack)) {
push(&tmp_stack, pop((stack))->card);
}
return(tmp_stack);
} else {
return(*stack);
}
}

View File

@@ -17,5 +17,6 @@ bool empty(struct stack *);
int length(struct stack *);
void push(struct stack **, struct card *);
struct stack *pop(struct stack **);
struct stack *reverse(struct stack **);
#endif