ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change
on this file since 81e8ab0 was
81e8ab0,
checked in by Peter A. Buhr <pabuhr@…>, 6 years ago
|
more updates
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[604e76d] | 1 | #include <stdlib.h> |
---|
| 2 | #include "c-stack.h" |
---|
| 3 | |
---|
| 4 | struct stack_node { |
---|
[81e8ab0] | 5 | void * value; |
---|
| 6 | struct stack_node * next; |
---|
[604e76d] | 7 | }; |
---|
| 8 | |
---|
| 9 | struct stack new_stack() { return (struct stack){ NULL }; /***/ } |
---|
| 10 | |
---|
[81e8ab0] | 11 | void clear_stack( struct stack * s, void (*free_el)( void * ) ) { |
---|
| 12 | for ( struct stack_node * next = s->head; next; ) { |
---|
| 13 | struct stack_node * crnt = next; |
---|
| 14 | next = crnt->next; |
---|
| 15 | free_el( crnt->value ); |
---|
| 16 | free( crnt ); |
---|
[604e76d] | 17 | } |
---|
[81e8ab0] | 18 | s->head = NULL; |
---|
[604e76d] | 19 | } |
---|
| 20 | |
---|
[81e8ab0] | 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; |
---|
[604e76d] | 27 | } |
---|
[81e8ab0] | 28 | *crnt = NULL; |
---|
[604e76d] | 29 | } |
---|
| 30 | |
---|
[81e8ab0] | 31 | _Bool stack_empty( const struct stack * s ) { return s->head == NULL; } |
---|
[604e76d] | 32 | |
---|
[81e8ab0] | 33 | void push_stack( struct stack * s, void * value ) { |
---|
| 34 | struct stack_node * n = malloc( sizeof(struct stack_node) ); /***/ |
---|
[604e76d] | 35 | *n = (struct stack_node){ value, s->head }; /***/ |
---|
| 36 | s->head = n; |
---|
| 37 | } |
---|
| 38 | |
---|
[81e8ab0] | 39 | void * pop_stack( struct stack * s ) { |
---|
| 40 | struct stack_node * n = s->head; |
---|
[604e76d] | 41 | s->head = n->next; |
---|
[81e8ab0] | 42 | void * v = n->value; |
---|
| 43 | free( n ); |
---|
| 44 | return v; |
---|
[604e76d] | 45 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.