source: doc/papers/general/evaluation/c-stack.c@ ad486c5b

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since ad486c5b was 4c80a75, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

update evaluation programs

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[604e76d]1#include <stdlib.h>
2#include "c-stack.h"
3
[ac4dad2]4typedef struct node {
[81e8ab0]5 void * value;
[ac4dad2]6 struct node * next;
7} node;
8
9void copy_stack( stack * s, const stack * t, void * (*copy)( const void * ) ) {
10 node ** cr = &s->head;
11 for ( node * nx = t->head; nx; nx = nx->next ) {
12 *cr = malloc( sizeof(node) ); /***/
13 (*cr)->value = copy( nx->value );
14 cr = &(*cr)->next;
[604e76d]15 }
[ac4dad2]16 *cr = NULL;
[604e76d]17}
18
[ac4dad2]19void clear_stack( stack * s, void (* free_el)( void * ) ) {
20 for ( node * nx = s->head; nx; ) {
21 node * cr = nx;
22 nx = cr->next;
23 free_el( cr->value );
24 free( cr );
[604e76d]25 }
[ac4dad2]26 s->head = NULL;
[604e76d]27}
[ac4dad2]28
[4c80a75]29stack new_stack() {
30 return (stack){ NULL }; /***/
31}
[ac4dad2]32
33stack * assign_stack( stack * s, const stack * t,
[8b001bd]34 void * (*copy_el)( const void * ), void (*free_el)( void * ) ) {
[e59f0bf]35 if ( s->head == t->head ) return s;
36 clear_stack( s, free_el ); /***/
37 copy_stack( s, t, copy_el ); /***/
38 return s;
39}
[604e76d]40
[4c80a75]41_Bool stack_empty( const stack * s ) {
42 return s->head == NULL;
43}
[604e76d]44
[ac4dad2]45void push_stack( stack * s, void * v ) {
46 node * n = malloc( sizeof(node) ); /***/
47 *n = (node){ v, s->head }; /***/
[604e76d]48 s->head = n;
49}
50
[ac4dad2]51void * pop_stack( stack * s ) {
52 node * n = s->head;
[604e76d]53 s->head = n->next;
[81e8ab0]54 void * v = n->value;
55 free( n );
56 return v;
[604e76d]57}
Note: See TracBrowser for help on using the repository browser.