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