source: doc/generic_types/evaluation/c-stack.c @ 4e9151f

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 4e9151f was a381b46, checked in by Aaron Moss <a3moss@…>, 7 years ago

Minor cleanup, also filled in benchmark source appendix

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <stdlib.h>
2#include "c-stack.h"
3
4struct stack_node {
5        void* value;
6        struct stack_node* next;
7};
8
9struct stack new_stack() { return (struct stack){ NULL }; /***/ }
10
11void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const void*)) {
12        struct stack_node** crnt = &s->head;
13        struct stack_node* next = t->head;
14        while ( next ) {
15                *crnt = malloc(sizeof(struct stack_node)); /***/
16                **crnt = (struct stack_node){ copy(next->value) }; /***/
17                crnt = &(*crnt)->next;
18                next = next->next;
19        }
20        *crnt = 0;
21}
22
23void clear_stack(struct stack* s, void (*free_el)(void*)) {
24        struct stack_node* next = s->head;
25        while ( next ) {
26                struct stack_node* crnt = next;
27                next = crnt->next;
28                free_el(crnt->value);
29                free(crnt);
30        }
31        s->head = NULL;
32}
33
34_Bool stack_empty(const struct stack* s) { return s->head == NULL; }
35
36void push_stack(struct stack* s, void* value) {
37        struct stack_node* n = malloc(sizeof(struct stack_node)); /***/
38        *n = (struct stack_node){ value, s->head }; /***/
39        s->head = n;
40}
41
42void* pop_stack(struct stack* s) {
43        struct stack_node* n = s->head;
44        s->head = n->next;
45        void* x = n->value;
46        free(n);
47        return x;
48}
Note: See TracBrowser for help on using the repository browser.