Skip to content

Commit

Permalink
feat: Add free for allocated structures and print
Browse files Browse the repository at this point in the history
  • Loading branch information
Mino1289 committed Aug 11, 2023
1 parent 8c597c1 commit 31b7f07
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
26 changes: 26 additions & 0 deletions deck.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,29 @@ DeckStackStatus deck_set_At(Deck deck, Position pos, Stack* stack) {
}
return STACK_ALREADY_SET;
}

void print_Deck(Deck deck) {
printf("Deck size: %hhu x %hhu\n", deck.length, deck.width);
printf("Deck stack:\n");
for (int i = 0; i < deck.length; ++i) {
for (int j = 0; j < deck.width; ++j) {
if (deck.stack[i][j]) {
printf("%p ", (void*) deck.stack[i][j]);
} else {
printf("0x0 ");
}
}
printf("\n");
}
}

void free_Deck(Deck *deck) {
for (int i = 0; i < deck->length; ++i) {
for (int j = 0; j < deck->width; ++j) {
if (deck->stack[i][j]) {
free_Stack(deck->stack[i][j]);
}
}
free(deck->stack[i]);
}
}
4 changes: 4 additions & 0 deletions deck.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ Stack* deck_get_At(Deck deck, Position pos);

DeckStackStatus deck_set_At(Deck deck, Position pos, Stack* stack);

void print_Deck(Deck deck);

void free_Deck(Deck *deck);

#endif
6 changes: 6 additions & 0 deletions stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ Container stack_Pop(Stack* stack) {
}
return element;
}

void free_Stack(Stack *stack) {
if (stack->ctn) {
free(stack->ctn);
}
}
3 changes: 2 additions & 1 deletion stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ int stack_Size(Stack stack);

void stack_Push(Stack* stack, Container container);

Container stack_Pop(Stack* stack);
Container stack_Pop(Stack *stack);

void free_Stack(Stack *stack);


#endif

0 comments on commit 31b7f07

Please sign in to comment.