source: doc/generic_types/evaluation/c-stack.c @ 12d3187

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 12d3187 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
RevLine 
[309be81]1#include <stdlib.h>
2#include "c-stack.h"
3
4struct stack_node {
5        void* value;
6        struct stack_node* next;
7};
8
[a381b46]9struct stack new_stack() { return (struct stack){ NULL }; /***/ }
[309be81]10
[87c5f40]11void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const void*)) {
[122aecd]12        struct stack_node** crnt = &s->head;
13        struct stack_node* next = t->head;
14        while ( next ) {
[3fb7f5e]15                *crnt = malloc(sizeof(struct stack_node)); /***/
16                **crnt = (struct stack_node){ copy(next->value) }; /***/
[122aecd]17                crnt = &(*crnt)->next;
18                next = next->next;
19        }
20        *crnt = 0;
21}
22
[87c5f40]23void clear_stack(struct stack* s, void (*free_el)(void*)) {
[309be81]24        struct stack_node* next = s->head;
25        while ( next ) {
26                struct stack_node* crnt = next;
27                next = crnt->next;
[87c5f40]28                free_el(crnt->value);
[309be81]29                free(crnt);
30        }
[d919f47]31        s->head = NULL;
[309be81]32}
33
[a381b46]34_Bool stack_empty(const struct stack* s) { return s->head == NULL; }
[309be81]35
36void push_stack(struct stack* s, void* value) {
[3fb7f5e]37        struct stack_node* n = malloc(sizeof(struct stack_node)); /***/
38        *n = (struct stack_node){ value, s->head }; /***/
[309be81]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.