diff --git a/arena_8h.html b/arena_8h.html index 3a9e435..13efb7a 100644 --- a/arena_8h.html +++ b/arena_8h.html @@ -365,7 +365,7 @@

Returns
A pointer to reallocated the memory chunk.
Return values
- +
NULLIf arena doesn't have enough memory for the reallocation.
NULLIf arena doesn't have enough memory for the reallocation or invalid parameters are given.
@@ -428,7 +428,7 @@

Returns
A pointer to reallocated the memory chunk.
Return values
- +
NULLIf arena doesn't have enough memory for the reallocation.
NULLIf arena doesn't have enough memory for the reallocation or invalid parameters are given.
diff --git a/examples_2fstack_2valid_parentheses_8c-example.html b/examples_2fstack_2valid_parentheses_8c-example.html index 0fe41a1..c93be23 100644 --- a/examples_2fstack_2valid_parentheses_8c-example.html +++ b/examples_2fstack_2valid_parentheses_8c-example.html @@ -88,7 +88,106 @@

Examples of how fstack.h header file is used in practice.

-
+
#include <stdint.h>
+
#include <stdio.h>
+
#include <string.h>
+
+
#define NAME paren_stk
+
#define VALUE_TYPE char
+
#include "fstack.h"
+
+
void print_paren_stk(struct paren_stk *stk)
+
{
+
if (stk->count > 0) {
+
printf("%c", paren_stk_at(stk, 0));
+
}
+
else {
+
printf("(empty)");
+
}
+
for (size_t i = 1; i < stk->count; i++) {
+
printf(" <- %c", paren_stk_at(stk, (uint32_t)i));
+
}
+
printf("\n");
+
}
+
+
char matching_paren(char c)
+
{
+
switch (c) {
+
case '(':
+
return ')';
+
case '{':
+
return '}';
+
case '[':
+
return ']';
+
case ')':
+
return '(';
+
case '}':
+
return '{';
+
case ']':
+
return '[';
+
}
+
return '\0';
+
}
+
+
bool isValid(char *s)
+
{
+
const size_t n = strlen(s);
+
struct paren_stk *stk = paren_stk_create((uint32_t)n);
+
+
bool retVal = true;
+
+
for (size_t i = 0; i < n; i++) {
+
switch (s[i]) {
+
case '(':
+
case '{':
+
case '[':
+
if (!paren_stk_is_full(stk)) {
+
paren_stk_push(stk, matching_paren(s[i]));
+
}
+
else {
+
assert(false);
+
}
+
break;
+
case ')':
+
case '}':
+
case ']':
+
retVal &= !paren_stk_is_empty(stk) && paren_stk_pop(stk) == s[i];
+
break;
+
default:
+
assert(false);
+
break;
+
}
+
}
+
retVal &= stk->count == 0;
+
+
paren_stk_destroy(stk);
+
+
return retVal;
+
}
+
+
#define print_and_assert(expr, expected) \
+
__extension__({ \
+
bool res = (expr); \
+
assert(res == (expected)); \
+
printf("%-17s = %s\n", #expr, res ? "true" : "false"); \
+
})
+
+
int main(void)
+
{
+
(void)(print_paren_stk);
+
+
print_and_assert(isValid("()"), true);
+
print_and_assert(isValid("("), false);
+
print_and_assert(isValid(")"), false);
+
print_and_assert(isValid("(]"), false);
+
print_and_assert(isValid("([]){}"), true);
+
print_and_assert(isValid("([){}"), false);
+
print_and_assert(isValid("{[][]}"), true);
+
print_and_assert(isValid("{[[]}"), false);
+
print_and_assert(isValid("{[(])}"), false);
+
}
+
Fixed-size array-based stack.
+