Last change
on this file since 478dade was
4c80a75,
checked in by Peter A. Buhr <pabuhr@…>, 7 years ago
|
update evaluation programs
|
-
Property mode set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | #include <stdlib.h> |
---|
2 | #include "c-stack.h" |
---|
3 | |
---|
4 | typedef struct node { |
---|
5 | void * value; |
---|
6 | struct node * next; |
---|
7 | } node; |
---|
8 | |
---|
9 | void copy_stack( stack * s, const stack * t, void * (*copy)( const void * ) ) { |
---|
10 | node ** cr = &s->head; |
---|
11 | for ( node * nx = t->head; nx; nx = nx->next ) { |
---|
12 | *cr = malloc( sizeof(node) ); /***/ |
---|
13 | (*cr)->value = copy( nx->value ); |
---|
14 | cr = &(*cr)->next; |
---|
15 | } |
---|
16 | *cr = NULL; |
---|
17 | } |
---|
18 | |
---|
19 | void clear_stack( stack * s, void (* free_el)( void * ) ) { |
---|
20 | for ( node * nx = s->head; nx; ) { |
---|
21 | node * cr = nx; |
---|
22 | nx = cr->next; |
---|
23 | free_el( cr->value ); |
---|
24 | free( cr ); |
---|
25 | } |
---|
26 | s->head = NULL; |
---|
27 | } |
---|
28 | |
---|
29 | stack new_stack() { |
---|
30 | return (stack){ NULL }; /***/ |
---|
31 | } |
---|
32 | |
---|
33 | stack * assign_stack( stack * s, const stack * t, |
---|
34 | void * (*copy_el)( const void * ), void (*free_el)( void * ) ) { |
---|
35 | if ( s->head == t->head ) return s; |
---|
36 | clear_stack( s, free_el ); /***/ |
---|
37 | copy_stack( s, t, copy_el ); /***/ |
---|
38 | return s; |
---|
39 | } |
---|
40 | |
---|
41 | _Bool stack_empty( const stack * s ) { |
---|
42 | return s->head == NULL; |
---|
43 | } |
---|
44 | |
---|
45 | void push_stack( stack * s, void * v ) { |
---|
46 | node * n = malloc( sizeof(node) ); /***/ |
---|
47 | *n = (node){ v, s->head }; /***/ |
---|
48 | s->head = n; |
---|
49 | } |
---|
50 | |
---|
51 | void * pop_stack( stack * s ) { |
---|
52 | node * n = s->head; |
---|
53 | s->head = n->next; |
---|
54 | void * v = n->value; |
---|
55 | free( n ); |
---|
56 | return v; |
---|
57 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.