Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 560ea8d8af
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 64 lines (51 sloc) 1.238 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include "rescheme.h"

#include <assert.h>

static struct rs_stack *rs_stack_alloc_frame(void *data);

struct rs_stack *rs_stack_push(struct rs_stack *stack, void *data)
{
struct rs_stack *new_top = rs_stack_alloc_frame(data);
if (stack != NULL) {
new_top->next = stack;
}
return new_top;
}

void *rs_stack_pop(struct rs_stack **stack)
{
assert(stack != NULL);
assert(*stack != NULL);

struct rs_stack *old_top = *stack;
*stack = old_top->next;
void *data = old_top->data;

free(old_top);
return data;
}


void *rs_stack_top(struct rs_stack *stack)
{
assert(stack !=NULL);
return stack->data;
}


static struct rs_stack *rs_stack_alloc_frame(void *data)
{
struct rs_stack *frame = malloc(sizeof(struct rs_stack));
if (frame == NULL) {
rs_fatal("could not allocate stack frame:");
}
frame->data = data;
frame->next = NULL;
return frame;
}


void rs_stack_test(void)
{
struct rs_stack *stack = NULL;
stack = rs_stack_push(stack, (void *) 1);
stack = rs_stack_push(stack, (void *) 2);
stack = rs_stack_push(stack, (void *) 3);
for (int i = 3; i > 0; i--) {
int data = (int) rs_stack_pop(&stack);
if (data != i) {
rs_fatal("expected %d, got %d", i, data);
}
}
assert(stack == NULL);
TRACE("passed");
}
Something went wrong with that request. Please try again.