Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/general/evaluation/c-stack.c

    r860f19f r604e76d  
    33
    44struct stack_node {
    5         void * value;
    6         struct stack_node * next;
     5        void* value;
     6        struct stack_node* next;
    77};
    88
    9 void clear_stack( struct stack * s, void (*free_el)( void * ) ) {
    10         for ( struct stack_node * next = s->head; next; ) {
    11                 struct stack_node * crnt = next;
     9struct stack new_stack() { return (struct stack){ NULL }; /***/ }
     10
     11void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const void*)) {
     12        struct stack_node** crnt = &s->head;
     13        for ( struct stack_node* next = t->head; next; next = next->next ) {
     14                *crnt = malloc(sizeof(struct stack_node)); /***/
     15                **crnt = (struct stack_node){ copy(next->value) }; /***/
     16                crnt = &(*crnt)->next;
     17        }
     18        *crnt = 0;
     19}
     20
     21void clear_stack(struct stack* s, void (*free_el)(void*)) {
     22    for ( struct stack_node* next = s->head; next; ) {
     23                struct stack_node* crnt = next;
    1224                next = crnt->next;
    13                 free_el( crnt->value );
    14                 free( crnt );
     25                free_el(crnt->value);
     26                free(crnt);
    1527        }
    1628        s->head = NULL;
    1729}
    1830
    19 struct stack new_stack() { return (struct stack){ NULL }; /***/ }
     31_Bool stack_empty(const struct stack* s) { return s->head == NULL; }
    2032
    21 void copy_stack( struct stack * s, const struct stack * t, void * (*copy)( const void * ) ) {
    22         struct stack_node ** crnt = &s->head;
    23         for ( struct stack_node * next = t->head; next; next = next->next ) {
    24                 *crnt = malloc( sizeof(struct stack_node) ); /***/
    25                 (*crnt)->value = copy( next->value );
    26                 crnt = &(*crnt)->next;
    27         }
    28         *crnt = NULL;
    29 }
    30 struct stack * assign_stack( struct stack * s, const struct stack * t,
    31                 void * (*copy_el)( const void * ), void (*free_el)( void * ) ) {
    32         if ( s->head == t->head ) return s;
    33         clear_stack( s, free_el ); /***/
    34         copy_stack( s, t, copy_el ); /***/
    35         return s;
    36 }
    37 
    38 _Bool stack_empty( const struct stack * s ) { return s->head == NULL; }
    39 
    40 void push_stack( struct stack * s, void * v ) {
    41         struct stack_node * n = malloc( sizeof(struct stack_node) ); /***/
    42         *n = (struct stack_node){ v, s->head }; /***/
     33void push_stack(struct stack* s, void* value) {
     34        struct stack_node* n = malloc(sizeof(struct stack_node)); /***/
     35        *n = (struct stack_node){ value, s->head }; /***/
    4336        s->head = n;
    4437}
    4538
    46 void * pop_stack( struct stack * s ) {
    47         struct stack_node * n = s->head;
     39void* pop_stack(struct stack* s) {
     40        struct stack_node* n = s->head;
    4841        s->head = n->next;
    49         void * v = n->value;
    50         free( n );
    51         return v;
     42        void* x = n->value;
     43        free(n);
     44        return x;
    5245}
Note: See TracChangeset for help on using the changeset viewer.