diff --git a/stack.c b/stack.c index 1d8d39ba8fa46b0c5a67709b43e8ee9f2053efa0..f92a89b3f51572ccdc75de875bee8a73d66b9500 100644 --- a/stack.c +++ b/stack.c @@ -15,19 +15,37 @@ void stack_init(stack *s, int size) { bool stack_is_empty(stack s) { bool is_empty = true; + if (s.top >= 0 && s.size >= 0 && s.data[0] >= 0) { + is_empty = false; + } + return is_empty; } + void stack_push(stack *s, int val) { + int next_top = s->top + 1; + // Push the value if the stack is not full + if (next_top < s->size && s->data[next_top] < 0) { + s->data[next_top] = val; + s->top = next_top; + } } void stack_pop(stack *s, int *val) { - + if (s->top >= 0) { + *val = s->data[s->top]; + s->data[s->top] = -1; + s->top -= 1; + s->size -= 1; + } } void stack_peek(stack s, int *val) { - + if (s.size > 0) { + *val = s.data[s.top]; + } } void stack_destroy(stack *s) { diff --git a/test.c b/test.c index 380eb1ee582d3f64dcc3a7db33186e17a0d4ef41..5faf01a13a4e8032cdb51b8dfec57473867da730 100644 --- a/test.c +++ b/test.c @@ -18,7 +18,6 @@ void test_stack_init(stack *s, int size) { void test_stack_destroy(stack *s) { printf("Testing stack_destroy()...\n"); stack_destroy(s); - // TODO: Verify if s is already null or not assert(s->data == NULL); assert(s->top == -1); printf("OK.\n"); @@ -40,7 +39,7 @@ void test_stack_peek(stack s, int *val) { printf("Testing stack_peek()...\n"); stack_peek(s, val); // TODO: Verify test - assert(val == &s.top); + assert(*val == s.data[s.top]); printf("OK.\n"); } @@ -48,14 +47,14 @@ void test_stack_push(stack *s, int val) { printf("Testing stack_push()...\n"); stack_push(s, val); // Testing push with remaining spaces - assert(s->top == val); + assert(s->data[s->top] == val); // Testing push on full stack - stack *stack_full; + stack stack_full; int value_top_full_stack_value = 100; - stack_init(stack_full, 1); - stack_push(stack_full, value_top_full_stack_value); - stack_push(stack_full, val); - assert(stack_full->top == value_top_full_stack_value); + stack_init(&stack_full, 1); + stack_push(&stack_full, value_top_full_stack_value); + stack_push(&stack_full, val); + assert(stack_full.data[stack_full.top] == value_top_full_stack_value); printf("OK.\n"); } @@ -67,13 +66,12 @@ void test_stack_pop(stack *s, int *val) { // Add a value if the stack is empty stack_push(s, 5); } - top_before_pop = s->top; + top_before_pop = s->data[s->top]; stack_pop(s, val); - // TODO: Verify test - assert(val == &top_before_pop); + assert(*val == top_before_pop); // Clear the stack - while(s->size != 0) { + while(s->top >= 0) { stack_pop(s, val); } // Test pop when stack is empty @@ -87,7 +85,7 @@ void test_stack_pop(stack *s, int *val) { int main() { stack s, non_empty_stack; int size = 10, value_to_add = 7; - int *val; + int val; printf("Starting the tests...\n"); // Test init and destroy test_stack_init(&s, size); @@ -97,14 +95,14 @@ int main() { stack_init(&s, size); stack_init(&non_empty_stack, 3); non_empty_stack.data[0] = 1; - non_empty_stack.top = non_empty_stack.data[0]; + non_empty_stack.top = 0; // Test the other functions test_stack_is_empty(&s); test_stack_is_not_empty(&non_empty_stack); - test_stack_peek(s, val); + test_stack_peek(s, &val); test_stack_push(&s, value_to_add); - test_stack_pop(&s, val); + test_stack_pop(&s, &val); // Free the memory stack_destroy(&s);