source: doc/generic_types/evaluation/c-stack.c @ 0d10090

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 0d10090 was 87c5f40, checked in by Aaron Moss <a3moss@…>, 7 years ago

Update benchmarks to include stack of pairs

  • 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() {
10        return (struct stack){ NULL };
11}
12
13void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const 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
25void clear_stack(struct stack* s, void (*free_el)(void*)) {
26        struct stack_node* next = s->head;
27        while ( next ) {
28                struct stack_node* crnt = next;
29                next = crnt->next;
30                free_el(crnt->value);
31                free(crnt);
32        }
33        s->head = NULL;
34}
35
36_Bool stack_empty(const struct stack* s) {
37        return s->head == NULL;
38}
39
40void push_stack(struct stack* s, void* value) {
41        struct stack_node* n = malloc(sizeof(struct stack_node));
42        *n = (struct stack_node){ value, s->head };
43        s->head = n;
44}
45
46void* pop_stack(struct stack* s) {
47        struct stack_node* n = s->head;
48        s->head = n->next;
49        void* x = n->value;
50        free(n);
51        return x;
52}
Note: See TracBrowser for help on using the repository browser.