Added stack#reverse.
This commit is contained in:
parent
9ed28d9d51
commit
5a62ecb955
15
lib/stack.c
15
lib/stack.c
@ -94,3 +94,18 @@ struct stack *pop(struct stack **stack) {
|
|||||||
|
|
||||||
return(popped_entry);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -17,5 +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 **);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include "test_helper.h"
|
||||||
#include "../lib/stack.h"
|
#include "../lib/stack.h"
|
||||||
|
|
||||||
void test_initialize_stack() {
|
void test_initialize_stack() {
|
||||||
@ -215,6 +216,65 @@ void test_pop_on_stack_with_more_than_one_element() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_reverse_on_empty_stack() {
|
||||||
|
struct stack *stack, *old_stack, *reversed_stack;
|
||||||
|
|
||||||
|
allocate_stack(&stack);
|
||||||
|
initialize_stack(stack);
|
||||||
|
old_stack = stack;
|
||||||
|
reversed_stack = reverse(&stack);
|
||||||
|
|
||||||
|
assert(reversed_stack == old_stack);
|
||||||
|
|
||||||
|
free_stack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_reverse_on_stack_with_one_element() {
|
||||||
|
struct stack *stack, *old_stack, *reversed_stack;
|
||||||
|
struct card *card;
|
||||||
|
|
||||||
|
allocate_card(&card);
|
||||||
|
initialize_card(card);
|
||||||
|
set_card(card, ACE, SPADES, EXPOSED, 0, 0);
|
||||||
|
|
||||||
|
allocate_stack(&stack);
|
||||||
|
initialize_stack(stack);
|
||||||
|
push(&stack, card);
|
||||||
|
old_stack = stack;
|
||||||
|
reversed_stack = reverse(&stack);
|
||||||
|
|
||||||
|
assert(reversed_stack == old_stack);
|
||||||
|
|
||||||
|
free_stack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_reverse_on_stack_with_more_than_one_element() {
|
||||||
|
struct stack *stack, *old_stack, *reversed_stack, *unreversed_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 = duplicate_stack(stack);
|
||||||
|
reversed_stack = reverse(&stack);
|
||||||
|
|
||||||
|
allocate_stack(&unreversed_stack);
|
||||||
|
initialize_stack(unreversed_stack);
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
push(&unreversed_stack, pop(&reversed_stack)->card);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(stacks_equal(unreversed_stack, old_stack));
|
||||||
|
|
||||||
|
free_stack(reversed_stack);
|
||||||
|
free_stack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
void test_stack() {
|
void test_stack() {
|
||||||
test_initialize_stack();
|
test_initialize_stack();
|
||||||
|
|
||||||
@ -231,5 +291,9 @@ void test_stack() {
|
|||||||
test_pop_on_stack_with_one_element();
|
test_pop_on_stack_with_one_element();
|
||||||
test_pop_on_stack_with_more_than_one_element();
|
test_pop_on_stack_with_more_than_one_element();
|
||||||
|
|
||||||
|
test_reverse_on_empty_stack();
|
||||||
|
test_reverse_on_stack_with_one_element();
|
||||||
|
test_reverse_on_stack_with_more_than_one_element();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user