Adding dfs example and fixing other examples

This commit is contained in:
Leonardo Vencovsky committed 2020-06-24 21:55:20 -03:00
1 parent 4d4f1256ee
commit fa2e3d1fcd
4 files changed
+122 -36

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
/**
* Example of In-Order Depth-First Search using:
* - A Stack
* - Recursion
* - Without both
*/
#define CMC_CAMEL_CASE
#include "macro_collections.h"
C_MACRO_COLLECTIONS_GEN(CMC, TREESET, (tree, BinaryTree, , , int32_t), (ITER, STR))
#define MYSTACK (stk, Stack, , , struct BinaryTreeNode *)
C_MACRO_COLLECTIONS(CMC, STACK, CORE, STRUCT, MYSTACK)
C_MACRO_COLLECTIONS(CMC, STACK, CORE, HEADER, MYSTACK)
C_MACRO_COLLECTIONS(CMC, STACK, CORE, SOURCE, MYSTACK)
void DFS(struct BinaryTreeNode *root)
{
if (!root)
return;
DFS(root->left);
fprintf(stdout, "%d ", root->value);
DFS(root->right);
}
int main(void)
{
struct BinaryTree *tree = tree_new(&(struct BinaryTreeFVal){ .cmp = cmc_i32_cmp, .str = cmc_i32_str, NULL });
for (int i = 1; i <= 10; i++)
tree_insert(tree, i);
int32_t result = 1;
cmc_assert(tree_min(tree, &result));
cmc_assert_equals(int32_t, 1, result);
cmc_assert(tree_max(tree, &result));
cmc_assert_equals(int32_t, 10, result);
printf("Without Recursion or Stack:\n");
tree_print(tree, stdout, "[ ", " ", " ]\n");
printf("Recursion:\n");
fprintf(stdout, "[ ");
DFS(tree->root);
fprintf(stdout, "]\n");
printf("Stack:\n");
fprintf(stdout, "[ ");
struct Stack *stack = stk_new(100, &(struct StackFVal){ NULL });
struct BinaryTreeNode *node = tree->root;
while (node || !stk_empty(stack))
{
if (node)
{
if (!stk_push(stack, node))
cmc_log_error("Could not push to stack %d\n", cmc_flags_to_str[stk_flag(stack)]);
node = node->left;
}
else
{
node = stk_top(stack);
if (!stk_pop(stack))
cmc_log_error("Could not pop from the stack %s\n", cmc_flags_to_str[stk_flag(stack)]);
fprintf(stdout, "%d ", node->value);
node = node->right;
}
}
stk_free(stack);
fprintf(stdout, "]\n");
tree_free(tree);
}
+14 -14
View File
@@ -1,14 +1,17 @@
#include <cmc/stack.h>
#include <utl/log.h>
#include "macro_collections.h"
CMC_GENERATE_STACK(stk, stack, int)
#define MYSTACK (stk, stack, , , int)
C_MACRO_COLLECTIONS(CMC, STACK, CORE, STRUCT, MYSTACK)
C_MACRO_COLLECTIONS(CMC, STACK, CORE, HEADER, MYSTACK)
C_MACRO_COLLECTIONS(CMC, STACK, CORE, SOURCE, MYSTACK)
struct
{
struct stack *from;
struct stack *aux;
struct stack *to;
} print_aux = {NULL};
} print_aux = { NULL };
void print_stacks()
{
@@ -18,10 +21,7 @@ void print_stacks()
int ib = print_aux.aux->buffer[i];
int ic = print_aux.to->buffer[i];
printf("| %c | %c | %c |\n",
ia == 0 ? 32 : ia + 48,
ib == 0 ? 32 : ib + 48,
ic == 0 ? 32 : ic + 48);
printf("| %c | %c | %c |\n", ia == 0 ? 32 : ia + 48, ib == 0 ? 32 : ib + 48, ic == 0 ? 32 : ic + 48);
}
printf("+---+---+---+\n\n");
@@ -50,9 +50,9 @@ void hanoi(int n, struct stack *from, struct stack *aux, struct stack *to)
int main(int argc, char const *argv[])
{
struct stack *from = stk_new(10);
struct stack *to = stk_new(10);
struct stack *aux = stk_new(10);
struct stack *from = stk_new(10, &(struct stack_fval){ NULL });
struct stack *to = stk_new(10, &(struct stack_fval){ NULL });
struct stack *aux = stk_new(10, &(struct stack_fval){ NULL });
for (int i = 4; i >= 1; i--)
stk_push(from, i);
@@ -65,9 +65,9 @@ int main(int argc, char const *argv[])
hanoi(stk_count(from), from, aux, to);
stk_free(from, NULL);
stk_free(to, NULL);
stk_free(aux, NULL);
stk_free(from);
stk_free(to);
stk_free(aux);
return 0;
}
+20 -21
View File
@@ -1,5 +1,6 @@
#include <cmc/list.h>
#include <utl/log.h>
// Prints a file in the terminal separated by pages
#include "macro_collections.h"
#define TAB_SIZE 4
#define ROW_INI_CAP DOC_LETTERS_IN_ROW
@@ -7,13 +8,11 @@
#define DOC_ROWS_IN_PAGE 50
#define DOC_MARGIN 4
CMC_GENERATE_LIST(row, row, char)
CMC_GENERATE_LIST(doc, document, struct row *)
C_MACRO_COLLECTIONS_ALL(CMC, LIST, (row, row, , , char))
C_MACRO_COLLECTIONS_ALL(CMC, LIST, (doc, document, , , struct row *))
void row_deallocator(struct row *r)
{
row_free(r, NULL);
}
struct row_fval *row_funcs = &(struct row_fval){ NULL };
struct document_fval *doc_funcs = &(struct document_fval){ .free = row_free };
struct document *document_load(const char *file_name);
void document_empty_line(FILE *fptr);
@@ -31,9 +30,9 @@ struct document *document_load(const char *file_name)
return NULL;
}
struct row *initial_row = row_new(ROW_INI_CAP);
struct row *initial_row = row_new(ROW_INI_CAP, row_funcs);
struct document *doc = doc_new(100);
struct document *doc = doc_new(100, doc_funcs);
doc_push_back(doc, initial_row);
int ch;
@@ -46,7 +45,7 @@ struct document *document_load(const char *file_name)
if (c == '\n')
{
struct row *new_row = row_new(ROW_INI_CAP);
struct row *new_row = row_new(ROW_INI_CAP, row_funcs);
doc_push_back(doc, new_row);
}
else if (c == '\t')
@@ -61,7 +60,7 @@ struct document *document_load(const char *file_name)
if (left > 0)
{
struct row *new_row = row_new(ROW_INI_CAP);
struct row *new_row = row_new(ROW_INI_CAP, row_funcs);
doc_push_back(doc, new_row);
while (left > 0)
@@ -73,7 +72,7 @@ struct document *document_load(const char *file_name)
}
else if (DOC_LETTERS_IN_ROW == row_count(current_row))
{
struct row *new_row = row_new(ROW_INI_CAP);
struct row *new_row = row_new(ROW_INI_CAP, row_funcs);
row_push_back(new_row, c);
doc_push_back(doc, new_row);
}
@@ -86,12 +85,12 @@ struct document *document_load(const char *file_name)
void document_empty_line(FILE *fptr)
{
fputs("|", fptr);
fputs("\t|", fptr);
for (size_t i = 0; i < DOC_LETTERS_IN_ROW + DOC_MARGIN * 2; i++)
{
fputc(' ', fptr);
}
fputs("|\n", fptr);
fputs("\t|\n", fptr);
}
void document_page_outline(FILE *fptr, int outline /* 0 - header, 1 - footer */)
@@ -102,7 +101,7 @@ void document_page_outline(FILE *fptr, int outline /* 0 - header, 1 - footer */)
#ifdef DOC_PAGE_NUMBER
static intmax_t page = 1;
fputs("|", fptr);
fputs("\t|", fptr);
for (size_t i = 0; i < (DOC_LETTERS_IN_ROW + DOC_MARGIN) - 8; i++)
{
fputc(' ', fptr);
@@ -116,8 +115,8 @@ void document_page_outline(FILE *fptr, int outline /* 0 - header, 1 - footer */)
#endif
}
fputs("+", fptr);
for (size_t i = 0; i < DOC_LETTERS_IN_ROW + DOC_MARGIN * 2; i++)
fputs("\t+", fptr);
for (size_t i = 0; i < DOC_LETTERS_IN_ROW + DOC_MARGIN * 2 + 3; i++)
{
fputc('-', fptr);
}
@@ -130,7 +129,7 @@ void document_page_outline(FILE *fptr, int outline /* 0 - header, 1 - footer */)
void document_page_margin(FILE *fptr, int margin /* 0 - first margin, 1 - second margin */)
{
if (margin == 0)
fputs("|", fptr);
fputs("\t|", fptr);
for (size_t i = 0; i < DOC_MARGIN; i++)
{
@@ -138,7 +137,7 @@ void document_page_margin(FILE *fptr, int margin /* 0 - first margin, 1 - second
}
if (margin == 1)
fputs("|\n", fptr);
fputs("\t|\n", fptr);
}
void document_print(struct document *doc, FILE *fptr)
@@ -202,7 +201,7 @@ int main(int argc, char const *argv[])
document_print(doc, stdout);
doc_free(doc, row_deallocator);
doc_free(doc);
return 0;
}
+1 -1
View File
@@ -1,5 +1,5 @@
#include <math.h>
#include "cmc/bitset.h"
#include <math.h>
CMC_CMC_BITSET_CORE(PUBLIC, HEADER, (bs, bitset))
CMC_CMC_BITSET_CORE(PUBLIC, SOURCE, (bs, bitset))