aaron-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 a0ad7dc was
122aecd,
checked in by Aaron Moss <a3moss@…>, 6 years ago
|
Expand benchmarks
|
-
Property mode set to
100644
|
File size:
1.0 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 | struct stack new_stack() { |
---|
10 | return (struct stack){ NULL }; |
---|
11 | } |
---|
12 | |
---|
13 | void copy_stack(struct stack* s, struct stack* t, void* (*copy)(void*)) { |
---|
14 | struct stack_node** crnt = &s->head; |
---|
15 | struct stack_node* next = t->head; |
---|
16 | while ( next ) { |
---|
17 | *crnt = malloc(sizeof(struct stack_node)); |
---|
18 | **crnt = (struct stack_node){ copy(next->value) }; |
---|
19 | crnt = &(*crnt)->next; |
---|
20 | next = next->next; |
---|
21 | } |
---|
22 | *crnt = 0; |
---|
23 | } |
---|
24 | |
---|
25 | void clear_stack(struct stack* s) { |
---|
26 | struct stack_node* next = s->head; |
---|
27 | while ( next ) { |
---|
28 | struct stack_node* crnt = next; |
---|
29 | next = crnt->next; |
---|
30 | free(crnt->value); |
---|
31 | free(crnt); |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | _Bool stack_empty(const struct stack* s) { |
---|
36 | return s->head == NULL; |
---|
37 | } |
---|
38 | |
---|
39 | void push_stack(struct stack* s, void* value) { |
---|
40 | struct stack_node* n = malloc(sizeof(struct stack_node)); |
---|
41 | *n = (struct stack_node){ value, s->head }; |
---|
42 | s->head = n; |
---|
43 | } |
---|
44 | |
---|
45 | void* pop_stack(struct stack* s) { |
---|
46 | struct stack_node* n = s->head; |
---|
47 | s->head = n->next; |
---|
48 | void* x = n->value; |
---|
49 | free(n); |
---|
50 | return x; |
---|
51 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.