SICP/shared/scm2c/stack.c

42 lines
677 B
C

#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
struct stack proc_stack;
stack* create_stack(void) {
stack* s = malloc(sizeof(stack));
if (!s) exit(-1);
s->first = NULL;
return s;
}
void save(void *d, stack *s) {
stack_elem* elem = malloc(sizeof(stack_elem));
elem->value = d;
if (!s->first) {
elem->next = NULL;
} else {
elem->next = s->first;
}
s->first = elem;
}
void* restore(stack *s) {
void *r;
stack_elem *se;
if (!s->first) {
printf("stack empty!\n");
exit(-1);
}
se = s->first;
r = s->first->value;
s->first = se->next;
free(se);
return r;
}