Skip to content
Gustav Louw edited this page Jan 13, 2021 · 4 revisions
#include <stk.h>

The CTL stk, analogous to the STL std::stack, is a container specializing in O(1) push (back) and pop (back) operations. A stk can be seen as a simplified deq, and therefor maintains all pointer validity and memory contiguousness properties of a deq.

Example: Storing and Summing Integers

#include <stdlib.h>
#include <stdio.h>

#define P
#define T int
#include <stk.h>

int main(void)
{
    stk_int a = stk_int_init();
    for(int i = 0; i < 16; i++)
        stk_int_push(&a, rand() % 1024);
    int sum = 0;
    while(!stk_int_empty(&a))
    {
        int* x = stk_int_top(&a);
        printf("%d\n", *x);
        sum += *x;
        stk_int_pop(&a);
    }
    printf("sum: %d\n", sum);
    stk_int_free(&a);
}
gcc test.c -I ctl

Memory ownership rules apply when #define P is omitted - simply declare functions T_free and T_copy, as per usual, where T is the type typedef.

Clone this wiki locally