diff --git a/.gitignore b/.gitignore index 96249cede61f2cea6cf2a665bfce534ab0b1bee5..03b628520efecef7644a2cf21d681f74c89ada6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ + +# Object files *.o -main \ No newline at end of file +*.exe + +# Executables +main + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..cd8cbe3de0538b56de4803180b938b5f566f3eda --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +CC:=gcc +CFLAGS:=-g -Wall -Wextra -pedantic -fsanitize=address +LDFLGS:=-fsanitize=address +NAME:=stack + +$(NAME): main.o $(NAME).o + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + +test: $(NAME)_test.o $(NAME).o + $(CC) $(CFLAGS) -o $@ $^ + ./test + +$(NAME).o: $(NAME).h +$(NAME)_test.o: $(NAME).h + +.PHONY: clean + +clean: + rm -f *.o $(NAME) test diff --git a/README.md b/README.md index 6815811d225fd4f48c938f064a918fbd955db127..08c8b3b72280dc98ac3d6fe47aec6bf72e85fa42 100644 --- a/README.md +++ b/README.md @@ -1 +1,12 @@ -# stack +# Stack + +``` +📦project + ┣ 📜main.c + ┣ 📜stack.c + ┣ 📜stack.h + ┣ 📜.gitignore + ┣ 📜Makefile + ┗ 📜README.md + ``` + diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..a45a2057498b1fe78855dfb73523af18676cb365 --- /dev/null +++ b/main.c @@ -0,0 +1,7 @@ +#include "stack.h" +#include <stdio.h> +#include <stdlib.h> + +int main(void) { + return EXIT_SUCCESS; +} diff --git a/stack.c b/stack.c index 82e099b41ba63dd366bb28f8c4f558500b048589..1e331d9d7c88b04e9ebe8f06f97751e638cc237e 100644 --- a/stack.c +++ b/stack.c @@ -1,66 +1,64 @@ +#include "stack.h" #include <stdio.h> #include <stdlib.h> -#include "stack.h" #define DEFAULT_CAPACITY 4 -void stack_init(stack *s) -{ - s->top = -1; +void stack_init(stack *s) { + s->top = -1; s->capacity = DEFAULT_CAPACITY; - s->data = malloc(sizeof(int) * DEFAULT_CAPACITY); + s->data = malloc(sizeof(int) * DEFAULT_CAPACITY); } -void stack_destroy(stack *s){ +void stack_destroy(stack *s) { free(s->data); - s->data = NULL; + s->data = NULL; s->capacity = -1; - s->top = -1; + s->top = -1; } -void stack_pop(stack *s, int *value){ - if (stack_is_empty(*s)) { - return; - } - if (s->top == s->capacity/4){ - s->capacity /= 2; - s->data = realloc(s->data, sizeof(int)*s->capacity); - } +void stack_pop(stack *s, int *value) { + if (stack_is_empty(*s)) { + return; + } + if (s->top == s->capacity / 4) { + s->capacity /= 2; + s->data = realloc(s->data, sizeof(int) * s->capacity); + } - *value = s->data[s->top]; - s->top -= 1; + *value = s->data[s->top]; + s->top -= 1; } -void stack_peek(stack s, int *value){ +void stack_peek(stack s, int *value) { if (!stack_is_empty(s)) { *value = s.data[s.top]; } } void stack_print(const stack s) { - //TODO: replace if statement with following as soon as relevant function is implemented - //if (!stack_is_empty()) { - if (s.top >= 0) { - printf(" TOP\n--------------------\n"); - for (int* spot = s.data + s.top; spot >= s.data; --spot) { - printf("%8d | %12d\n", spot - s.data, *spot); - } - printf("--------------------\n BOTTOM\n"); - } else { - printf("STACK EMPTY\n"); - } + // TODO: replace if statement with following as soon as relevant function is + // implemented if (!stack_is_empty()) { + if (s.top >= 0) { + printf(" TOP\n--------------------\n"); + for (int *spot = s.data + s.top; spot >= s.data; --spot) { + printf("%8ld | %12d\n", spot - s.data, *spot); + } + printf("--------------------\n BOTTOM\n"); + } else { + printf("STACK EMPTY\n"); + } } void stack_clone(stack s, stack *clone) { - clone->top = s.top; + clone->top = s.top; clone->capacity = s.capacity; - clone->data = malloc(sizeof(int) * s.capacity); + clone->data = malloc(sizeof(int) * s.capacity); for (int i = 0; i <= s.top && i < s.capacity; i++) { clone->data[i] = s.data[i]; } } -int get_length(stack s) -{ +int get_length(stack s) { return s.top + 1; } diff --git a/stack.h b/stack.h index 22defdd8929236c7e8187c630ac81c4d77d5e138..5ee3740c9a8445a8fc3dccc9b12b7b7469ecbb8d 100644 --- a/stack.h +++ b/stack.h @@ -19,3 +19,4 @@ int get_length(stack s); void stack_print(const stack s); #endif + diff --git a/stack_test.c b/stack_test.c index cc4c4887d4235016ba81c6d9eb2125ffc59ce177..d3b20b4e149b4726e3d6eb38c2f89ca4db07b76a 100644 --- a/stack_test.c +++ b/stack_test.c @@ -1,5 +1,6 @@ #include "minunit.h" #include "stack.h" +#include <stdlib.h> MU_TEST(stack_init_test) { // Arrange @@ -20,8 +21,8 @@ MU_TEST_SUITE(stack_test_suite) { MU_RUN_TEST(stack_init_test); } -int main () { +int main() { MU_RUN_SUITE(stack_test_suite); MU_REPORT(); return MU_EXIT_CODE; -} \ No newline at end of file +}