Non-destructive stack#reverse.
This commit is contained in:
parent
5a62ecb955
commit
0e0c44f5c6
10
lib/stack.c
10
lib/stack.c
@ -95,17 +95,17 @@ struct stack *pop(struct stack **stack) {
|
|||||||
return(popped_entry);
|
return(popped_entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stack *reverse(struct stack **stack) {
|
struct stack *reverse(struct stack *stack) {
|
||||||
if (length(*stack) > 1) {
|
if (length(stack) > 1) {
|
||||||
struct stack *tmp_stack;
|
struct stack *tmp_stack;
|
||||||
|
|
||||||
allocate_stack(&tmp_stack);
|
allocate_stack(&tmp_stack);
|
||||||
initialize_stack(tmp_stack);
|
initialize_stack(tmp_stack);
|
||||||
while (!empty(*stack)) {
|
while (!empty(stack)) {
|
||||||
push(&tmp_stack, pop((stack))->card);
|
push(&tmp_stack, pop((&stack))->card);
|
||||||
}
|
}
|
||||||
return(tmp_stack);
|
return(tmp_stack);
|
||||||
} else {
|
} else {
|
||||||
return(*stack);
|
return(stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,6 @@ bool empty(struct stack *);
|
|||||||
int length(struct stack *);
|
int length(struct stack *);
|
||||||
void push(struct stack **, struct card *);
|
void push(struct stack **, struct card *);
|
||||||
struct stack *pop(struct stack **);
|
struct stack *pop(struct stack **);
|
||||||
struct stack *reverse(struct stack **);
|
struct stack *reverse(struct stack *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -222,7 +222,7 @@ void test_reverse_on_empty_stack() {
|
|||||||
allocate_stack(&stack);
|
allocate_stack(&stack);
|
||||||
initialize_stack(stack);
|
initialize_stack(stack);
|
||||||
old_stack = stack;
|
old_stack = stack;
|
||||||
reversed_stack = reverse(&stack);
|
reversed_stack = reverse(stack);
|
||||||
|
|
||||||
assert(reversed_stack == old_stack);
|
assert(reversed_stack == old_stack);
|
||||||
|
|
||||||
@ -241,7 +241,7 @@ void test_reverse_on_stack_with_one_element() {
|
|||||||
initialize_stack(stack);
|
initialize_stack(stack);
|
||||||
push(&stack, card);
|
push(&stack, card);
|
||||||
old_stack = stack;
|
old_stack = stack;
|
||||||
reversed_stack = reverse(&stack);
|
reversed_stack = reverse(stack);
|
||||||
|
|
||||||
assert(reversed_stack == old_stack);
|
assert(reversed_stack == old_stack);
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ void test_reverse_on_stack_with_more_than_one_element() {
|
|||||||
push(&stack, card[i]);
|
push(&stack, card[i]);
|
||||||
}
|
}
|
||||||
old_stack = duplicate_stack(stack);
|
old_stack = duplicate_stack(stack);
|
||||||
reversed_stack = reverse(&stack);
|
reversed_stack = reverse(stack);
|
||||||
|
|
||||||
allocate_stack(&unreversed_stack);
|
allocate_stack(&unreversed_stack);
|
||||||
initialize_stack(unreversed_stack);
|
initialize_stack(unreversed_stack);
|
||||||
@ -275,6 +275,26 @@ void test_reverse_on_stack_with_more_than_one_element() {
|
|||||||
free_stack(stack);
|
free_stack(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_reverse_should_not_change_stack() {
|
||||||
|
struct stack *stack, *old_stack;
|
||||||
|
struct card *card[3];
|
||||||
|
|
||||||
|
allocate_stack(&stack);
|
||||||
|
initialize_stack(stack);
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
allocate_card(&card[i]);
|
||||||
|
initialize_card(card[i]);
|
||||||
|
set_card(card[i], TWO + i, DIAMONDS + i, EXPOSED, 0, 0);
|
||||||
|
push(&stack, card[i]);
|
||||||
|
}
|
||||||
|
old_stack = stack;
|
||||||
|
reverse(stack);
|
||||||
|
|
||||||
|
assert(stack == old_stack);
|
||||||
|
|
||||||
|
free_stack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
void test_stack() {
|
void test_stack() {
|
||||||
test_initialize_stack();
|
test_initialize_stack();
|
||||||
|
|
||||||
@ -294,6 +314,7 @@ void test_stack() {
|
|||||||
test_reverse_on_empty_stack();
|
test_reverse_on_empty_stack();
|
||||||
test_reverse_on_stack_with_one_element();
|
test_reverse_on_stack_with_one_element();
|
||||||
test_reverse_on_stack_with_more_than_one_element();
|
test_reverse_on_stack_with_more_than_one_element();
|
||||||
|
test_reverse_should_not_change_stack();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user