ADTaaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change
on this file since 860f19f was
860f19f,
checked in by Aaron Moss <a3moss@…>, 5 years ago
|
Clean up code edits
|
-
Property mode set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | #include <stdlib.h> |
---|
2 | #include "c-stack.h" |
---|
3 | |
---|
4 | struct stack_node { |
---|
5 | void * value; |
---|
6 | struct stack_node * next; |
---|
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 | } |
---|
18 | |
---|
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; |
---|
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 }; /***/ |
---|
43 | s->head = n; |
---|
44 | } |
---|
45 | |
---|
46 | void * pop_stack( struct stack * s ) { |
---|
47 | struct stack_node * n = s->head; |
---|
48 | s->head = n->next; |
---|
49 | void * v = n->value; |
---|
50 | free( n ); |
---|
51 | return v; |
---|
52 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.