-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
48 lines (38 loc) · 1.03 KB
/
stack.c
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
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "stack.h"
Stack *stack_new() {
Stack *stack = malloc(sizeof(Stack));
stack->size = 0;
stack->content = 0;
return stack;
}
void stack_free(Stack *stack) {
if (stack->size > 0) {
free(stack->content);
}
free(stack);
}
void stack_push(Stack *stack, void *item) {
stack->size++;
// We expand the memory allocated by one word, then write the new
// value to the end.
stack->content =
realloc(stack->content, stack->size * sizeof *stack->content);
stack->content[stack->size - 1] = item;
}
void *stack_pop(Stack *stack) {
assert(stack->size >= 1);
stack->size--;
void *item = stack->content[stack->size];
stack->content =
realloc(stack->content, stack->size * sizeof *stack->content);
return item;
}
void *stack_peek(Stack *stack) {
assert(stack->size >= 1);
return stack->content[stack->size - 1];
}
bool stack_empty(Stack *stack) { return stack->size == 0; }