- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/evaluation/c-stack.c
r604e76d r860f19f 3 3 4 4 struct stack_node { 5 void * value;6 struct stack_node * next;5 void * value; 6 struct stack_node * next; 7 7 }; 8 8 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; 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; 24 12 next = crnt->next; 25 free_el( crnt->value);26 free( crnt);13 free_el( crnt->value ); 14 free( crnt ); 27 15 } 28 16 s->head = NULL; 29 17 } 30 18 31 _Bool stack_empty(const struct stack* s) { return s->head == NULL;}19 struct stack new_stack() { return (struct stack){ NULL }; /***/ } 32 20 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 }; /***/ 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 }; /***/ 36 43 s->head = n; 37 44 } 38 45 39 void * pop_stack(struct stack* s) {40 struct stack_node * n = s->head;46 void * pop_stack( struct stack * s ) { 47 struct stack_node * n = s->head; 41 48 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; 45 52 }
Note:
See TracChangeset
for help on using the changeset viewer.