-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
52 lines (41 loc) · 977 Bytes
/
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 <stdlib.h>
#include <stdbool.h>
#include "stack.h"
void stack_init(struct stack_t *stack, size_t capacity)
{
stack->size = 0;
stack->capacity = capacity;
stack->items = (void **)(malloc(capacity * sizeof(void *)));
}
void free_stack(struct stack_t *stack)
{
free(stack->items);
free(stack);
}
inline bool stack_empty(struct stack_t *stack)
{
return !stack->size;
}
inline bool stack_full(struct stack_t *stack)
{
return stack->size == stack->capacity;
}
inline bool stack_push(struct stack_t *stack, void *item)
{
if (stack_full(stack))
return false;
*(stack->items + stack->size++) = item;
return true;
}
inline void *stack_pop(struct stack_t *stack)
{
if (stack_empty(stack))
return NULL;
return *(stack->items + --stack->size);
}
inline void *stack_get(struct stack_t *stack)
{
if (stack_empty(stack))
return NULL;
return *(stack->items + stack->size - 1);
}