Last change
on this file since ba068c0 was
f4e3419d,
checked in by Peter A. Buhr <pabuhr@…>, 7 years ago
|
restructure paper documents
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[309be81] | 1 | #include <stdlib.h> |
---|
| 2 | #include "c-stack.h" |
---|
| 3 | |
---|
| 4 | struct stack_node { |
---|
| 5 | void* value; |
---|
| 6 | struct stack_node* next; |
---|
| 7 | }; |
---|
| 8 | |
---|
[a381b46] | 9 | struct stack new_stack() { return (struct stack){ NULL }; /***/ } |
---|
[309be81] | 10 | |
---|
[87c5f40] | 11 | void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const void*)) { |
---|
[122aecd] | 12 | struct stack_node** crnt = &s->head; |
---|
[6a8ac0b] | 13 | for ( struct stack_node* next = t->head; next; next = next->next ) { |
---|
[3fb7f5e] | 14 | *crnt = malloc(sizeof(struct stack_node)); /***/ |
---|
| 15 | **crnt = (struct stack_node){ copy(next->value) }; /***/ |
---|
[122aecd] | 16 | crnt = &(*crnt)->next; |
---|
| 17 | } |
---|
| 18 | *crnt = 0; |
---|
| 19 | } |
---|
| 20 | |
---|
[87c5f40] | 21 | void clear_stack(struct stack* s, void (*free_el)(void*)) { |
---|
[6a8ac0b] | 22 | for ( struct stack_node* next = s->head; next; ) { |
---|
[309be81] | 23 | struct stack_node* crnt = next; |
---|
| 24 | next = crnt->next; |
---|
[87c5f40] | 25 | free_el(crnt->value); |
---|
[309be81] | 26 | free(crnt); |
---|
| 27 | } |
---|
[d919f47] | 28 | s->head = NULL; |
---|
[309be81] | 29 | } |
---|
| 30 | |
---|
[a381b46] | 31 | _Bool stack_empty(const struct stack* s) { return s->head == NULL; } |
---|
[309be81] | 32 | |
---|
| 33 | void push_stack(struct stack* s, void* value) { |
---|
[3fb7f5e] | 34 | struct stack_node* n = malloc(sizeof(struct stack_node)); /***/ |
---|
| 35 | *n = (struct stack_node){ value, s->head }; /***/ |
---|
[309be81] | 36 | s->head = n; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void* pop_stack(struct stack* s) { |
---|
| 40 | struct stack_node* n = s->head; |
---|
| 41 | s->head = n->next; |
---|
| 42 | void* x = n->value; |
---|
| 43 | free(n); |
---|
| 44 | return x; |
---|
[6a8ac0b] | 45 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.