Non-destructive stack#reverse.

This commit is contained in:
Murilo Pereira
2011-05-01 01:13:15 -03:00
parent 5a62ecb955
commit 0e0c44f5c6
3 changed files with 30 additions and 9 deletions

View File

@@ -95,17 +95,17 @@ struct stack *pop(struct stack **stack) {
return(popped_entry);
}
struct stack *reverse(struct stack **stack) {
if (length(*stack) > 1) {
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);
while (!empty(stack)) {
push(&tmp_stack, pop((&stack))->card);
}
return(tmp_stack);
} else {
return(*stack);
return(stack);
}
}

View File

@@ -17,6 +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 **);
struct stack *reverse(struct stack *);
#endif