about summary refs log tree commit diff
path: root/usth/ICT2.1/labwork/3/stack.c
blob: 708fb59052ff7083808fd7491e3aae81fe76df7d (plain) (blame)
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
/*
 * Stack implemented using linked list.
 * This is free and unencumbered software released into the public domain.
 */

#include <stdlib.h>

#include "stack.h"

stack *mkstack()
{
	stack *s = malloc(sizeof(stack));
	s->stack = NULL;
	return s;
}

int stack_empty(stack *s)
{
	return s->stack == NULL;
}

void stack_push(stack *s, void *item)
{
	s->stack = cons(item, s->stack);
}

void *stack_top(stack *s)
{
	return car(s->stack);
}

void *stack_pop(stack *s)
{
	void *first = car(s->stack);
	construct *rest = cdr(s->stack);
	free(s->stack);
	s->stack = rest;
	return first;
}