Ignore:
File:
1 edited

Legend:

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

    r604e76d r860f19f  
    33
    44struct stack_node {
    5         void* value;
    6         struct stack_node* next;
     5        void * value;
     6        struct stack_node * next;
    77};
    88
    9 struct stack new_stack() { return (struct stack){ NULL }; /***/ }
    10 
    11 void 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 
    21 void clear_stack(struct stack* s, void (*free_el)(void*)) {
    22     for ( struct stack_node* next = s->head; next; ) {
    23                 struct stack_node* crnt = next;
     9void clear_stack( struct stack * s, void (*free_el)( void * ) ) {
     10        for ( struct stack_node * next = s->head; next; ) {
     11                struct stack_node * crnt = next;
    2412                next = crnt->next;
    25                 free_el(crnt->value);
    26                 free(crnt);
     13                free_el( crnt->value );
     14                free( crnt );
    2715        }
    2816        s->head = NULL;
    2917}
    3018
    31 _Bool stack_empty(const struct stack* s) { return s->head == NULL; }
     19struct stack new_stack() { return (struct stack){ NULL }; /***/ }
    3220
    33 void 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 }; /***/
     21void 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}
     30struct 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
     40void 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 }; /***/
    3643        s->head = n;
    3744}
    3845
    39 void* pop_stack(struct stack* s) {
    40         struct stack_node* n = s->head;
     46void * pop_stack( struct stack * s ) {
     47        struct stack_node * n = s->head;
    4148        s->head = n->next;
    42         void* x = n->value;
    43         free(n);
    44         return x;
     49        void * v = n->value;
     50        free( n );
     51        return v;
    4552}
Note: See TracChangeset for help on using the changeset viewer.