- File:
-
- 1 edited
-
doc/papers/general/evaluation/c-stack.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/evaluation/c-stack.c
r4c80a75 r860f19f 2 2 #include "c-stack.h" 3 3 4 typedef structnode {4 struct stack_node { 5 5 void * value; 6 struct node * next;7 } node;6 struct stack_node * next; 7 }; 8 8 9 void copy_stack( stack * s, const stack * t, void * (*copy)( const void * ) ) { 10 node ** cr = &s->head; 11 for ( node * nx = t->head; nx; nx = nx->next ) { 12 *cr = malloc( sizeof(node) ); /***/ 13 (*cr)->value = copy( nx->value ); 14 cr = &(*cr)->next; 15 } 16 *cr = NULL; 17 } 18 19 void clear_stack( stack * s, void (* free_el)( void * ) ) { 20 for ( node * nx = s->head; nx; ) { 21 node * cr = nx; 22 nx = cr->next; 23 free_el( cr->value ); 24 free( cr ); 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; 12 next = crnt->next; 13 free_el( crnt->value ); 14 free( crnt ); 25 15 } 26 16 s->head = NULL; 27 17 } 28 18 29 stack new_stack() { 30 return (stack){ NULL }; /***/ 19 struct stack new_stack() { return (struct stack){ NULL }; /***/ } 20 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; 31 29 } 32 33 stack * assign_stack( stack * s, const stack * t, 30 struct stack * assign_stack( struct stack * s, const struct stack * t, 34 31 void * (*copy_el)( const void * ), void (*free_el)( void * ) ) { 35 32 if ( s->head == t->head ) return s; … … 39 36 } 40 37 41 _Bool stack_empty( const stack * s ) { 42 return s->head == NULL; 43 } 38 _Bool stack_empty( const struct stack * s ) { return s->head == NULL; } 44 39 45 void push_stack( st ack * s, void * v ) {46 node * n = malloc( sizeof(node) ); /***/47 *n = ( node){ v, s->head }; /***/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 }; /***/ 48 43 s->head = n; 49 44 } 50 45 51 void * pop_stack( st ack * s ) {52 node * n = s->head;46 void * pop_stack( struct stack * s ) { 47 struct stack_node * n = s->head; 53 48 s->head = n->next; 54 49 void * v = n->value;
Note:
See TracChangeset
for help on using the changeset viewer.