-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
52 lines (46 loc) · 1.31 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
49
50
51
52
#include "stack.h"
stack_t *stack_create(void) {
return (stack_t *)calloc(1, sizeof(stack_t));
}
void stack_push(stack_t *stack, void *val) {
stack_elem_t* new_elem = (stack_elem_t *)malloc(sizeof(stack_elem_t));
new_elem->value = val;
new_elem->next = stack->top;
stack->top = new_elem;
}
void *stack_pop(stack_t *stack) {
if (!stack) { return NULL; }
void *val = NULL;
stack_elem_t *top_frame = stack->top;
if (top_frame) {
val = top_frame->value;
stack->top = top_frame->next;
free(top_frame);
} else {
stack->top = NULL;
}
return val;
}
void *stack_peek(stack_t *stack) {
if (!stack || !stack->top) { return NULL; }
return stack->top->value;
}
void stack_destroy(stack_t *stack) {
stack_elem_t *current_frame = stack->top;
while (current_frame) {
stack_elem_t *next = current_frame->next;
free(current_frame);
current_frame = next;
}
free(stack);
}
void stack_destroy_with_elements(stack_t *stack) {
stack_elem_t *current_frame = stack->top;
while (current_frame) {
stack_elem_t *next = current_frame->next;
free(current_frame->value);
free(current_frame);
current_frame = next;
}
free(stack);
}