diff options
| author | mo khan <mo.khan@gmail.com> | 2020-08-09 15:47:03 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-08-09 15:47:03 -0600 |
| commit | 7c4da6d24d54a971a4e918c3a327e052f4685ccb (patch) | |
| tree | bf2e8cca2a93c0605a54c5218caea446e9c0833c /src | |
| parent | ce1f797f4af5aee2574a021fd24c0c7405870f45 (diff) | |
Allow storing void* on Stack
Diffstat (limited to 'src')
| -rw-r--r-- | src/02/05/stack.c | 16 | ||||
| -rw-r--r-- | src/02/05/stack.h | 8 | ||||
| -rw-r--r-- | src/02/05/stack_test.c | 30 |
3 files changed, 34 insertions, 20 deletions
diff --git a/src/02/05/stack.c b/src/02/05/stack.c index cbfd91e..ba3655d 100644 --- a/src/02/05/stack.c +++ b/src/02/05/stack.c @@ -1,7 +1,7 @@ #include "stack.h" #include <stdlib.h> -Node *node_init(int data) { +Node *node_init(void *data) { Node *node = malloc(sizeof(Node)); node->next = NULL; node->data = data; @@ -18,7 +18,7 @@ int stack_size(Stack *self) { if (!self || !self->head) return 0; - int count; + int count = 0; Node *current = self->head; while (current) { ++count; @@ -28,25 +28,25 @@ int stack_size(Stack *self) { return count; } -int stack_peek(Stack *self) { +void *stack_peek(Stack *self) { if (self->head) return self->head->data; - return -1; + return NULL; } -void stack_push(Stack *stack, int data) { +void stack_push(Stack *stack, void *data) { Node *node = node_init(data); node->next = stack->head; stack->head = node; } -int stack_pop(Stack *self) { +void *stack_pop(Stack *self) { if (self->head) { Node *tmp = self->head; - int data = tmp->data; + void *data = tmp->data; self->head = self->head->next; free(tmp); return data; } - return -1; + return NULL; } diff --git a/src/02/05/stack.h b/src/02/05/stack.h index a3f424e..0ac9ee8 100644 --- a/src/02/05/stack.h +++ b/src/02/05/stack.h @@ -1,6 +1,6 @@ typedef struct node { struct node *next; - int data; + void *data; } Node; typedef struct { @@ -8,7 +8,7 @@ typedef struct { } Stack; Stack *stack_init(); -int stack_peek(Stack *self); +void *stack_peek(Stack *self); int stack_size(Stack *self); -void stack_push(Stack *self, int data); -int stack_pop(Stack *self); +void stack_push(Stack *self, void *data); +void *stack_pop(Stack *self); diff --git a/src/02/05/stack_test.c b/src/02/05/stack_test.c index 5c74290..f53fe58 100644 --- a/src/02/05/stack_test.c +++ b/src/02/05/stack_test.c @@ -9,7 +9,7 @@ AfterEach(Stack) {} Ensure(Stack, when_pushing_an_item_on_to_a_stack) { Stack *stack = stack_init(); - stack_push(stack, 10); + stack_push(stack, (void *)10); assert_that(stack_size(stack), is_equal_to(1)); assert_that(stack_peek(stack), is_equal_to(10)); @@ -18,22 +18,36 @@ Ensure(Stack, when_pushing_an_item_on_to_a_stack) { Ensure(Stack, when_pushing_multiple_items_on_to_a_stack) { Stack *stack = stack_init(); - stack_push(stack, 20); - stack_push(stack, 10); - stack_push(stack, 50); + stack_push(stack, (void *)20); + stack_push(stack, (void *)10); + stack_push(stack, (void *)50); assert_that(stack_size(stack), is_equal_to(3)); assert_that(stack_peek(stack), is_equal_to(50)); } +Ensure(Stack, when_pushing_a_custom_type_on_to_a_stack) { + typedef struct { + } Item; + + Stack *stack = stack_init(); + Item *item = malloc(sizeof(Item)); + + stack_push(stack, item); + + assert_that(stack_size(stack), is_equal_to(1)); + assert_that(stack_peek(stack), is_equal_to(item)); + assert_that(stack_pop(stack), is_equal_to(item)); +} + Ensure(Stack, when_popping_an_item_off_of_a_stack) { Stack *stack = stack_init(); - stack_push(stack, 20); - stack_push(stack, 10); - stack_push(stack, 50); + stack_push(stack, (void *)20); + stack_push(stack, (void *)10); + stack_push(stack, (void *)50); - int result = stack_pop(stack); + void *result = stack_pop(stack); assert_that(stack_size(stack), is_equal_to(2)); assert_that(stack_peek(stack), is_equal_to(10)); |
