Changeset e84382b for doc/papers/general/evaluation/c-stack.c
- Timestamp:
- Mar 9, 2018, 1:34:04 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 860f19f
- Parents:
- b51e5fdb (diff), 3d8f2f8 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/evaluation/c-stack.c
rb51e5fdb re84382b 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 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 ); 15 } 16 s->head = NULL; 17 } 8 18 9 19 struct stack new_stack() { return (struct stack){ NULL }; /***/ } 10 20 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)->value = copy( next->value);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 ); 16 26 crnt = &(*crnt)->next; 17 27 } 18 28 *crnt = NULL; 19 29 } 20 21 struct stack* assign_stack(struct stack* s, const struct stack* t, 22 void* (*copy_el)(const void*), void (*free_el)(void*)) { 30 struct stack * assign_stack( struct stack * s, const struct stack * t, 31 void * (*copy_el)( const void * ), void (*free_el)( void * ) ) { 23 32 if ( s->head == t->head ) return s; 24 33 clear_stack( s, free_el ); /***/ … … 27 36 } 28 37 29 void clear_stack(struct stack* s, void (*free_el)(void*)) { 30 for ( struct stack_node* next = s->head; next; ) { 31 struct stack_node* crnt = next; 32 next = crnt->next; 33 free_el(crnt->value); 34 free(crnt); 35 } 36 s->head = NULL; 37 } 38 _Bool stack_empty( const struct stack * s ) { return s->head == NULL; } 38 39 39 _Bool stack_empty(const struct stack* s) { return s->head == NULL; } 40 41 void push_stack(struct stack* s, void* value) { 42 struct stack_node* n = malloc(sizeof(struct stack_node)); /***/ 40 void push_stack( struct stack * s, void * value ) { 41 struct stack_node * n = malloc( sizeof(struct stack_node) ); /***/ 43 42 *n = (struct stack_node){ value, s->head }; /***/ 44 43 s->head = n; 45 44 } 46 45 47 void * pop_stack(struct stack* s) {48 struct stack_node * n = s->head;46 void * pop_stack( struct stack * s ) { 47 struct stack_node * n = s->head; 49 48 s->head = n->next; 50 void * x= n->value;51 free( n);52 return x;49 void * v = n->value; 50 free( n ); 51 return v; 53 52 }
Note: See TracChangeset
for help on using the changeset viewer.