-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
43 lines (34 loc) · 895 Bytes
/
stack.c
File metadata and controls
43 lines (34 loc) · 895 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include "../includes/stack.h"
#include "../includes/memory.h"
Stack *stack_new() {
Stack* stack = malloc(sizeof(Stack));
if (stack == NULL) {
fprintf(stderr, "Could not allocate memory for stack\n");
return NULL;
}
stack->count = 0;
stack->capacity = 0;
stack->items = NULL;
return stack;
}
void stack_free(Stack *stack) {
free(stack->items);
free(stack);
}
Word* stack_push(Stack* stack, Word word) {
if (stack->count >= stack->capacity) {
REALLOC_DA(Word, stack);
}
stack->items[stack->count] = word;
return &stack->items[stack->count++];
}
void stack_trace(Stack *stack) {
printf("STACK[%"PRIu64"]: [ ", stack->count);
for (size_t i = 0; i < stack->count; i++) {
print_word(stdout, stack->items[i], 0);
printf(" ");
}
printf("]\n");
}